I'm having quite the issue with wordpress/woocommerce logic that is driving me crazy. I don't think woocommerce works the way it should out of the box but apparently its working correctly. Basically here is the situation: we have one coupon code that will give user 50% off any item in the barrel category. with the way it currently works if someone adds two barrels to the cart and applies the coupon they get 50% off each barrel - we only want them to get 50% off one barrel no matter how many different barrels they add to the cart. I had written some code that worked most of the time however now it seems to be failing most of the time. here is the code added to the functions.php file: /** * * WooCommerce Coupon Fixer Code. - Added 9-11-2014 BG * Limit usage to x items doesnt work by default, only on qty of that item not of that item type *in the cart. * **/ add_action('woocommerce_coupon_get_discount_amount', 'cart_level_coupon_variant_logic',10,5); function cart_level_coupon_variant_logic($discount, $discounting_amount, $cart_item, $single, $coupon) { // only for the right coupon code (anything with a limit on it) if (trim($coupon->limit_usage_to_x_items != '') && $discount > 0) { // if qty is 0 then return... can't work with div by 0 $qty = (int)$cart_item['quantity']; if ($qty == 0) return $discount; // global variable... don't like it but it works if (! isset($GLOBALS['coupon_qty_stash'])) { // the first time just create the variable and load the difference.. the stash $GLOBALS['coupon_qty_stash'] = (int)$coupon->limit_usage_to_x_items - $qty; } else { // every other time get the stash remaining global $coupon_qty_stash; // none remaining if ($coupon_qty_stash <= 0) return 0; // what's qty is left in coupon code stash? $diff = $coupon_qty_stash - $qty; if ($diff < 0) { // calculate appropriate discount $per_unit_discount = $discounting_amount / $qty; $discount = $per_unit_discount * $coupon_qty_stash; } $coupon_qty_stash = $diff; } } return $discount; } PHP: I need some help with this asap, its causing me a lot of customer support problems.
Unfortunately, not - but I see what you are getting at. The first time I apply this coupon it doesn't even show any coupon amt - not sure why
I don't have woo running right this sec but it seems to me that you would set the discount to product% rather than the entire cart(cart%). I am sort of exhausted though so I may be missing a bit of logic there. In theory that may be just a product specifically that the coupon code gets attached to rather than a cat unless you can iterate several products governed by said coupon code. hope that helps, Nigel
Nigel, That's the way I would expect it to work as well. I have tried creating the coupon code to be able to just apply to certain products but when I do this it still gives the same result. The coupon is set to give a 50% off product discount to any products in the "barrel" category and I have the box checked to limit the usage to 1 items. So if I have one item added in the category but set the qty to 2 it will only apply the coupon once. but if I have two items in the cart in the same category it applies coupon twice. Strangest part is I had this code working before the last wordpress/woocommerce update...
Strange. I have to start a woo commerce site tomorrow so I will try to get it staged this evening. I am happy to take a look. That sounds sorta odd. The only thing have running for clients needs updating as well. N.
Are you sure you didn't find the code here? https://gist.github.com/BGTG-IT/38546134c99c70bfef51 Anyways, it's not really going to do what you want as it seems to be applying this by dollar amount and not percentage. Have you tried removing this code to see if that helps? Anyways, it might work if you do this although I don't like the code. And you should probably make this specific to your coupon code. <?php add_action('woocommerce_coupon_get_discount_amount', 'cart_level_coupon_variant_logic',10,5); function cart_level_coupon_variant_logic($discount, $discounting_amount, $cart_item, $single, $coupon) { global $coupon_qty_stash; if ($coupon->limit_usage_to_x_items != '' && $discount > 0) { $qty = $cart_item['quantity']; if ($qty == 0) { return $discount; } if (!isset($GLOBALS['coupon_qty_stash'])) { $GLOBALS['coupon_qty_stash'] = $coupon->limit_usage_to_x_items - ($qty + $coupon_qty_stash); } else { if ($coupon_qty_stash <= 0) { return 0; } // what's qty is left in coupon code stash? $diff = $coupon_qty_stash - $qty; if ($diff < 0) { return 0; } } } return $discount; } PHP:
Markus: yes that looks like the code I originally used (I don't like it either) I'm going to play with it to see if maybe I messed something up.
Markus, what you put in seems to be working at the moment, I'm still testing a couple other options... I wish there was a more elegant way to do it. Also, i notice when I first apply the code it doesn't actually show the discount amount as a line item (which it should) but then when you move to the cart it seems to work out fine... I guess I really don't understand exactly how this filter works with woocommerce.
oh sorry, that problem might be fixed with this <?php add_action('woocommerce_coupon_get_discount_amount', 'cart_level_coupon_variant_logic',10,5); function cart_level_coupon_variant_logic($discount, $discounting_amount, $cart_item, $single, $coupon) { global $coupon_qty_stash; if ($coupon->limit_usage_to_x_items != '' && $discount > 0) { $qty = $cart_item['quantity']; if ($qty == 0) { return $discount; } if (!isset($GLOBALS['coupon_qty_stash'])) { $GLOBALS['coupon_qty_stash'] = $coupon->limit_usage_to_x_items - ($qty + $coupon_qty_stash); } else { if ($coupon_qty_stash <= 0) { return $discount; } // what's qty is left in coupon code stash? $diff = $coupon_qty_stash - $qty; if ($diff < 0) { return 0; } } } return $discount; } PHP: