buildPlaceholderItem example

return $cartService->add($cart$product$context);
    }

    /** * Adds the provided code to the current cart. */
    public function addPromotionCode(string $code, Cart $cart, CartService $cartService, SalesChannelContext $context): Cart
    {
        $itemBuilder = new PromotionItemBuilder();

        // ??? currencyPrecision is unused         $lineItem = $itemBuilder->buildPlaceholderItem($code);

        $cart = $cartService->add($cart$lineItem$context);

        return $cart;
    }

    /** * Removes the provided code to the current cart. */
    public function removePromotionCode(string $code, Cart $cart, CartService $cartService, SalesChannelContext $context): Cart
    {
        
#[Route(path: '/checkout/promotion/add', name: 'frontend.checkout.promotion.add', defaults: ['XmlHttpRequest' => true], methods: ['POST'])]     public function addPromotion(Cart $cart, Request $request, SalesChannelContext $context): Response
    {
        return Profiler::trace('cart::add-promotion', function D) use ($cart$request$context) {
            try {
                $code = (string) $request->request->get('code');

                if ($code === '') {
                    throw RoutingException::missingRequestParameter('code');
                }

                $lineItem = $this->promotionItemBuilder->buildPlaceholderItem($code);

                $cart = $this->cartService->add($cart$lineItem$context);

                // we basically show all cart errors or notices                 // at the moments its not possible to show success messages with "green" color                 // from the cart...thus it has to be done in the storefront level                 // so if we have an promotion added notice, we simply convert this to                 // a success flash message                 $addedEvents = $cart->getErrors()->filterInstance(PromotionCartAddedInformationError::class);
                if ($addedEvents->count() > 0) {
                    $this->addFlash(self::SUCCESS, $this->trans('checkout.codeAddedSuccessful'));

                    
PromotionCollector::SKIP_AUTOMATIC_PROMOTIONS => true,
            ]
        );

        $salesChannelContext = $this->orderConverter->assembleSalesChannelContext(
            $order,
            $context,
            $options,
        );
        $cart = $this->orderConverter->convertToCart($order$context);

        $promotionLineItem = $this->promotionItemBuilder->buildPlaceholderItem($code);

        $cart->add($promotionLineItem);
        $recalculatedCart = $this->recalculateCart($cart$salesChannelContext);

        $conversionContext = (new OrderConversionContext())
            ->setIncludeCustomer(false)
            ->setIncludeBillingAddress(false)
            ->setIncludeDeliveries(false)
            ->setIncludeTransactions(false)
            ->setIncludeOrderDate(false);

        

    /** * This test verifies that the immutable LineItem Type from * the constructor is correctly used in the LineItem. * * @group promotions */
    public function testLineItemType(): void
    {
        $builder = new PromotionItemBuilder();

        $item = $builder->buildPlaceholderItem('CODE-123');

        static::assertEquals(PromotionProcessor::LINE_ITEM_TYPE, $item->getType());
    }

    /** * This test verifies that we get a correct percentage price of 0 * for our placeholder item. This is important to avoid any wrong * calculations or side effects that could modify the cart amount. * * @group promotions */
    
Home | Imprint | This part of the site doesn't use cookies.