Fix Wordpress Woocommerce Coupon Logic

Discussion in 'PHP' started by blaineglynn, Nov 5, 2014.

  1. #1
    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.
     
    blaineglynn, Nov 5, 2014 IP
  2. blaineglynn

    blaineglynn Active Member

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #2
    I've got $20 for the first person who can come up with a working solution!
     
    blaineglynn, Nov 5, 2014 IP
  3. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #3
    Does this help? :)

    <?php
    if (trim($coupon->limit_usage_to_x_items) != '' && $discount > 0)
        {
    
    PHP:
     
    Anveto, Nov 5, 2014 IP
  4. blaineglynn

    blaineglynn Active Member

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #4
    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
     
    blaineglynn, Nov 5, 2014 IP
  5. Nigel Lew

    Nigel Lew Notable Member

    Messages:
    4,642
    Likes Received:
    406
    Best Answers:
    21
    Trophy Points:
    295
    #5
    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 Lew, Nov 5, 2014 IP
  6. blaineglynn

    blaineglynn Active Member

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #6
    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...
     
    blaineglynn, Nov 5, 2014 IP
  7. Nigel Lew

    Nigel Lew Notable Member

    Messages:
    4,642
    Likes Received:
    406
    Best Answers:
    21
    Trophy Points:
    295
    #7
    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.
     
    Nigel Lew, Nov 5, 2014 IP
  8. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #8
    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:
     
    Anveto, Nov 5, 2014 IP
  9. blaineglynn

    blaineglynn Active Member

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #9
    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.
     
    blaineglynn, Nov 5, 2014 IP
  10. blaineglynn

    blaineglynn Active Member

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #10
    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.
     
    blaineglynn, Nov 5, 2014 IP
  11. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #11
    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:
     
    Anveto, Nov 5, 2014 IP