public function collect(StructCollection $fetchDefinitions, StructCollection $data, Cart $cart, SalesChannelContext $context, CartBehavior $behavior): void
    {
        /** @var Collection $promotionDefinitions */
        $promotionDefinitions = $fetchDefinitions->filterInstance(CartPromotionsFetchDefinition::class);

        // verify if we even have to collect data.
        // if no definition object exists (not prepared), then simply skip this step.
        if ($promotionDefinitions->count() <= 0) {
            return;
        }

        /** @var CartPromotionsFetchDefinition $definition */
        $definition = $promotionDefinitions->getElements()[0];

        /** @var array $codes */
        $codes = $this->getCodesFromPlaceholdersIds($cart, $definition->getLineItemIds());

        /** @var array $promotions */
        $promotions = $this->searchPromotions($codes, $context);

        $newPromotions = [];

        // we now have a list of promotions that could be added to our cart.
        // verify if they have any discounts. if so, add them to our
        // data struct, which ensures that they will be added later in the enrichment process.
        /** @var PromotionEntity $promotion */
        foreach ($promotions as $promotion) {
            /** @var PromotionDiscountCollection|null $collection */
            $collection = $promotion->getDiscounts();

            // check if no discounts have been set
            if (!$collection instanceof PromotionDiscountCollection || count($collection->getElements()) <= 0) {
                continue;
            }

            $newPromotions[] = $promotion;
        }

        // add the collected promotions to our definition.
        // these promotions are converted into line items in the enrichment function.
        $data->set(self::DATA_KEY, new CartPromotionsDataDefinition($newPromotions));
    }
public function collect(StructCollection $fetchDefinitions, StructCollection $data, Cart $cart, SalesChannelContext $context, CartBehavior $behavior): void
    {
        /** @var Collection $promotionDefinitions */
        $promotionDefinitions = $fetchDefinitions->filterInstance(CartPromotionsFetchDefinition::class);

        // verify if we even have to collect data.
        // if no definition object exists (not prepared), then simply skip this step.
        if ($promotionDefinitions->count() <= 0) {
            return;
        }

        /** @var CartPromotionsFetchDefinition $definition */
        $definition = $promotionDefinitions->getElements()[0];

        /** @var array $codes */
        $codes = $this->getCodesFromPlaceholdersIds($cart, $definition->getLineItemIds());

        /** @var array $promotions */
        $promotions = $this->searchPromotions($codes, $context);

        $newPromotions = [];

        // we now have a list of promotions that could be added to our cart.
        // verify if they have any discounts. if so, add them to our
        // data struct, which ensures that they will be added later in the enrichment process.
        /** @var PromotionEntity $promotion */
        foreach ($promotions as $promotion) {
            /** @var PromotionDiscountCollection|null $collection */
            $collection = $promotion->getDiscounts();

            // check if no discounts have been set
            if (!$collection instanceof PromotionDiscountCollection || count($collection->getElements()) <= 0) {
                continue;
            }

            $newPromotions[] = $promotion;
        }

        // add the collected promotions to our definition.
        // these promotions are converted into line items in the enrichment function.
        $data->set(self::DATA_KEY, new CartPromotionsDataDefinition($newPromotions));
    }
public function collect ( StructCollection $ fetchDefinitions , StructCollection $ data , Cart $ cart , SalesChannelContext $ context , CartBehavior $ behavior ) : void { /** @var Collection $promotionDefinitions */ $ promotionDefinitions = $ fetchDefinitions -> filterInstance ( CartPromotionsFetchDefinition :: class ) ; // verify if we even have to collect data. // if no definition object exists (not prepared), then simply skip this step. if ( $ promotionDefinitions -> count ( ) <= 0 ) { return ; } /** @var CartPromotionsFetchDefinition $definition */ $ definition = $ promotionDefinitions -> getElements ( ) [ 0 ] ; /** @var array $codes */ $ codes = $ this -> getCodesFromPlaceholdersIds ( $ cart , $ definition -> getLineItemIds ( ) ) ; /** @var array $promotions */ $ promotions = $ this -> searchPromotions ( $ codes , $ context ) ; $ newPromotions = [ ] ; // we now have a list of promotions that could be added to our cart. // verify if they have any discounts. if so, add them to our // data struct, which ensures that they will be added later in the enrichment process. /** @var PromotionEntity $promotion */ foreach ( $ promotions as $ promotion ) { /** @var PromotionDiscountCollection|null $collection */ $ collection = $ promotion -> getDiscounts ( ) ; // check if no discounts have been set if ( ! $ collection instanceof PromotionDiscountCollection || count ( $ collection -> getElements ( ) ) <= 0 ) { continue ; } $ newPromotions [ ] = $ promotion ; } // add the collected promotions to our definition. // these promotions are converted into line items in the enrichment function. $ data -> set ( self :: DATA_KEY , new CartPromotionsDataDefinition ( $ newPromotions ) ) ; }
This function is used to collect our promotion data for our cart.
It queries the database for all placeholder promotions and their codes
along with all non-code promotions that are applied automatically if conditions are met.
The eligible promotions will then be passed on to the enrichment function.

@throws InvalidPayloadException
@throws InvalidQuantityException
This function is used to collect our promotion data for our cart . It queries the database for all placeholder promotions and their codes along with all non - code promotions that are applied automatically if conditions are met . The eligible promotions will then be passed on to the enrichment function .