2

I am trying to apply discounts based on quantity amounts in the cart for each product. Each product has it's own quantity discounts set by custom fields. I'm using the action "woocommerce_before_calculate_totals" and in the foreach loop trying to apply the discount from the custom fields. However, it only works for the first item in the cart. The second item is set at the full price. I can't see why it doesn't work for the rest of the items in the cart.

Here is my current code:

add_action( 'woocommerce_before_calculate_totals', 'set_custom_cart_item_price', 20, 1 );

function set_custom_cart_item_price( $wc_cart ) {

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

    // First loop to check if product 11 is in cart
    foreach ( $wc_cart->get_cart() as $key => $cart_item ){

        //  This is necessary for WC 3.0+
        $qty = $cart_item['quantity'];

        $discount1 = get_post_meta($cart_item['product_id'], '_discount_1_4_items', true);
        $discount2 = get_post_meta($cart_item['product_id'], '_discount_5_9_items', true);
        $discount3 = get_post_meta($cart_item['product_id'], '_discount_10_19_items', true);
        $discount4 = get_post_meta($cart_item['product_id'], '_discount_20_items', true);

        $OriginalPrice = $cart_item['data']->get_price();

        if($qty >= 1 && $qty <=4){
            $FinalPrice = $OriginalPrice*$discount1;
        }elseif($qty >= 5 && $qty <=9){
            $FinalPrice = $OriginalPrice*$discount2;
        }elseif($qty >= 10 && $qty <=19){
            $FinalPrice = $OriginalPrice*$discount3;
        }elseif($qty >= 20 ){
            $FinalPrice = $OriginalPrice*$discount4;
        }
        $cart_item['data']->set_price( $FinalPrice );
    }

}
1
  • Warning: A non-numeric value encountered in Commented Jun 22, 2022 at 8:40

2 Answers 2

1

So the answer to this is instead of placing the code in your functions.php, you create a "woocommerce" folder in your theme folder, and place the "cart" folder and the "cart.php" template file from the woocommerce plugin inside of that. Then you place the same code as in the "woocommerce_before_calculate_totals" action inside the loop of the cart template. So the code below is what I placed in the cart.php file and it now works!

$qty = $cart_item['quantity'];

$discount1 = get_post_meta($cart_item['product_id'], '_discount_1_4_items', true);
$discount2 = get_post_meta($cart_item['product_id'], '_discount_5_9_items', true);
$discount3 = get_post_meta($cart_item['product_id'], '_discount_10_19_items', true);
$discount4 = get_post_meta($cart_item['product_id'], '_discount_20_items', true);

$OriginalPrice = $cart_item['data']->get_price();

if($qty >= 1 && $qty <=4){
    $FinalPrice = $OriginalPrice*$discount1;
}elseif($qty >= 5 && $qty <=9){
    $FinalPrice = $OriginalPrice*$discount2;
}elseif($qty >= 10 && $qty <=19){
    $FinalPrice = $OriginalPrice*$discount3;
}elseif($qty >= 20 ){
    $FinalPrice = $OriginalPrice*$discount4;
}
$cart_item['data']->set_price( $FinalPrice );
0

In your code, you are trying to equate $qty to the number of items $qty = 5 which should be $qty == 5.

You don't need to check if $qty == 5 && $qty == 10 you might need to change that to || operator to check for either of the case else the case might not be truthy.

And you are applying discount 4 for all the four cases so you might need to refactor your code.

2
  • I adjusted the code as something happened when I copy and pasted. The new updated code above is the actual current code I am using. Commented Aug 23, 2018 at 6:40
  • Warning: A non-numeric value encountered * Commented Jun 22, 2022 at 8:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.