addLineItems example

$productLineItem = new LineItem($productId, LineItem::PRODUCT_LINE_ITEM_TYPE, $productId, 1);
        $taxRulesCustomItem = new TaxRuleCollection([new TaxRule($taxForCustomItem)]);
        $customLineItem = (new LineItem($customItemId, LineItem::CUSTOM_LINE_ITEM_TYPE, $customItemId, 1))
            ->setLabel('custom')
            ->setPriceDefinition(new QuantityPriceDefinition(200, $taxRulesCustomItem, 2));

        $creditLineItem = (new LineItem($creditId, LineItem::CREDIT_LINE_ITEM_TYPE, $creditId, 1))
            ->setLabel('credit')
            ->setPriceDefinition(new AbsolutePriceDefinition(-100));

        $cart->addLineItems(new LineItemCollection([$productLineItem$customLineItem$creditLineItem]));

        $calculated = $this->processor->process($cart$this->context, new CartBehavior());

        static::assertCount(3, $calculated->getLineItems());

        $creditLineItem = $calculated->getLineItems()->filterType(LineItem::CREDIT_LINE_ITEM_TYPE)->first();
        static::assertInstanceOf(LineItem::class$creditLineItem);
        static::assertInstanceOf(CalculatedPrice::class$creditLineItem->getPrice());
        static::assertCount(2, $creditCalculatedTaxes = $creditLineItem->getPrice()->getCalculatedTaxes()->getElements());

        $calculatedTaxForCustomItem = array_filter($creditCalculatedTaxesfn (CalculatedTax $tax) => (int) $tax->getTaxRate() === $taxForCustomItem);

        
return $this->builder->build($cart$data$this->context, $cartBehaviour);
    }

    private function createCart(bool $withoutLineItems = false): Cart
    {
        $cart = new Cart('test');
        if ($withoutLineItems) {
            return $cart;
        }

        $lineItems = $this->createLineItems();
        $cart->addLineItems($lineItems);

        return $cart;
    }

    private function createLineItems(): LineItemCollection
    {
        $lineItems = new LineItemCollection();
        $lineItem = $this->createLineItem();
        $lineItems->add($lineItem);

        return $lineItems;
    }
static::assertSame($lineItemWithFreeDelivery ?? false, $rule->match($scope));
        $rule = new CartHasDeliveryFreeItemRule(false);
        static::assertSame(!$lineItemWithFreeDelivery$rule->match($scope));
    }

    /** * @dataProvider inputProvider */
    public function testMatchInCartScope(?bool $lineItemWithFreeDelivery): void
    {
        $scope = new CartRuleScope(new Cart(Uuid::randomHex())$this->createMock(SalesChannelContext::class));
        $scope->getCart()->addLineItems(new LineItemCollection([$this->getLineItem($lineItemWithFreeDelivery)]));

        $rule = new CartHasDeliveryFreeItemRule(true);
        static::assertSame($lineItemWithFreeDelivery ?? false, $rule->match($scope));
        $rule = new CartHasDeliveryFreeItemRule(false);
        static::assertSame(!$lineItemWithFreeDelivery$rule->match($scope));
    }

    public function testMatchInCartScopeWithEmptyCart(): void
    {
        $scope = new CartRuleScope(new Cart(Uuid::randomHex())$this->createMock(SalesChannelContext::class));

        
$item2 = $this->createProductItem(20, 10);
        $item3 = $this->createProductItem(50, 10);

        $item1->setReferencedId($item1->getId());
        $item2->setReferencedId($item2->getId());
        $item3->setReferencedId($item3->getId());

        $item1->setQuantity(10);
        $item2->setQuantity(10);
        $item3->setQuantity(10);

        $cart->addLineItems(new LineItemCollection([$item1$item2$item3]));

        $rules = new AndRule(
            [
                $this->getProductsRule([$item1->getReferencedId()$item2->getReferencedId()]),
            ]
        );

        $ruleEntity = new RuleEntity();
        $ruleEntity->setId(Uuid::randomHex());
        $ruleEntity->setPayload($rules);

        
// if we have a line item in cart for this discount, it is already stored and we do not need to add         // another lineitem         if ($lineItems->count() > 0) {
            return;
        }

        $priceDefinition = new QuantityPriceDefinition(0, new TaxRuleCollection(), 1);
        $price = $this->quantityPriceCalculator->calculate($priceDefinition$context);

        $promotionItem = $this->builder->buildDeliveryPlaceholderLineItem($discount$priceDefinition$price);

        $toCalculate->addLineItems(new LineItemCollection([$promotionItem]));
    }

    private function isAutomaticDisount(LineItem $discountItem): bool
    {
        return empty($discountItem->getPayloadValue('code'));
    }
}
if ($orderNumber === null) {
            throw OrderException::missingOrderNumber($order->getId());
        }

        $cart->addExtension(self::ORIGINAL_ORDER_NUMBER, new IdStruct($orderNumber));
        /* NEXT-708 support: - transactions */

        $lineItems = LineItemTransformer::transformFlatToNested($order->getLineItems());

        $cart->addLineItems($lineItems);
        $cart->setDeliveries(
            $this->convertDeliveries($order->getDeliveries()$lineItems)
        );

        $event = new OrderConvertedEvent($order$cart$context);
        $this->eventDispatcher->dispatch($event);

        return $event->getConvertedCart();
    }

    /** * @param array<string, array<string, bool>|string> $overrideOptions * * @throws InconsistentCriteriaIdsException */
