Multiple BuyXGetY promotions with the same gift product are not applying correctly

When the second promotion retrieves the order item created by the first promotion using the findOrCreateOrderItem method, it comes with an outdated order.

It seems that the `buildAdjustmentAmount` method does not correctly calculate the unit price in the second promotion when the promotion is included.

Steps to reproduce it

Please review the failing test for further investigation.

CommentFileSizeAuthor
order_item.png89.66 KBfacine
order.png92.9 KBfacine

Issue fork commerce-3474801

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

facine created an issue. See original summary.

facine’s picture

Title: Multiple BuyXGetY promotions with the same gift product are not applying correctly » Multiple (display_inclusive) BuyXGetY promotions with the same gift product are not applying correctly
facine’s picture

Issue summary: View changes
facine’s picture

It seems that the unit price is reset the second time the promotion is applied due to the issue at this line: https://git.drupalcode.org/project/commerce/-/blob/3.0.x/modules/promoti....

For now, as a workaround, I recalculate the unit price in the `buildAdjustmentAmount` method before calculating the new adjustment.

  protected function buildAdjustmentAmount(OrderItemInterface $order_item, $quantity) {
    if ($this->configuration['offer_type'] == 'percentage') {
      $percentage = (string) $this->configuration['offer_percentage'];
      $adjusted_total_price = $order_item->getTotalPrice();
      foreach ($order_item->getAdjustments(['promotion']) as $adjustment) {
        if ($adjustment->isIncluded()) {
          $adjusted_total_price = $adjusted_total_price->add($adjustment->getAmount());
        }
      }
jsacksick’s picture

Priority: Critical » Normal

I don't consider this a "critical" bug report TBH (per https://www.drupal.org/docs/develop/issues/fields-and-other-parts-of-an-...).