Using order_percentage_off the order doesn't return adjustments

Created on 26 November 2024, 26 days ago

Problem/Motivation

To export orders to ERP we need to extract the promotions from the order.

There are two methods available for the adjustments

/**
   * Collects all adjustments that belong to the order.
   *
   * Unlike getAdjustments() which returns only order adjustments, this
   * method returns both order and order item adjustments.
   *
   * Important:
   * The returned adjustments are unprocessed, and must be processed before use.
   *
   * @param string[] $adjustment_types
   *   The adjustment types to include.
   *   Examples: fee, promotion, tax. Defaults to all adjustment types.
   *
   * @return \Drupal\commerce_order\Adjustment[]
   *   The adjustments.
   *
   * @see \Drupal\commerce_order\AdjustmentTransformerInterface::processAdjustments()
   */
  public function collectAdjustments(array $adjustment_types = []);
/**
   * Gets the adjustments.
   *
   * @param string[] $adjustment_types
   *   The adjustment types to include.
   *   Examples: fee, promotion, tax. Defaults to all adjustment types.
   *
   * @return \Drupal\commerce_order\Adjustment[]
   *   The adjustments.
   */
  public function getAdjustments(array $adjustment_types = []);

My expectations would be:
If I need to retrieve the order total promotion adjustments only, I should use:
$orderPromotionAdjustments = $order->getAdjustments(['promotion']);

If I need to retrieve all adjustments, including the order item adjustments, I should use:
$orderAndOrderItemsPromotionAdjustments = $order->collectAdjustments(['promotion']);

And I think it's important to separate them, so they are not applied twice (in export) for cases like order_item_percentage_off?

But $order->getAdjustments(['promotion']); never returns any promotions (while $order->collectAdjustments(['promotion']); returns all promotions as expected)

Steps to reproduce

See above

Proposed resolution

Find out why $order->getAdjustments(['promotion']); does not return the order promotions, in contrast to the function documentation.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

3.0

Component

Order

Created by

🇩🇪Germany Anybody Porta Westfalica

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Anybody
  • 🇩🇪Germany Anybody Porta Westfalica
  • 🇩🇪Germany Anybody Porta Westfalica
  • 🇮🇱Israel jsacksick

    All promotions are applied / distributed to order items. There is no order level promotion per say. $order->getAdjustments() would return order level adjustments (so none in case of promotions). The main difference lies in the fact that the adjustment is included or not...

  • 🇩🇪Germany Anybody Porta Westfalica

    Thank you very much for the immediate feedback @jsacksick - and sorry, from the naming and method description I would have expected that differently.

    To help others: So you always need to use
    $order->collectAdjustments(['promotion']);
    as
    $order->getAdjustments(['promotion']); will never return a result by design.

    Afterwards you can use
    $promitionAdjustment->isIncluded() to check, if the adjustment is already included, for the cases described above.

    Right?

    Thank you once again!!

  • 🇮🇱Israel jsacksick

    Either that or you could load the promotion using the adjustment source ID, and then do different logic based on the promotion offer plugin.

  • 🇩🇪Germany Anybody Porta Westfalica

    Either that or you could load the promotion using the adjustment source ID, and then do different logic based on the promotion offer plugin.

    Thanks yes - but isn't using the adjustment source ID always risky, as the referred promotion may have changed in the meantime? (e.g. someone changed the discount from 10% to 5%)

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024