public function enrich(StructCollection $data, Cart $cart, SalesChannelContext $context, CartBehavior $behavior): void
    {
        /** @var array $promotionLineItems */
        $promotionLineItems = $this->getPromotionLineItems($cart);

        // first thing is, to remove all promotion placeholder items.
        // the real line items will be added later in here
        //  we only touch new ones, which means, we have to remove their placeholders (only)
        /** @var LineItem $lineItem */
        foreach ($promotionLineItems as $lineItem) {
            if ($this->isPromotionPlaceholder($lineItem)) {
                $cart->getLineItems()->removeElement($lineItem);
            }
        }

        if (!$behavior->isRecalculation()) {
            // we are in "live checkout mode", which means, we also have to ensure
            // that promotions get removed again if the condition isn't met anymore.
            // this could be due to removing a minimum quantity of a product, or anything else.
            // so we remove every item and re-add our valid promotions from the "collect" step again.
            /** @var LineItem $lineItem */
            foreach ($promotionLineItems as $lineItem) {
                if ($this->isRealPromotionItem($lineItem)) {
                    $cart->getLineItems()->removeElement($lineItem);
                }
            }
        }

        // do not add new promotions if we didn't collect data!
        // we have already cleaned up our cart depending on our recalculation mode above.
        // if we didn't collect anything, this is the place to stop enrichment
        if (!$data->has(self::DATA_KEY) && !$data->get(self::DATA_KEY) instanceof CartPromotionsDataDefinition) {
            return;
        }

        /** @var CartPromotionsDataDefinition $promotionData */
        $promotionData = $data->get(self::DATA_KEY);

        // now add all collected and
        // valid promotions to the cart
        /** @var PromotionEntity $promotion */
        foreach ($promotionData->getPromotions() as $promotion) {
            // lets build separate line items for each
            // of the available discounts within the current promotion
            /** @var array $lineItems */
            $lineItems = $this->buildDiscountLineItems($promotion, $cart, $context);

            // ...and finally add our new line items to the cart
            $cart->addLineItems(new LineItemCollection($lineItems));
        }
    }
public function enrich(StructCollection $data, Cart $cart, SalesChannelContext $context, CartBehavior $behavior): void
    {
        /** @var array $promotionLineItems */
        $promotionLineItems = $this->getPromotionLineItems($cart);

        // first thing is, to remove all promotion placeholder items.
        // the real line items will be added later in here
        //  we only touch new ones, which means, we have to remove their placeholders (only)
        /** @var LineItem $lineItem */
        foreach ($promotionLineItems as $lineItem) {
            if ($this->isPromotionPlaceholder($lineItem)) {
                $cart->getLineItems()->removeElement($lineItem);
            }
        }

        if (!$behavior->isRecalculation()) {
            // we are in "live checkout mode", which means, we also have to ensure
            // that promotions get removed again if the condition isn't met anymore.
            // this could be due to removing a minimum quantity of a product, or anything else.
            // so we remove every item and re-add our valid promotions from the "collect" step again.
            /** @var LineItem $lineItem */
            foreach ($promotionLineItems as $lineItem) {
                if ($this->isRealPromotionItem($lineItem)) {
                    $cart->getLineItems()->removeElement($lineItem);
                }
            }
        }

        // do not add new promotions if we didn't collect data!
        // we have already cleaned up our cart depending on our recalculation mode above.
        // if we didn't collect anything, this is the place to stop enrichment
        if (!$data->has(self::DATA_KEY) && !$data->get(self::DATA_KEY) instanceof CartPromotionsDataDefinition) {
            return;
        }

        /** @var CartPromotionsDataDefinition $promotionData */
        $promotionData = $data->get(self::DATA_KEY);

        // now add all collected and
        // valid promotions to the cart
        /** @var PromotionEntity $promotion */
        foreach ($promotionData->getPromotions() as $promotion) {
            // lets build separate line items for each
            // of the available discounts within the current promotion
            /** @var array $lineItems */
            $lineItems = $this->buildDiscountLineItems($promotion, $cart, $context);

            // ...and finally add our new line items to the cart
            $cart->addLineItems(new LineItemCollection($lineItems));
        }
    }
public function enrich ( StructCollection $ data , Cart $ cart , SalesChannelContext $ context , CartBehavior $ behavior ) : void { /** @var array $promotionLineItems */ $ promotionLineItems = $ this -> getPromotionLineItems ( $ cart ) ; // first thing is, to remove all promotion placeholder items. // the real line items will be added later in here //  we only touch new ones, which means, we have to remove their placeholders (only) /** @var LineItem $lineItem */ foreach ( $ promotionLineItems as $ lineItem ) { if ( $ this -> isPromotionPlaceholder ( $ lineItem ) ) { $ cart -> getLineItems ( ) -> removeElement ( $ lineItem ) ; } } if ( ! $ behavior -> isRecalculation ( ) ) { // we are in "live checkout mode", which means, we also have to ensure // that promotions get removed again if the condition isn't met anymore. // this could be due to removing a minimum quantity of a product, or anything else. // so we remove every item and re-add our valid promotions from the "collect" step again. /** @var LineItem $lineItem */ foreach ( $ promotionLineItems as $ lineItem ) { if ( $ this -> isRealPromotionItem ( $ lineItem ) ) { $ cart -> getLineItems ( ) -> removeElement ( $ lineItem ) ; } } } // do not add new promotions if we didn't collect data! // we have already cleaned up our cart depending on our recalculation mode above. // if we didn't collect anything, this is the place to stop enrichment if ( ! $ data -> has ( self :: DATA_KEY ) && ! $ data -> get ( self :: DATA_KEY ) instanceof CartPromotionsDataDefinition ) { return ; } /** @var CartPromotionsDataDefinition $promotionData */ $ promotionData = $ data -> get ( self :: DATA_KEY ) ; // now add all collected and // valid promotions to the cart /** @var PromotionEntity $promotion */ foreach ( $ promotionData -> getPromotions ( ) as $ promotion ) { // lets build separate line items for each // of the available discounts within the current promotion /** @var array $lineItems */ $ lineItems = $ this -> buildDiscountLineItems ( $ promotion , $ cart , $ context ) ; // ...and finally add our new line items to the cart $ cart -> addLineItems ( new LineItemCollection ( $ lineItems ) ) ; } }
This function enriches the cart with custom data that has been collected in our previous function.
All collected promotions will now be converted into real Promotion Line Items.
Placeholder items will be removed accordingly.
If we are in "live checkout mode", then we also remove existing promotion line items,
that might not be valid anymore.

@throws InvalidPayloadException
@throws InvalidQuantityException
@throws MixedLineItemTypeException
This function enriches the cart with custom data that has been collected in our previous function . All collected promotions will now be converted into real Promotion Line Items . Placeholder items will be removed accordingly . If we are in live checkout mode then we also remove existing promotion line items that might not be valid anymore .