setDeliveryInformation example

private function getLineItem(
        float $weight = 10.0,
        int $quantity = 1,
        bool $freeDelivery = true,
        ?float $height = null,
        ?float $width = null,
        ?float $length = null
    ): LineItem {
        return (new LineItem(Uuid::randomHex(), 'product', null, $quantity))
            ->setPrice(new CalculatedPrice(0, 0, new CalculatedTaxCollection()new TaxRuleCollection()))
            ->setDeliveryInformation(
                new DeliveryInformation(
                    $quantity,
                    $weight,
                    $freeDelivery,
                    null,
                    null,
                    $height,
                    $width,
                    $length,
                )
            );
    }
'free item' => [true],
            'not free item' => [false],
            'no delivery information' => [null],
        ];
    }

    private function getLineItem(?bool $freeDelivery = null): LineItem
    {
        $lineItem = new LineItem(Uuid::randomHex(), LineItem::PRODUCT_LINE_ITEM_TYPE);

        if ($freeDelivery !== null) {
            $lineItem->setDeliveryInformation(new DeliveryInformation(3, null, $freeDelivery));
        }

        return $lineItem;
    }
}
$this->removeExtensions($cart);
        $this->removeExtensions($convertedCart);

        // remove delivery information from line items
        foreach ($cart->getDeliveries() as $delivery) {
            // remove address from ShippingLocation             $property = ReflectionHelper::getProperty(ShippingLocation::class, 'address');
            $property->setValue($delivery->getLocation(), null);

            foreach ($delivery->getPositions() as $position) {
                $position->getLineItem()->setDeliveryInformation(null);
                $position->getLineItem()->setQuantityInformation(null);

                foreach ($position->getLineItem()->getChildren() as $lineItem) {
                    $lineItem->setDeliveryInformation(null);
                    $lineItem->setQuantityInformation(null);
                }
            }

            $delivery->getShippingMethod()->setPrices(new ShippingMethodPriceCollection());
        }

        
