private function buildDiscountLineItems(PromotionEntity $promotion, Cart $cart, SalesChannelContext $context): array
    {
        /** @var PromotionDiscountCollection|null $collection */
        $collection = $promotion->getDiscounts();

        if (!$collection instanceof PromotionDiscountCollection) {
            return [];
        }

        $lineItems = [];

        /** @var PromotionDiscountEntity $discount */
        foreach ($collection->getElements() as $discount) {
            // skip if already added! we do not update existing items!
            // depending on our recalculation mode, all promotion items have been removed anyway by now.
            // in recalculation mode, we only add NEW items...and not edit existing ones!
            if ($cart->has($discount->getId())) {
                continue;
            }

            $itemIDs = [];

            // check what type of discount we have.
            // with this, we know how we have to apply our discount.
            // either add discount line items with filter rules
            // or reduce shipping costs, and so on...
            switch ($discount->getScope()) {
                case PromotionDiscountEntity::SCOPE_CART:
                    $itemIDs = $this->itemCollector->getAllLineItemIDs($cart);
                    break;
            }

            // add a new discount line item for this discount
            // if we have at least one valid item that will be discounted.
            if (count($itemIDs) > 0) {
                /* @var LineItem $discountItem */
                $discountItem = $this->itemBuilder->buildDiscountLineItem(
                    $promotion,
                    $discount,
                    $context->getContext()->getCurrencyPrecision()
                );

                $lineItems[] = $discountItem;
            }
        }

        return $lineItems;
    }
private function buildDiscountLineItems(PromotionEntity $promotion, Cart $cart, SalesChannelContext $context): array
    {
        /** @var PromotionDiscountCollection|null $collection */
        $collection = $promotion->getDiscounts();

        if (!$collection instanceof PromotionDiscountCollection) {
            return [];
        }

        $lineItems = [];

        /** @var PromotionDiscountEntity $discount */
        foreach ($collection->getElements() as $discount) {
            // skip if already added! we do not update existing items!
            // depending on our recalculation mode, all promotion items have been removed anyway by now.
            // in recalculation mode, we only add NEW items...and not edit existing ones!
            if ($cart->has($discount->getId())) {
                continue;
            }

            $itemIDs = [];

            // check what type of discount we have.
            // with this, we know how we have to apply our discount.
            // either add discount line items with filter rules
            // or reduce shipping costs, and so on...
            switch ($discount->getScope()) {
                case PromotionDiscountEntity::SCOPE_CART:
                    $itemIDs = $this->itemCollector->getAllLineItemIDs($cart);
                    break;
            }

            // add a new discount line item for this discount
            // if we have at least one valid item that will be discounted.
            if (count($itemIDs) > 0) {
                /* @var LineItem $discountItem */
                $discountItem = $this->itemBuilder->buildDiscountLineItem(
                    $promotion,
                    $discount,
                    $context->getContext()->getCurrencyPrecision()
                );

                $lineItems[] = $discountItem;
            }
        }

        return $lineItems;
    }
private function buildDiscountLineItems ( PromotionEntity $ promotion , Cart $ cart , SalesChannelContext $ context ) : array { /** @var PromotionDiscountCollection|null $collection */ $ collection = $ promotion -> getDiscounts ( ) ; if ( ! $ collection instanceof PromotionDiscountCollection ) { return [ ] ; } $ lineItems = [ ] ; /** @var PromotionDiscountEntity $discount */ foreach ( $ collection -> getElements ( ) as $ discount ) { // skip if already added! we do not update existing items! // depending on our recalculation mode, all promotion items have been removed anyway by now. // in recalculation mode, we only add NEW items...and not edit existing ones! if ( $ cart -> has ( $ discount -> getId ( ) ) ) { continue ; } $ itemIDs = [ ] ; // check what type of discount we have. // with this, we know how we have to apply our discount. // either add discount line items with filter rules // or reduce shipping costs, and so on... switch ( $ discount -> getScope ( ) ) { case PromotionDiscountEntity :: SCOPE_CART : $ itemIDs = $ this -> itemCollector -> getAllLineItemIDs ( $ cart ) ; break ; } // add a new discount line item for this discount // if we have at least one valid item that will be discounted. if ( count ( $ itemIDs ) > 0 ) { /* @var LineItem $discountItem */ $ discountItem = $ this -> itemBuilder -> buildDiscountLineItem ( $ promotion , $ discount , $ context -> getContext ( ) -> getCurrencyPrecision ( ) ) ; $ lineItems [ ] = $ discountItem ; } } return $ lineItems ; }
This function builds separate line items for each of the
available discounts within the provided promotion.
Every item will be built with a corresponding price definition based
on the configuration of a discount entity.
The resulting list of line items will then be returned and can
be added to the cart.
The function will already avoid duplicate entries.

@throws InvalidPayloadException
@throws InvalidQuantityException
This function builds separate line items for each of the available discounts within the provided promotion . Every item will be built with a corresponding price definition based on the configuration of a discount entity . The resulting list of line items will then be returned and can be added to the cart . The function will already avoid duplicate entries .