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.49k stars 9.3k forks source link

Wrong discount calculation when using multiple fixed discount cart rules #32964

Closed tiagosampaio closed 2 weeks ago

tiagosampaio commented 3 years ago

Preconditions (*)

  1. I believe that this happens to any Magento version but I'm using 2.3.7 Commerce Edition currently
  2. Create two products with the base price of $19.00 each. Both products must be available to be purchased in the front store.
  3. Create two cart discount rules
  4. The first cart rule will apply a fixed discount amount of $10.00 to the whole cart. Define a specific coupon code to activate this rule in the shopping cart
  5. The second cart rule will apply a fixed discount amount of $18.00 to only one of the products in the cart. This cart rule must be applied automatically to the customer's cart
  6. Both cart rules must be active

Steps to reproduce (*)

  1. Go to the front store and add the two products in the cart, each one with the price of $19.00 (total $38.00)
  2. Go to the cart
  3. You should be able to see the second rule already applied and a discount of $18.00 must be visible.
  4. In the shopping cart, apply the coupon code to activate the fixed $10.00 discount rule in the cart.
  5. You'll be able to see the total discount applied is $24.00 while it should be $28.00
  6. The cart total is shown as $14.00 but the correct value should be $10.00

Expected result (*)

  1. The discount amount should be $28.00 in total.

Actual result (*)

  1. Only a $24.00 discount is applied.

Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

m2-assistant[bot] commented 3 years ago

Hi @tiagosampaio. Thank you for your report. To help us process this issue please make sure that you provided the following information:

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

Please, add a comment to assign the issue: @magento I am working on this


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket.

:movie_camera: You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

m2-assistant[bot] commented 3 years ago

Hi @engcom-Oscar. 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-Oscar commented 3 years ago

Hi @tiagosampaio ! Thank you for your report. Unfortunately, we are not able to reproduce the issue. May you please update description and add more detailed steps to it, like exact rules configuration? After following steps from the ticket, we got next result:

Screenshot from 2021-05-07 10-51-25

tiagosampaio commented 3 years ago

1 - Create two products with the price of $19 each (SKUs: test-product-one, test-product-two). 2 - Create two Cart Price Rules:

The first rule is applied only to the product test-product-two (filtered in the actions section) Applies a fixed discount of $18 on the product's price.

image

Configuration:

{
"salesrule": [
    {
        "rule_id" : 5,
        "name" : "Test Product Two Discount",
        "description" : "",
        "from_date" : "2021-05-07",
        "to_date" : null,
        "uses_per_customer" : 0,
        "is_active" : 1,
        "conditions_serialized" : "{\"type\":\"Magento\\\\SalesRule\\\\Model\\\\Rule\\\\Condition\\\\Combine\",\"attribute\":null,\"operator\":null,\"value\":true,\"is_value_processed\":null,\"aggregator\":\"all\"}",
        "actions_serialized" : "{\"type\":\"Magento\\\\SalesRule\\\\Model\\\\Rule\\\\Condition\\\\Product\\\\Combine\",\"attribute\":null,\"operator\":null,\"value\":\"1\",\"is_value_processed\":null,\"aggregator\":\"all\",\"conditions\":[{\"type\":\"Magento\\\\SalesRule\\\\Model\\\\Rule\\\\Condition\\\\Product\",\"attribute\":\"sku\",\"operator\":\"==\",\"value\":\"test-product-two\",\"is_value_processed\":false,\"attribute_scope\":\"\"}]}",
        "stop_rules_processing" : 0,
        "is_advanced" : 1,
        "product_ids" : null,
        "sort_order" : 0,
        "simple_action" : "by_fixed",
        "discount_amount" : 18.0000,
        "discount_qty" : null,
        "discount_step" : 0,
        "apply_to_shipping" : 0,
        "times_used" : 0,
        "is_rss" : 1,
        "coupon_type" : 1,
        "use_auto_generation" : 0,
        "uses_per_coupon" : 0,
        "simple_free_shipping" : 0,
        "row_id" : 5,
        "created_in" : 1,
        "updated_in" : 2147483647
    }
]}

The second one applies a fixed discount of $10 to the whole cart.

image image

{
"salesrule": [
    {
        "rule_id" : 6,
        "name" : "Ten Dollars to Whole Cart",
        "description" : "",
        "from_date" : "2021-05-07",
        "to_date" : null,
        "uses_per_customer" : 0,
        "is_active" : 1,
        "conditions_serialized" : "{\"type\":\"Magento\\\\SalesRule\\\\Model\\\\Rule\\\\Condition\\\\Combine\",\"attribute\":null,\"operator\":null,\"value\":true,\"is_value_processed\":null,\"aggregator\":\"all\"}",
        "actions_serialized" : "{\"type\":\"Magento\\\\SalesRule\\\\Model\\\\Rule\\\\Condition\\\\Product\\\\Combine\",\"attribute\":null,\"operator\":null,\"value\":\"1\",\"is_value_processed\":null,\"aggregator\":\"all\"}",
        "stop_rules_processing" : 0,
        "is_advanced" : 1,
        "product_ids" : null,
        "sort_order" : 0,
        "simple_action" : "cart_fixed",
        "discount_amount" : 10.0000,
        "discount_qty" : null,
        "discount_step" : 0,
        "apply_to_shipping" : 0,
        "times_used" : 0,
        "is_rss" : 1,
        "coupon_type" : 2,
        "use_auto_generation" : 0,
        "uses_per_coupon" : 0,
        "simple_free_shipping" : 0,
        "row_id" : 6,
        "created_in" : 1,
        "updated_in" : 2147483647
    }
]}

