Coupon code not working properly if allowed categories is set

Started by Kuubs, November 10, 2025, 12:41:35 PM

Previous topic - Next topic

Kuubs

I have an issue with the coupon codes. When I add 2 products, add the coupon code, it works great, but the moment you remove the first product from the cart, the code is not being added anymore. Problem is the helper file of the cart.php. On the setCouponCode it works with indexes but when you remove the first product the index is not 0 but 1 for the first product. That needs to be rewritten.

There is a bug with coupon codes. When you use allowed categories, add 2 products to the cart within these 2 categories and then remove the first product (very important to remove the first product) from the cart, teh coupon will not be applied properly. This is because the helper file that sets teh coupon is bugged, see setCouponCode()
This is a bug that is almost as old as 4.2.2 , but on the newer versions the bug is still here. The helper cart file is changed, but it didnt solve the issue

lalolyshop

Same problem : Coupon code not operate since upadate.
I just created a discount code in a category. When items are added to the cart and the coupon is added, the popup bar spins indefinitely. The Chrome console shows a 303 error (looping redirect). I disabled admintools just in case, but the problem persists.

Kuubs

I made a small plugin that fixes this issue. I added this to my ajax cart plugin so thats why its called the way it is.

plugins/system/vmcartajax

vmcartajax.php
<?php
defined
('_JEXEC') or die;

use 
Joomla\CMS\Factory;
use 
Joomla\CMS\Plugin\CMSPlugin;

class 
PlgSystemVmCartAjax extends CMSPlugin
{

    public function 
__construct(&$subject$config = array())
    {
        
parent::__construct($subject$config);

        if (
version_compare(JVERSION'4.0.0''ge') && method_exists($this'registerLegacyListener')) {
            
$this->registerLegacyListener('plgVmOnUpdateCart');
        }
    }

    public function 
plgVmOnUpdateCart(&$cart, &$force, &$html)
    {
        
// Reindex cart products to ensure sequential keys starting from 0
        
if (isset($cart->products) && is_array($cart->products)) {
            
$cart->products array_values($cart->products);
        }
    }

    public function 
onAfterInitialise()
    {
       
    }

    


}

vmcartajax.xml
<?xml version="1.0" encoding="UTF-8"?>
<extension version="3.9" type="plugin" group="system" method="upgrade">
    <name>VirtueMart Ajax Cart / Reindexer plugin</name>
    <author>YUZI</author>
    <creationDate>2025-10-28</creationDate>
    <copyright>(C) 2025 yuzi. All rights reserved.</copyright>
    <license>GNU General Public License version 3 or later</license>
    <authorEmail>info@yuzi.nl</authorEmail>
    <authorUrl>www.yuzi.nl</authorUrl>
    <version>1.0.0</version>
    <description>Gets Ajax Cart and reindexes cart contents.</description>
   
    <files>
        <filename plugin="vmcartajax">vmcartajax.php</filename>
    </files>
   
    <config>
       
    </config>
</extension>