Search code examples
wordpresswoocommerceproductcartcoupon

Change product sale price to regular price in WooCommerce cart when a coupon is applied, but exclude certain categories


When a coupon is applied (belonging to a certain type) I change the product discount price to the regular price via:

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);
function add_custom_price( $cart_object) {

    global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $coupon = False;

    if ($coupons = WC()->cart->get_applied_coupons()  == False ) 
      $coupon = False;
    else {
        foreach ( WC()->cart->get_applied_coupons() as $code ) {
          $coupons1 = new WC_Coupon( $code );
          if ($coupons1->type == 'percent_product' || $coupons1->type == 'percent')
            $coupon = True;
        }
    }

    if ($coupon == True)
        foreach ( $cart_object->get_cart() as $cart_item ) 
        {
            $price = $cart_item['data']->regular_price;
            $cart_item['data']->set_price( $price );
        }
}

But if I have a category excluded, the code freaks out because it changes the price from sale to regular in the cart and does not add a discount.

How to work around this so that the excluded category does not change to the regular price?


Solution

  • To exclude certain categories you can use has_term() when loop through the cart items

    So you get:

    function action_woocommerce_before_calculate_totals( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
        
        // Specific categories: the term name/term_id/slug. Several could be added, separated by a comma
        $excluded_categories = array( 63, 15, 'categorie-1' );
    
        // Initialize
        $coupon_flag = false;
    
        // Loop through applied coupons
        foreach ( $cart->get_applied_coupons() as $coupon_code ) {
            // Get an instance of the WC_Coupon Object
            $coupon = new WC_Coupon( $coupon_code );
            
            // Only for certain types, several can be added, separated by a comma
            if ( in_array( $coupon->get_discount_type(), array( 'percent', 'percent_product' ) ) ) {
                $coupon_flag = true;
                break;
            }
        }
        
        // True
        if ( $coupon_flag ) {
            // Loop through cart items
            foreach ( $cart->get_cart() as $cart_item ) {
                // Get product ID in
                $product_id = $cart_item['product_id'];
    
                // NOT contains the definite term
                if ( ! has_term( $excluded_categories, 'product_cat', $product_id ) ) {
                    // Get regular price
                    $regular_price = $cart_item['data']->get_regular_price();
                    
                    // Set new price
                    $cart_item['data']->set_price( $regular_price );
                }
            }
        }
    }
    add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );