Magento2: Promo code bug - Promotion still applies after updating the cart to no longer be valid with the promotion.

Created on 29 Jun 2016  路  18Comments  路  Source: magento/magento2

Hi, I have seen an issue using promo codes created via the admin area. If you create a promo code which is only applicable to a minimum value in the cart and apply the promotion, if I then remove item quantities to below the promotion value limit, the promotion is still applied.

Steps to reproduce

Step 1) Create a promotion code (order value above 5k get 5% off) via admin
Step 2) Promotion code to be used: DEMO2
Step 3) Add products or multiple of a product to the cart over 5k worth.
Step 4) Go to basket and apply promotion code: DEMO2
Step 5) Promotion applied and discount of 5% given
Step 6) Remove/Reduce the qty of products to get the price below 5k
Step 7) Order value is now less than 5k but customer still gets discount

Expected result

Expected the promo code to be removed with a message about it no longer being valid

Actual result

Discount still applies.

Tax Cannot Reproduce Clear Description Format is not valid bug report

Most helpful comment

+1, I can confirm the same bug as @jameshalsall. For my part I'm developing a custom shipping method extension and I'm checking for free shipping in the collectRates(RateRequest $request) method of my shipping carrier model. The $request argument has a getFreeShipping() method and, if a free shipping coupon has been applied once, it will always return true even after the coupon has been cancelled.

You don't even need complex coupon rules to reproduce it, just try this :

  1. Create a free shipping coupon with no specific conditions (Actions -> Apply to shipping amount = Yes, Actions -> Free Shipping = For shipment with matching items)
  2. Add an item to cart and apply the coupon. At this point, $request->getFreeShipping() correctly returns true.
  3. Cancel the coupon. At this point, $request->getFreeShipping() still returns true which is incorrect.

All 18 comments

Which Magento version do you use ?

please post your Cart Price Rules

Magento ver. 2.0.7 Enterprise

screen shot 2016-06-29 at 13 36 31

screen shot 2016-06-29 at 13 37 13
screen shot 2016-06-29 at 13 37 46

I have been toying with a fix, but not been able to get a complete fix for this yet.
For now I was looking to add some logic to https://github.com/magento/magento2/blob/develop/app/code/Magento/Checkout/Controller/Cart/UpdatePost.php#L50

by overwriting the controller and adding some logic after line 50 to remove the promotion like this:

 //check promo code
                $cartQuote = $this->cart->getQuote();
                $itemsCount = $cartQuote->getItemsCount();

                if ($itemsCount) {
                    $couponCode = $cartQuote->getCouponCode();
                    if ($couponCode) { //has code
                        $quoteRepository = $this->_objectManager->get('Magento\Quote\Api\CartRepositoryInterface');
                        $couponManagement = $this->_objectManager->get('Magento\Quote\Model\CouponManagement');
                        $escaper = $this->_objectManager->get('Magento\Framework\Escaper');

                        $cartQuote->getShippingAddress()->setCollectShippingRates(true);
                        $cartQuote->setCouponCode('')->collectTotals();
                        $quoteRepository->save($cartQuote);
                        $couponManagement->remove($this->cart->getQuote()->getId());
                        $this->cart->save();

                        $this->messageManager->addError(
                            __(
                                'The coupon code "%1" has been removed because of updates to your cart.  Please input the discount code again if you believe it still applies.',
                                $escaper->escapeHtml($couponCode)
                            )
                        );

                    }

                }

I don't know enough about how the Cart Price Rules work to provide a clean solution. Copying how the promo codes are added did not seem to perform the same checks as doing the same logic in this controller.

I can't reproduce your issue.

I created the same cart rule as yours & it works like a charm

So what happens when the cart no longer meets the promo criteria? Does it show a message or just remove the promo code?

just remove the promo code

We are experiencing the same issue. Has anyone made progress on this?

I am not sure if this was ever 'fixed' via this bug report, but after a Magento 2 update (2.1.x) I believe QA told us this was fixed. I am quite sure that the temp fix above I included also worked.

This is indeed still an issue in 2.1.2.

For us we have a promo code that gives free shipping for specific products. Apply the promo code when the basket contains a valid product for that coupon, and then remove the product. If you look at the quote table's free_shipping value it remains as 1 instead of being set back to 0.

+1, I can confirm the same bug as @jameshalsall. For my part I'm developing a custom shipping method extension and I'm checking for free shipping in the collectRates(RateRequest $request) method of my shipping carrier model. The $request argument has a getFreeShipping() method and, if a free shipping coupon has been applied once, it will always return true even after the coupon has been cancelled.

You don't even need complex coupon rules to reproduce it, just try this :

  1. Create a free shipping coupon with no specific conditions (Actions -> Apply to shipping amount = Yes, Actions -> Free Shipping = For shipment with matching items)
  2. Add an item to cart and apply the coupon. At this point, $request->getFreeShipping() correctly returns true.
  3. Cancel the coupon. At this point, $request->getFreeShipping() still returns true which is incorrect.

@carlowens @Dayssam @Fairlight60 thank you for reporting and investigating the issue. Is it reproducible for you on the latest develop branch?
I tried latest develop and everything worked good for me. On step 6 I decreased amount of products in cart and discount disappeared on page reloading, coupon was unapplied.
Does it work the same for you?

Hello,
I have the same issue at 2.1.2

What I did to fix it temporary:
I added this lines

$address = $this->cart->getQuote()->getShippingAddress();
$address = $address->setFreeShipping(false)->save();

Into \Magento\Checkout\Controller\Cart\Delete::execute()
and \Magento\Checkout\Controller\Cart\UpdatePost::execute()

It forces Magento to refresh the shipping method to free shipping "false".
After that, Magento set the shipping method conveniently.

It can be done by plugin or pre-dispatch/post-dispatch event.

I hope it will be fixed in next release.

@srbarba to fix and make sure it works we need steps to reproduce. Could you please provide the steps?

Same issue on 2.1.3

Found the same thing, the problem seems to be with 3rd party shipping modules like Amasty, so I'm guessing that's why they still have not sorted it out. The fix I made was to add the following code to the execute() function of the following 2 files. This clears the freeshipping flag when they go to the cart of checkout pages. Thanks to @srbarba for the pointer.

vendor\magento\module-checkout\Controller\Cart\Index.php

public function execute()
    {
        // THELETTUCE FIX FOR COUPON CODE FREE SHIPPING
        $address = $this->cart->getQuote()->getShippingAddress();
        $address = $address->setFreeShipping(false)->save();

vendor\magento\module-checkout\Controller\Index\Index.php

public function execute()
    {
        // THELETTUCE FIX FOR COUPON CODE FREE SHIPPING
        $address = $this->getOnepage()->getQuote()->getShippingAddress();
        $address = $address->setFreeShipping(false)->save();

@carlowens, thank you for your report.
We were not able to reproduce this issue by following the steps you provided. If you'd like to update it, please reopen the issue.
We tested the issue on 2.1.9

I confirm the issue with Magento 2.1.7 EE. Using third party extension like Owebia for the shipment, the shipment request has the free_shipping value to 1. It's due to quote_address.free_shipping value which stay at 1 even when you remove the coupon. Solution provided by @srbarba helped. I used a afterExecute plugin for that.

I didn't tested yet on newest version but I will give a try on 2.2 as we plan to migrate to this version.

Was this page helpful?
0 / 5 - 0 ratings