Hi.
Just for 16, not >= 16?
I think this may require some customisation since it’s such a specific rule.
One approach, for the discount amounts, if there are many different % values you could probably put the % value in a custom field per product.
If there are only a handful of products, the logic could just go in the code and look at the product IDs themselves.
To run the code, I think the ‘woocommerce_get_discounted_price’ hook would be suitable. Thats ran per line and gets passed the line items.
As for the count, we have a helper called “get_cart_contents_count()” so that part is simple enough.
add_filter( 'woocommerce_get_discounted_price', 'custom_discounted_price_rules', 10, 2 );
function custom_discounted_price_rules( $price, $values ) {
if ( 16 === WC()->cart->get_cart_contents_count() ) {
$product_id = $values['product_id'];
switch ( $product_id ) {
case 901 : // For product ID 901
$discount = '5'; // 5%
break;
default :
$discount = 0;
break;
}
if ( $discount ) {
$price = $price - ( $price / 100 * $discount );
}
}
return $price;
}
This would apply a 5% discount on ID 901 if cart count is 16. As for display, it won’t show anything special, just an adjusted total in the cart, so you’d probably want to show that the discount happened somewhere, maybe in the totals table or as a notice.
Thread Starter
Zoee
(@epixmedia)
Hi Mike, thanks for the reply, I’ve finally had some time to test this and I can’t get it to work.
I’ve put my 4 product ID’s in and give them all a 5% discount but it doesnt apply anything.
Any ideas?
add_filter( "woocommerce_get_discounted_price", "custom_discounted_price_rules", 10, 2 );
function custom_discounted_price_rules( $price, $values ) {
if ( 16 === WC()->cart->get_cart_contents_count() ) {
$product_id = $values['product_id'];
switch ( $product_id ) {
case 91 : //Grass-tastic
$discount = '5'; // 5%
break;
case 89 : //Alfalfa-mazing
$discount = '5'; // 5%
break;
case 86 : //Alfalfa Pellets
$discount = '5'; // 5%
break;
case 83 : //Grass Pellets
$discount = '5'; // 5%
break;
default :
$discount = 0;
break;
}
if ( $discount ) {
$price = $price - ( $price / 100 * $discount );
}
}
return $price;
}
Just to clarify, you had 16 items in cart? Thats the quantities combined.
Try changing:
$product_id = $values['product_id'];
to
$product_id = absint( $values['product_id'] );
This will keep it numeric.
The code I posted above did work fine on my test install.
Thread Starter
Zoee
(@epixmedia)
Hi Mike, I’ve just added some echo’s and it gets inside the code in the correct places, and if I echo $price it echos the new single product price wit hte 5% discount, but no where on the basket does it adjust the prices. Sub totals and total still equal the total price without the basket….
I’m confused 🙂
Thread Starter
Zoee
(@epixmedia)
So I emptied all but one product from my basket, and it wokred. So I added another back, and it worked!
Figure that out. Am just going to test it a bit further but it looks like its working 🙂
Is there a hook to edit the single item price or whats the best hook to show a message above the total?
Thanks 🙂
Single item price in the cart table? Show me where? I don’t think there was in testing, it just affected the totals.
Thread Starter
Zoee
(@epixmedia)
Hi Mike, I’m revisiting this and struggling to find what I need. My discount code works perfectly, but I need to display a message somewhere on the basket (above it, or ideally just above the “total” to indicate to the user that the discount has been applied.
What’s the best way to do this?
Thanks 🙂