static::assertFalse($this->contextExists($token));
    }

    public function testReplaceUpdatesCartTokenToo(): void
    {
        $token = Random::getAlphanumericString(32);

        $context = $this->getContainer()->get(SalesChannelContextFactory::class)
            ->create($token, TestDefaults::SALES_CHANNEL);

        $cart = new Cart($token);
        $cart->addLineItems(new LineItemCollection([new LineItem('test', 'test', Uuid::randomHex(), 10)]));
        $this->getContainer()->get(CartPersister::class)->save($cart$context);

        static::assertTrue($this->cartExists($token));

        $newToken = $this->contextPersister->replace($token$context);

        static::assertTrue($this->cartExists($newToken));
        static::assertFalse($this->cartExists($token));
    }

    public function testCustomerIdColumnIsBeingUsed(): void
    {
$productId1 = $this->createProduct($currentContext->getContext());
        $productId2 = $this->createProduct($currentContext->getContext());

        $productLineItem1 = new LineItem($productId1, LineItem::PRODUCT_LINE_ITEM_TYPE, $productId1);
        $productLineItem2 = new LineItem($productId2, LineItem::PRODUCT_LINE_ITEM_TYPE, $productId2);
        $productLineItem1->setStackable(true);
        $productLineItem2->setStackable(true);
        $productLineItem1->setQuantity(1);
        $guestProductQuantity = 5;
        $productLineItem2->setQuantity($guestProductQuantity);

        $previousCart->addLineItems(new LineItemCollection([$productLineItem1$productLineItem2]));
        $previousCart->markUnmodified();

        $cartMergedEvent = new CartMergedEvent(new Cart('customerToken')$currentContext$previousCart);

        $subscriber->addCartMergedNoticeFlash($cartMergedEvent);

        static::assertNotEmpty($infoFlash = $session->getFlashBag()->get('info'));

        static::assertEquals('checkout.cart-merged-hint', $infoFlash[0]);
    }

    
new TaxRuleCollection(),
                1,
                null,
                $listPrice
            )
        );
    }

    protected static function createCart(LineItemCollection $lineItemCollection): Cart
    {
        $cart = new Cart(Uuid::randomHex());
        $cart->addLineItems($lineItemCollection);

        return $cart;
    }
}
static::assertCount(0, $caughtEvent->getCart()->getLineItems());
    }

    public function testCartVerifyPersistEventIsFiredAndPersisted(): void
    {
        $caughtEvent = null;
        $this->addEventListener($this->getContainer()->get('event_dispatcher'), CartVerifyPersistEvent::classstatic function DCartVerifyPersistEvent $event) use (&$caughtEvent): void {
            $caughtEvent = $event;
        });

        $cart = new Cart('existing');
        $cart->addLineItems(new LineItemCollection([
            new LineItem(Uuid::randomHex(), LineItem::PROMOTION_LINE_ITEM_TYPE, Uuid::randomHex(), 1),
        ]));

        $this->getContainer()->get(CartPersister::class)
            ->save($cart$this->getSalesChannelContext($cart->getToken()));

        $token = $this->getContainer()->get(Connection::class)
            ->fetchOne('SELECT token FROM cart WHERE token = :token', ['token' => $cart->getToken()]);

        static::assertNotEmpty($token);

        
$productId1 = $this->createProduct($currentContext->getContext());
        $productId2 = $this->createProduct($currentContext->getContext());

        $productLineItem1 = new LineItem($productId1, LineItem::PRODUCT_LINE_ITEM_TYPE, $productId1);
        $productLineItem2 = new LineItem($productId2, LineItem::PRODUCT_LINE_ITEM_TYPE, $productId2);
        $productLineItem1->setStackable(true);
        $productLineItem2->setStackable(true);
        $productLineItem1->setQuantity(1);
        $guestProductQuantity = 5;
        $productLineItem2->setQuantity($guestProductQuantity);

        $cart->addLineItems(new LineItemCollection([$productLineItem1$productLineItem2]));
        $cart->markUnmodified();

        $this->getContainer()->get(CartPersister::class)->save($cart$currentContext);

        // Create Saved Customer cart         $customerToken = Random::getAlphanumericString(32);
        $customerContext = $this->createSalesChannelContext($customerToken);

        $this->contextPersister->save($customerToken[]$currentContext->getSalesChannel()->getId()$this->customerId);

        $cart = new Cart($customerToken);

        
$cart = new Cart(Uuid::randomHex());
        $context = $this->createMock(SalesChannelContext::class);
        $expectedLineItem = new LineItem($productId, 'product');

        $this->lineItemRegistryMock->expects(static::once())
            ->method('create')
            ->with($expectedLineItemData$this->createMock(SalesChannelContext::class))
            ->willReturn($expectedLineItem);

        $this->translatorCallback();

        $this->controller->addLineItems($cartnew RequestDataBag($request->request->all())$request$context);
    }

    public function testAddLineItemsCallsLineItemSetDefaultValues(): void
    {
        $productId = Uuid::randomHex();
        $productId2 = Uuid::randomHex();
        $lineItemData = [
            'id' => $productId,
            'referencedId' => $productId,
            'type' => 'product',
            'priceDefinition' => [
                
private function getCart(): Cart
    {
        $items = [
            new LineItem(Uuid::randomHex(), LineItem::PRODUCT_LINE_ITEM_TYPE),
            new LineItem(Uuid::randomHex(), LineItem::PROMOTION_LINE_ITEM_TYPE),
            new LineItem(Uuid::randomHex(), LineItem::CREDIT_LINE_ITEM_TYPE),
            new LineItem(Uuid::randomHex(), LineItem::CUSTOM_LINE_ITEM_TYPE),
        ];

        $cart = new Cart('token');
        $cart->addLineItems(new LineItemCollection($items));

        return $cart;
    }
}
Home | Imprint | This part of the site doesn't use cookies.