$deliveryTime = null;
        if ($product->getDeliveryTime() !== null) {
            $deliveryTime = DeliveryTime::createFromEntity($product->getDeliveryTime());
        }

        $weight = $product->getWeight();

        $lineItem->setStates($product->getStates());

        if ($lineItem->hasState(State::IS_PHYSICAL)) {
            $lineItem->setDeliveryInformation(
                new DeliveryInformation(
                    (int) $product->getAvailableStock(),
                    $weight,
                    $product->getShippingFree() === true,
                    $product->getRestockTime(),
                    $deliveryTime,
                    $product->getHeight(),
                    $product->getWidth(),
                    $product->getLength()
                )
            );
        }


    protected static function createLineItemWithDeliveryInfo(
        bool $freeDelivery,
        int $quantity = 1,
        ?float $weight = 50.0,
        ?float $height = null,
        ?float $width = null,
        ?float $length = null,
        int $stock = 9999
    ): LineItem {
        return self::createLineItem(LineItem::PRODUCT_LINE_ITEM_TYPE, $quantity)->setDeliveryInformation(
            new DeliveryInformation(
                $stock,
                $weight,
                $freeDelivery,
                null,
                (new DeliveryTime())->assign([
                    'min' => 1,
                    'max' => 3,
                    'unit' => 'weeks',
                    'name' => '1-3 weeks',
                ]),
                
$context = $this->createMock(SalesChannelContext::class);

        $delivery = $this->getMockBuilder(Delivery::class)
            ->disableOriginalConstructor()
            ->getMock();
        $costs = new CalculatedPrice(0, 0, new CalculatedTaxCollection()new TaxRuleCollection());
        $delivery->expects(static::atLeastOnce())->method('getShippingCosts')->willReturn($costs);
        $delivery->expects(static::never())->method('setShippingCosts');
        $delivery->expects(static::atLeastOnce())->method('getShippingMethod')->willReturn($shippingMethod);

        $lineItem = new LineItem(Uuid::randomHex(), 'product');
        $lineItem->setDeliveryInformation(
            new DeliveryInformation(
                10,
                12.0,
                false,
                null,
                $this->deliveryTime
            )
        );
        $lineItem->setPrice(new CalculatedPrice(0, 0, new CalculatedTaxCollection()new TaxRuleCollection()));
        $price = $lineItem->getPrice();
        static::assertNotNull($price);

        
$lineItem = $this->createLineItem();
        $lineItems->add($lineItem);

        return $lineItems;
    }

    private function createLineItem(): LineItem
    {
        $lineItem = new LineItem('testid', LineItem::PRODUCT_LINE_ITEM_TYPE);

        $deliveryInformation = $this->createDeliveryInformation();
        $lineItem->setDeliveryInformation($deliveryInformation);

        $price = new CalculatedPrice(100, 200, new CalculatedTaxCollection()new TaxRuleCollection());
        $lineItem->setPrice($price);

        return $lineItem;
    }

    private function createDeliveryInformation(): DeliveryInformation
    {
        $deliveryTime = $this->createDeliveryTime();

        
$deliveryProcessor = $this->getContainer()->get(DeliveryProcessor::class);

        $cartDataCollection = new CartDataCollection();
        $cartDataCollection->set(
            DeliveryProcessor::buildKey($this->salesChannelContext->getShippingMethod()->getId()),
            $this->salesChannelContext->getShippingMethod()
        );
        $originalCart = new Cart('original');
        $calculatedCart = new Cart('calculated');

        $lineItem = new LineItem('test', LineItem::PRODUCT_LINE_ITEM_TYPE);
        $lineItem->setDeliveryInformation(new DeliveryInformation(5, 0, false));
        $lineItem->setPrice(new CalculatedPrice(5.0, 5.0, new CalculatedTaxCollection([
            new CalculatedTax(5, 19, 5),
        ])new TaxRuleCollection()));

        $calculatedCart->setLineItems(new LineItemCollection([$lineItem]));

        $cartBehavior = new CartBehavior();

        static::assertCount(0, $calculatedCart->getDeliveries());

        $deliveryProcessor->process($cartDataCollection$originalCart$calculatedCart$this->salesChannelContext, $cartBehavior);

        
$toCalculate->add($lineItem);
        }
    }

    private function enrich(LineItem $lineItem): void
    {
        if ($lineItem->getDeliveryInformation() !== null) {
            return;
        }

        $lineItem->setDeliveryInformation(new DeliveryInformation($lineItem->getQuantity(), 0, false));
    }
}
 $configData['fields']);
    }

    private function createLineItem(int $stock, string $id = 'line-item-id'): LineItem
    {
        $lineItem = new LineItem(
            $id,
            LineItem::PRODUCT_LINE_ITEM_TYPE,
            'product-id'
        );

        $lineItem->setDeliveryInformation(new DeliveryInformation(
            $stock,
            1.0,
            false
        ));

        return $lineItem;
    }
}
->disableOriginalConstructor()
            ->getMock();
        $costs = new CalculatedPrice(0.0, 0.0, new CalculatedTaxCollection()new TaxRuleCollection());
        $delivery->expects(static::atLeastOnce())->method('getShippingCosts')->willReturn($costs);
        $newCosts = null;
        $delivery->expects(static::once())->method('setShippingCosts')->willReturnCallback(function D$costsParameter) use (&$newCosts): void {
            /** @var CalculatedPrice $newCosts */
            $newCosts = $costsParameter;
        });

        $lineItem = new LineItem(Uuid::randomHex(), 'product');
        $lineItem->setDeliveryInformation(
            new DeliveryInformation(
                10,
                12.0,
                true,
                null,
                $this->deliveryTime
            )
        );
        $lineItem->setPrice(new CalculatedPrice(1, 1, new CalculatedTaxCollection()new TaxRuleCollection()));
        $price = $lineItem->getPrice();
        static::assertNotNull($price);

        
Home | Imprint | This part of the site doesn't use cookies.