-1

What I'm doing: I am creating a coupon based promo. The coupon will discount 10% of the most expensive item in the cart that belongs to a specific product category.

The problem: The code below successfully checks if the cart contains products from the specific category, and obtains 10% of the price of the most expensive product from that array to use for the discount. However, I can't get the coupon discount amount to change in the cart.

The code:

function change_coupon_price_for_promo( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) ){
        return;
    }
    
    // coupon code
    $coupon_name = 'MYCOUPONNAME'; 
    
    // if coupon is applied to cart...
    if( $cart->has_discount( $coupon_name ) ) {
        
        // Form array of products from the cart that belong to the specified category
        $specProduct = array(); // array of specific products in cart
        foreach ( $cart->get_cart() as $key => $cart_item ) {
            $cart_item_id = $cart_item['product_id'];
            $terms = wp_get_post_terms( $cart_item_id, 'product_cat' );
            foreach ( $terms as $term ){
                // gets product cat id
                $product_cat_id = $term->term_id;
                // gets an array of all parent category levels
                $product_parent_categories_all_hierachy = get_ancestors( $product_cat_id, 'product_cat' );      
                // This cuts the array and extracts the last set in the array
                $last_parent_cat = array_slice($product_parent_categories_all_hierachy, -1, 1, true);
                if(in_array(3831, $last_parent_cat)){
                    $specProduct[$key] = $cart_item_id;
                    break;
                }
            }
        }
        
        //if there are items from the specific category in the cart
        if(!empty($specProduct)){
            $specPriceArray = array(); // array of specific product prices in cart
            foreach ($specProduct as $key => $specItem) {
                $thisProd = wc_get_product($specItem);
                $thisPrice = $thisProd->get_price();
                $specPriceArray[$key] = $thisPrice;
            }
            
            // most expensive specific product price
            $highestSpec = max($specPriceArray);
            // discount of most expensive item
            $finalDiscount = $highestSpec * .1;
            
            // print_r($specProduct); GOOD
            // print_r($specPriceArray); GOOD
            // echo $highestSpec; GOOD
            // echo $finalDiscount; GOOD

            /***** APPLY COUPON CHANGE HERE<-------HOW??? *****/
            $cart->discount_total = $finalDiscount;
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'change_coupon_price_for_promo', 10, 1 ); 

What I'm asking:

The last line of the code before the closing brackets...

$cart->discount_total = $finalDiscount;

is what I tried using to change the coupon's discount in the cart. It doesn't work. How can I accomplish this? TY.

EDIT: I should note that the coupon being added is set up as a "Fixed cart discount" with a rate of 0 so it is added as a line item to the order. This way the user sees the discount for the coupon code on its own line in the cart/checkout.

Seb G
  • 661
  • 1
  • 15
  • 34

1 Answers1

0

you can use function set_price. you can call it in your code like:

$cart_item['data']->set_price('New price here')

use this inside the foreach that loops your cart items

Dharman
  • 30,962
  • 25
  • 85
  • 135
Meni
  • 119
  • 1
  • 4
  • 13
  • True, I could definitely discount the product itself, but the coupon would still show as a line item with a value of $0.00. I could change it on the front-end with jQuery to match though so I'll keep this work around in mind! Thanks – Seb G Jul 14 '20 at 17:15
  • have you seen this answer? [link](https://stackoverflow.com/questions/22928367/how-to-create-custom-discount-for-the-cart-in-woocommerce) – Meni Jul 14 '20 at 17:34
  • This should be the accepted answer to this question. – Meni Jul 23 '20 at 12:41