setBehavior example

$builder
                ->create(['id' => 'product-2', 'referencedId' => 'product-2']$salesChannelContext)
                ->setReferencedId('product-1')
                ->setQuantityInformation(
                    (new QuantityInformation())
                        ->setMinPurchase(1)
                        ->setMaxPurchase(1)
                        ->setPurchaseSteps(1)
                )
        );

        $cart->setBehavior(new CartBehavior([
            ProductCartProcessor::SKIP_PRODUCT_STOCK_VALIDATION => true,
        ]));

        static::assertCount(0, $cart->getErrors());

        $validator = new ProductLineItemValidator();
        $validator->validate($cart$cart->getErrors()$salesChannelContext);

        static::assertCount(0, $cart->getErrors());
    }

    
 {
    }

    public function process(Cart $original, SalesChannelContext $context, CartBehavior $behavior): Cart
    {
        return Profiler::trace('cart::process', function D) use ($original$context$behavior) {
            $cart = new Cart($original->getToken());
            $cart->setCustomerComment($original->getCustomerComment());
            $cart->setAffiliateCode($original->getAffiliateCode());
            $cart->setCampaignCode($original->getCampaignCode());
            $cart->setSource($original->getSource());
            $cart->setBehavior($behavior);
            $cart->addState(...$original->getStates());

            if ($behavior->hookAware()) {
                // reset modified state that apps always have the same entry state                 foreach ($original->getLineItems()->getFlat() as $item) {
                    $item->markUnModifiedByApp();
                }
            }

            // move data from previous calculation into new cart             $cart->setData($original->getData());

            

                ]
            )
        );

        $data = new CartDataCollection();

        $cart = new Cart('test');
        $cartBehavior = new CartBehavior([
            DeliveryProcessor::SKIP_DELIVERY_PRICE_RECALCULATION => true,
        ]);
        $cart->setBehavior($cartBehavior);

        $quantityPriceCalculatorMock = $this->createMock(QuantityPriceCalculator::class);
        $quantityPriceCalculatorMock->expects(static::once())->method('calculate')->willReturn($costs);

        $deliveryCalculator = new DeliveryCalculator(
            $quantityPriceCalculatorMock,
            $this->createMock(PercentageTaxRuleBuilder::class),
        );

        $deliveryCalculator->calculate($data$cartnew DeliveryCollection([$delivery])$context);
        static::assertNotNull($newCosts);
        
foreach ($cart->getLineItems()->getFlat() as $lineItem) {
            $lineItem->setDeliveryInformation(null);
            $lineItem->setQuantityInformation(null);
        }

        $this->resetDataTimestamps($cart->getLineItems());
        foreach ($cart->getDeliveries() as $delivery) {
            $this->resetDataTimestamps($delivery->getPositions()->getLineItems());
        }
        $cart->setRuleIds([]);
        // The behaviour will be set during the process, therefore we remove it here         $cart->setBehavior(null);

        // unique identifier is set at runtime to be random uuid         foreach ($convertedCart->getLineItems()->getFlat() as $lineItem) {
            $lineItem->assign(['uniqueIdentifier' => 'foo']);
        }

        foreach ($convertedCart->getDeliveries() as $delivery) {
            foreach ($delivery->getPositions() as $position) {
                $position->getLineItem()->assign(['uniqueIdentifier' => 'foo']);

                foreach ($position->getLineItem()->getChildren()->getFlat() as $lineItem) {
                    


        $this->expectException(CartException::class);
        $this->expectExceptionMessage('Cart instance of the cart facade were never calculated. Please call calculate() before using the cart facade.');

        $facade->calculate();
    }

    public function testCalculateWorksWhenBehaviorIsGiven(): void
    {
        $cart = new Cart('foo');
        $cart->setBehavior(new CartBehavior());

        $helper = $this->createMock(CartFacadeHelper::class);
        $helper->expects(static::once())->method('calculate');

        $facade = new CartFacade(
            $helper,
            $this->createMock(ScriptPriceStubs::class),
            $cart,
            $this->createMock(SalesChannelContext::class)
        );

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

        static::assertNotEmpty($token);
    }

    public function testRecalculationCartShouldNotBeSaved(): void
    {
        $cartBehavior = new CartBehavior([], true, true);

        $cart = new Cart('existing');
        $cart->setBehavior($cartBehavior);
        $cart->add(
            (new LineItem('A', 'test'))
                ->setPrice(new CalculatedPrice(0, 0, new CalculatedTaxCollection()new TaxRuleCollection()))
                ->setLabel('test')
        );

        $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()]);

        
yield 'Discount product price' => [
            'discount-product-price',
            [
                'p1' => new ExpectedPrice(90, 90),
            ],
        ];
    }

    private function createCart(): Cart
    {
        $cart = new Cart('test');
        $cart->setBehavior(new CartBehavior());
        $cart->addState('default-state');

        return $cart;
    }

    /** * @param array<string, ExpectedPrice|null> $expectations */
    private function assertItems(ItemsFacade|CartFacade|LineItemCollection $scope, array $expectations): void
    {
        foreach ($expectations as $key => $expected) {
            
Home | Imprint | This part of the site doesn't use cookies.