• Hi all, we’re building a new site for a client and they have a complex discount requirement.

    When 16 items are added to the basket (any combination just a total of 16 items in the basket) they want to apply a discount to all items, but the discount for each product is different.

    So they may have 10 of item A, and 6 of item B which is a total of 16.

    Item A would get a 10% discount whilst item B would get a 5% discount.

    Is this possible or are we going to have to code our own?

    Thanks ??

    https://www.ads-software.com/plugins/woocommerce/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    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;
    }

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    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 ??

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    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 ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Complex discount requirements’ is closed to new replies.