Skip to content

Fixed cart discount calculated incorrectly when product first added to cart. #26622

Closed
@aligent-lturner

Description

@aligent-lturner

Preconditions (*)

  1. Magento 2.3.4
  2. PHP 7.1+
  3. Create a cart price rule for a fixed amount for the whole cart (e.g. $1). The rule should have no conditions attached to it (or restrictions on the items it applies to)
  4. Create at least one configurable product with at least one simple product attached to each configurable.
  5. Set the price of each item to be $10 (actual price doesn't matter - just used $10 for example)

Steps to reproduce (*)

  1. Navigate to PDP of configurable product
  2. Select options for the configurable and add to cart

Expected result (*)

  1. Item is added to cart
  2. Cart total is $9 ($10 for item - $1 discount)

Actual result (*)

  1. Item is added to cart
  2. Cart total is $9.50 ($10 for item - $0.50 discount)

Notes

The issue is also present any time a new simple product is added to the cart. For example, if a different configurable option was chosen, the discount would be calculated as $0.67 (then $0.75 for another). The reason for this behaviour is due to the following:

When a configurable is added to the cart, two items actually get added as Quote items - the configurable, and the simple item representing the selected options.
As part of the rule calculation, it skips child (simple) items by checking the result of getParentItemId():

//Skipping child items to avoid double calculations
if ($item->getParentItemId()) {
continue;
}

However, for a newly added item, parentItemId is not set yet, as this is only set in the beforeSave function of the quote item:

public function beforeSave()
{
parent::beforeSave();
if ($this->getParentItem()) {
$this->setParentItemId($this->getParentItem()->getId());
}
return $this;
}

Instead, the Validator class should be checking the result of getParentItem

Metadata

Metadata

Labels

Component: QuoteComponent: SalesRuleFixed in 2.4.xThe issue has been fixed in 2.4-develop branchIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedProgress: PR CreatedIndicates that Pull Request has been created to fix issueReported on 2.3.4Indicates original Magento version for the Issue report.Reproduced on 2.3.xThe issue has been reproduced on latest 2.3 releaseReproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branch

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions