magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.56k stars 9.32k forks source link

Coupon is applied properly on cart but not on checkout #38746

Closed ioweb-gr closed 5 months ago

ioweb-gr commented 5 months ago

Preconditions and environment

Steps to reproduce

  1. Create a cart rule where the user can add two products from different manufactures but is supposed to receive a discount only on one
  2. The rule uses a COUPON CODE
  3. Example conditions image
  4. Example actions image
  5. Try to apply the coupon on cart and it works image
  6. Try to go to checkout and apply it image

Expected result

The coupon is properly applied

Actual result

The coupon is not applied

Additional information

No response

Release note

No response

Triage and priority

m2-assistant[bot] commented 5 months ago

Hi @ioweb-gr. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

ioweb-gr commented 5 months ago

@magento give me 2.4.6-p4 instance

magento-deployment-service[bot] commented 5 months ago

Hi @ioweb-gr. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 5 months ago

Hi @ioweb-gr, unfortunately there is no ability to deploy Magento instance at the moment. Please try again later.

m2-assistant[bot] commented 5 months ago

Hi @engcom-Bravo. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

m2-assistant[bot] commented 5 months ago

Hi @engcom-Dash. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:


engcom-Dash commented 5 months ago

Hi @ioweb-gr

Thanks for reporting and collaboration.

Verified the issue on magento 2.4 dev instance but the issue is not reproducable.

The coupon is applied succesfully on both cart and checkout.

Steps to reproduce:

  1. Create a cart rule where the user can add two products from different manufactures but is supposed to receive a discount only on one
  2. The rule uses a COUPON CODE
  3. Add conditions and actions.
  4. On the UI, add the product with Manufacture with couponcode
  5. Apply coupon code on cart
  6. Apply coupon code on checkout

Please refer the screenrecording.

Uploading 38746-1 (1).mov…

ioweb-gr commented 5 months ago

@engcom-Dash I cannot see the video, the attached screen recording is broken. Can you please show me the cart rule settings? From your description of the problem I can see that the rule isn't exactly what I've shown. The product needs to also belong to a specific category. Please confirm that the rules are correct.

I also have a problem deploying an instance for 2.4.6 p4 with magento bot.

engcom-Dash commented 5 months ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 5 months ago

Hi @engcom-Dash. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 5 months ago

Hi @engcom-Dash, here is your Magento Instance: https://c6939cdbaf717e2e390d09bcff2162b3.instances-prod.magento-community.engineering Admin access: https://c6939cdbaf717e2e390d09bcff2162b3.instances-prod.magento-community.engineering/admin_3fae Login: 4986b2ea Password: 1c049f5c53bb

engcom-Dash commented 5 months ago

Hi @ioweb-gr

Verified the issue again in magneto 2.4 dev instance. But the issue is not reproducable.

Able to apply the coupon code in both cart and checkout page

The rule has manufacturer and product belonging to a specific category.

Please refer the attached screenshots and recording.

https://github.com/magento/magento2/assets/60198592/dae32933-5328-4d9c-8d08-3f5c81df9b40

ioweb-gr commented 5 months ago

Hi @engcom-Dash ,

you've added two products but they are the same product and the rule is applicable to both of them. You need to add one product where the rule is applicable and another where the rule isn't. So the first product should be with manufacturer M1 and the rule is applied, and the second with manufacturer M2 and the rule isn't valid for this product.

ioweb-gr commented 5 months ago

While trying to debug this I notice it fails here


    /**
     * Validate coupon code.
     *
     * @param \Magento\Quote\Model\Quote $quote
     * @return $this
     */
    protected function _validateCouponCode(\Magento\Quote\Model\Quote $quote)
    {
        $code = $quote->getData('coupon_code');
        if ($code !== null && strlen($code)) {
            $addressHasCoupon = false;
            $addresses = $quote->getAllAddresses();
            if (count($addresses) > 0) {
                foreach ($addresses as $address) {
                    if ($address->hasCouponCode()) {
                        $addressHasCoupon = true;
                    }
                }
                if (!$addressHasCoupon) {
                    $quote->setCouponCode('');
                }
            }
        }
        return $this;
    }

While validating the code it's trying to check both shipping / billing address to verify if the coupon code is set. However, the coupon code is only set in the quote earlier

\Magento\Quote\Model\CouponManagement::set

        try {
            $quote->setCouponCode($couponCode);
            $this->quoteRepository->save($quote->collectTotals());
        } catch (LocalizedException $e) {
            throw new CouldNotSaveException(__('The coupon code couldn\'t be applied: ' .$e->getMessage()), $e);
        } catch (\Exception $e) {
            throw new CouldNotSaveException(
                __("The coupon code couldn't be applied. Verify the coupon code and try again."),
                $e
            );
        }

So basically I'm assuming that the $quote->setCouponCode($couponCode); should also trigger setting the coupon code into the address itself. But it doesn't

Could you point me to where the coupon code should be set in the address to see why it's not being set?

ioweb-gr commented 5 months ago

@magento give me 2.4.7 instance

magento-deployment-service[bot] commented 5 months ago

Hi @ioweb-gr. Thank you for your request. I'm working on Magento instance for you.

ioweb-gr commented 5 months ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 5 months ago

Hi @ioweb-gr. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 5 months ago

Hi @ioweb-gr, here is your Magento Instance: https://7857dcfddb63bdcae5c6a22ded0862ab.instances-prod.magento-community.engineering Admin access: https://7857dcfddb63bdcae5c6a22ded0862ab.instances-prod.magento-community.engineering/admin_7cb0 Login: c1622ff9 Password: 08cf0d872131

magento-deployment-service[bot] commented 5 months ago

Hi @ioweb-gr, here is your Magento Instance: https://c6939cdbaf717e2e390d09bcff2162b3.instances-prod.magento-community.engineering Admin access: https://c6939cdbaf717e2e390d09bcff2162b3.instances-prod.magento-community.engineering/admin_9849 Login: f37e119c Password: c48e2e68f223

ioweb-gr commented 5 months ago

OK we found the culprit. There was a free shipping rule above X amount. When adding the two products, the free shipping rule was activating and preventing subsequent rules processing. Moreover, this was not enforced on cart but only on checkout hence the confusion.

After upgrading to 2.4.7 to test our instance, this difference was eliminated and our coupon was not applying in both cart and checkout which was correct due to the settings on the free shipping rule.

So there is an actual problem in 2.4.6-p4 where this use case causes mismatch but it's solved in 2.4.7.

We can safely close this issue