I need certain items to have a mix and match quantity discount based on category. I have seen some other codes that accomplish this using an overall discount at the bottom of the cart, however I need the price of each item to reflect the discount, and I also need that price to carry over to the sales order so we can invoice appropriately. Here's what I have so far:
add_action( 'woocommerce_cart_calculate_fees','custom_bulk_discount', 20, 1 );
function custom_bulk_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$category = 'ferns';
$calculated_qty = 0;
$calculated_total = 0;
foreach($cart->get_cart() as $cart_item):
if(has_term($category, 'product_cat', $cart_item['product_id'])):
$item_price = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->price : $cart_item['data']->get_price(); // The price for one (assuming that there is always 2.99)
$item_qty = $cart_item["quantity"];// Quantity
$item_line_total = $cart_item["line_total"]; // Item total price (price x quantity)
$calculated_qty += $item_qty; // ctotal number of items in cart
$calculated_total += $item_line_total; // calculated total items amount
endif;
endforeach;
if ( $calculated_qty >= 100 && $calculated_qty < 200 ){
foreach($cart->get_cart() as $cart_item):
$price = $cart_item['data']->price - 1;
if(has_term($category, 'product_cat', $cart_item['product_id'])):
$cart_item['data']->set_price( $price );
endif;
endforeach;
}else if( $calculated_qty >= 200){
foreach($cart->get_cart() as $cart_item):
$price = $cart_item['data']->price - 1.5;
if(has_term($category, 'product_cat', $cart_item['product_id'])):
$cart_item['data']->set_price( $price );
endif;
endforeach;
}
}
The problem is the subtotal isn't updating with the correct price. I'm assuming that is because I'm only changing the display price, not the price that is actually calculated. Is there a way to do what I'm asking?
First, you are not using the right hook, your code can be simplified and there are missing things.
To discount cart items based on quantity for specific product category try:
Code goes in functions.php file of your child theme (or in a plugin). It should work.