1

Registering like so and all is well.

Event::on(
   OrderAdjustments::class,
   OrderAdjustments::EVENT_REGISTER_ORDER_ADJUSTERS,
   function(RegisterComponentTypesEvent $event) {
         $event->types[] = Freebie::class;
   }
 );

Per the docs (bolds mine):

Each of those models may be applied to the order or one of its line items.

An adjustment model always belongs to an order, but can optionally belong to a line item.

We've set up according to this [example here][1] which demonstrates looping through line items and adding the adjuster to each item. All good so far.

But how might we add an adjuster to only the order?

If we take the adjustment code out of the loop )as in the example linked above) and leave off the $adjustment->setLineItem($item);, the model does not appear in cart.adjustments nor cart.orderAdjustments

Craft CMS 3.7.50 [PRO]
Commerce 3.4.15 [PRO]


  [1]: https://craftcms.com/docs/commerce/3.x/extend/adjusters.html#designating-an-order-and-line-item

1 Answer 1

1

Can you try this?

use craft\commerce\models\OrderAdjustment;

$adjustment = new OrderAdjustment();

$adjustment->setOrder($order); // $order - craft\commerce\elements\Order;
4
  • yeah no go. pretty sure type is also required, but yeah bare minimum: setOrder and type still does not add the adjustment to the order only.
    – Steve Mock
    Commented Oct 20, 2022 at 14:39
  • @SteveMock That's weird You have at the very beginning: $adjustments = []; ? And in the end: $adjustments[] = $adjustment; return $adjustments;
    – RomanAvr
    Commented Oct 20, 2022 at 15:05
  • that's right. the class fires update successfully. would it matter that this is a [sprig][putyourlightson.com/plugins/sprig] cart update?
    – Steve Mock
    Commented Oct 20, 2022 at 18:08
  • I guess that yes
    – RomanAvr
    Commented Oct 21, 2022 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.