VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Topic started by: Kuubs on March 01, 2024, 12:18:49 PM

Title: Coupon code disallowed categories is very buggy: FIXED IT< FIXED CORE CODE
Post by: Kuubs on March 01, 2024, 12:18:49 PM
Hello,

Am i the only one around here that finds the allowed categories for the coupons very buggy? When I set certain categories most of the time it just refused to apply coupon codes for a specific product in the allowed category.. Anyone know why this happens?

Where is this code located, maybe I can take a look and see if I can make some changes to it...

EDIT:

I found the solution and the culprit.

The problem is this method in the calculationh.php helper file in administrator

protected function couponHandler($_code)

There is this peice of code:

for($i = 0; $i < $sizeof_cartitems_by_product; $i++){
            if( (!empty($allowed_product_ids) and in_array($this->_cart->cartPrices[$i]['virtuemart_product_id'],$allowed_product_ids)) || (!empty($allowed_productcat_ids) and array_intersect($this->_cart->products[$i]->categories, $allowed_productcat_ids))){
               $coupon_discount += ($this->_cart->cartPrices[$i]['salesPriceTt'] * ($_data->coupon_value / 100));
            }
         }

this is pretty buggy because the cart doesn't always start at the 0 index.. So i changed this code to this:

foreach ($this->_cart->products as $i => $product) {
            if ((!empty($allowed_product_ids) && in_array($this->_cart->cartPrices[$i]['virtuemart_product_id'], $allowed_product_ids)) || (!empty($allowed_productcat_ids) && array_intersect($product->categories, $allowed_productcat_ids))) {
               $coupon_discount += ($this->_cart->cartPrices[$i]['salesPriceTt'] * ($_data->coupon_value / 100));
            }
         }

Now it works beautifully! I will post this as a code change to be implemented in the core code
Title: Re: Coupon code disallowed categories is very buggy: FIXED IT< FIXED CORE CODE
Post by: Alexb65 on March 08, 2024, 10:52:56 AM
Nice shot!
Title: Re: Coupon code disallowed categories is very buggy: FIXED IT< FIXED CORE CODE
Post by: Milbo on March 19, 2024, 21:39:52 PM
well done,
Funny, I did not see this post, but looks we have almost the same solution
https://dev.virtuemart.net/projects/virtuemart/repository/virtuemart/revisions/10981/diff/trunk/virtuemart/administrator/components/com_virtuemart/helpers/calculationh.php