3 - With all set then go to the storefront and add test-product-one and test-product-two to the cart. 4 - Go to the shopping cart page and you'll be able to see the discount of -$18.00 already applied because test-product-two is in the cart.

image

5 - Apply the coupon code to the cart 6 - Check the discount amount now

image

The version I used for this testing is Magento Commerce 2.4.2-p1

tiagosampaio commented 3 years ago

@engcom-Oscar any update on this?

engcom-Oscar commented 3 years ago

@tiagosampaio Thank you for your reply, with more detailed steps I was able to reproduce this issue and confirmed it.

engcom-Foxtrot commented 3 years ago

The issue is caused by calculation in \Magento\SalesRule\Helper\CartFixedDiscount::getDiscountAmount, there the whole cart price discount is divided between all cart items. So, for the first cart item, total price is 19 - (10 / 2) = $14, and for the second one 19 - (10 / 2) - 18 = -4, that becames $0. Finally, the cart total is 14 + 0 = $14.

engcom-Oscar commented 3 years ago

After investigating this issue, we found out that problem caused by wrong sequence of applying cart price rules.

Preconditions (*)

  1. Magento version 2.4-develop
  2. Create two products with the base price of $19.00 each. Both products must be available to be purchased in the front store.
  3. Create first cart rule with fixed discount amount of $10.00 to the whole cart. Define a specific coupon code to activate this rule in the shopping cart
  4. Create second cart rule with fixed discount amount of $18.00 to only one of the products in the cart. This cart rule must be applied automatically to the customer's cart

Steps to reproduce (*)

  1. Go to the front store and add two created products to the cart, each one with the price of $19.00 (total $38.00)
  2. Go to the cart page
  3. Apply the coupon code to activate the fixed $10.00 discount rule in the cart.

Actual result (*)

Primarily discount calculating by second rule ($10 for whole cart by coupon) and it using formula sum of discount divided on qty of order items: $10 / 2 = $5 on each product. This make price of both product $19 - $5 = $14, and subtotal of the cart = $28. After this first cart rule applying (minus $18 for one of the product), but because our product price only $14, so discount is only $14, and product's price became 0. Therefore only $24 discount is applied, and the cart total is shown as $14.00.

Expected result (*)

Primarily discount calculating should be by first cart rule (minus $18 for one of the product), to make product price $19 - $18 = $1, cart total = $20. Then second rule should be applying correctly ($10 for whole cart by coupon), to make cart total $10.00. Total discount amount should be $28.00

artskirk commented 2 years ago

@magento I am working on this

engcom-Bravo commented 1 month ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 1 month ago

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

magento-deployment-service[bot] commented 1 month ago

Hi @engcom-Bravo, here is your Magento Instance: https://c0a44b9caa363e941ccaf50aad1a6c7d.instances-prod.magento-community.engineering Admin access: https://c0a44b9caa363e941ccaf50aad1a6c7d.instances-prod.magento-community.engineering/admin_20d8 Login: 6a8562de Password: c8629bbcf037

engcom-Delta commented 2 weeks ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 2 weeks ago

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

magento-deployment-service[bot] commented 2 weeks ago

Hi @engcom-Delta, here is your Magento Instance: https://c0a44b9caa363e941ccaf50aad1a6c7d.instances-prod.magento-community.engineering Admin access: https://c0a44b9caa363e941ccaf50aad1a6c7d.instances-prod.magento-community.engineering/admin_da8d Login: 19d98d99 Password: 3c1428b69d2e

engcom-Delta commented 2 weeks ago

Hi @tiagosampaio ,

Thank you for reporting and collaboration. Verified the issue on Magento 2.4-develop and the issue is not reproducible. Kindly refer the screenshots.

Steps to reproduce -

  1. Create two products with $19 as price
  2. Create two Cart Price Rules: The first rule is applied only to the product product2 (filtered in the actions section) Applies a fixed discount of $18 on the product's price. image
  3. The second one applies a fixed discount of $10 to the whole cart.
  4. With all set then go to the storefront and add product1 and product2 to the cart.
  5. Go to the shopping cart page and you'll be able to see the discount of -$18.00 already applied because product2 is in the cart.
  6. Apply the coupon code to the cart
  7. Check the discount amount now
  8. Discount is displaying expected as $28 image Kindly recheck the issue on latest Magento 2.4-develop instance and Please let us know if you are still facing any issue. Thanks.
tiagosampaio commented 1 week ago

@engcom-Delta, this still happens in version 2.4.7-p1.

I have the following cart rules created:

image

Fixed $10.00 to the whole cart

image

Fixed $18.00 to a particular category

image

When I go to the cart page and add the products to my cart I have the following result:

image

The total discount applied to my cart should be $28.00, not $23.19.

The Testing Product #2 should receive a fixed $18.00 discount amount, because it belongs to category #4, and the $10.00 should be applied to the whole cart after adding the MY10 discount coupon, according to my rule.

image

Thanks.