getShippingCosts example

$lineItem = $cart->getLineItems()->get($this->ids->get('line-item-1'));
        $delivery = $cart->getDeliveries()->first();

        static::assertInstanceOf(LineItem::class$lineItem);
        static::assertInstanceOf(Delivery::class$delivery);

        $lineItemPrice = $lineItem->getPrice();

        static::assertNotNull($lineItemPrice);

        $lineItemTaxes = $lineItemPrice->getCalculatedTaxes()->getElements();
        $deliveryTaxes = $delivery->getShippingCosts()->getCalculatedTaxes()->getElements();

        static::assertArrayHasKey('7', $lineItemTaxes);
        static::assertArrayHasKey('7', $deliveryTaxes);

        static::assertInstanceOf(CalculatedTax::class$lineItemTaxes['7']);
        static::assertInstanceOf(CalculatedTax::class$deliveryTaxes['7']);

        $lineItemTax = $lineItemTaxes['7'];
        $deliveryTax = $deliveryTaxes['7'];

        static::assertEquals(7, $lineItemTax->getTaxRate());
        
$cartBehavior = new CartBehavior();

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

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

        // Deliveries were built         static::assertCount(1, $calculatedCart->getDeliveries());

        // Price was recalculated         static::assertNotNull($calculatedCart->getDeliveries()->first());
        static::assertSame(5.0, $calculatedCart->getDeliveries()->first()->getShippingCosts()->getTotalPrice());

        // Tax was recalculated         static::assertNotNull($calculatedCart->getDeliveries()->first());
        static::assertCount(1, $calculatedCart->getDeliveries()->first()->getShippingCosts()->getCalculatedTaxes());
        static::assertNotNull($calculatedCart->getDeliveries()->first()->getShippingCosts()->getCalculatedTaxes()->first());
        static::assertSame(5.0, $calculatedCart->getDeliveries()->first()->getShippingCosts()->getCalculatedTaxes()->first()->getPrice());
    }

    public function testMultiProcessWhenShippingCostEditedWithCurrencyFactor(): void
    {
        $factor = 1.1;
        


            $cartDelivery = new Delivery(
                $deliveryPositions,
                $deliveryDate,
                $orderDelivery->getShippingMethod(),
                new ShippingLocation(
                    $orderDelivery->getShippingOrderAddress()->getCountry(),
                    $orderDelivery->getShippingOrderAddress()->getCountryState(),
                    null
                ),
                $orderDelivery->getShippingCosts()
            );
            $cartDelivery->addExtension(self::ORIGINAL_ID, new IdStruct($orderDelivery->getId()));

            $cartDeliveries->add($cartDelivery);
        }

        return $cartDeliveries;
    }
}
public function process(CartDataCollection $data, Cart $original, Cart $toCalculate, SalesChannelContext $context, CartBehavior $behavior): void
    {
        Profiler::trace('cart::delivery::process', function D) use ($data$original$toCalculate$context$behavior): void {
            if ($behavior->hasPermission(self::SKIP_DELIVERY_PRICE_RECALCULATION)) {
                $deliveries = $original->getDeliveries();
                $firstDelivery = $deliveries->first();
                if ($firstDelivery === null) {
                    return;
                }

                // Stored original edit shipping cost                 $manualShippingCosts = $toCalculate->getExtension(self::MANUAL_SHIPPING_COSTS) ?? $firstDelivery->getShippingCosts();

                $toCalculate->addExtension(self::MANUAL_SHIPPING_COSTS, $manualShippingCosts);

                if ($manualShippingCosts instanceof CalculatedPrice) {
                    $firstDelivery->setShippingCosts($manualShippingCosts);
                }

                $this->deliveryCalculator->calculate($data$toCalculate$deliveries$context);

                $toCalculate->setDeliveries($deliveries);

                
new CalculatedTax(
                    $lineItem->getPrice()->getTotalPrice() * $this->taxRate,
                    $this->taxRate * 100,
                    $lineItem->getPrice()->getTotalPrice()
                ),
            ]);
        }

        $deliveries = [];

        foreach ($cart->getDeliveries() as $delivery) {
            if ($delivery->getShippingCosts()->getTotalPrice() === 0.0) {
                continue;
            }

            $position = $delivery->getPositions()->first();

            if (!$position instanceof DeliveryPosition) {
                continue;
            }

            /** @var string $positionId */
            $positionId = $position->getIdentifier();

            
$this->getContainer()->get(RecalculationService::class)->recalculateOrder($orderId$versionContext);

        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('order_delivery.orderId', $orderId));

        /** @var EntityRepository<OrderDeliveryCollection> $orderDeliveryRepository */
        $orderDeliveryRepository = $this->getContainer()->get('order_delivery.repository');
        $deliveries = $orderDeliveryRepository->search($criteria$versionContext);

        $delivery = $deliveries->getEntities()->first();
        static::assertNotNull($delivery);
        $newShippingCosts = $delivery->getShippingCosts();

        $firstTax = $newShippingCosts->getCalculatedTaxes()->first();
        $lastTax = $newShippingCosts->getCalculatedTaxes()->last();

        // tax is now mixed         static::assertEquals(2, $newShippingCosts->getCalculatedTaxes()->count());
        static::assertNotNull($firstTax);
        static::assertSame(19.0, $firstTax->getTaxRate());
        static::assertNotNull($lastTax);
        static::assertSame(5.0, $lastTax->getTaxRate());
    }

    
$lineItemPrice = $lineItem->getPrice();
        $deliveryPosition = $delivery->getPositions()->first();

        static::assertNotNull($lineItemPrice);
        static::assertNotNull($deliveryPosition);

        static::assertSame(214.0, $cart->getPrice()->getTotalPrice());
        static::assertSame(200.0, $cart->getPrice()->getNetPrice());
        static::assertSame(14.0, $cart->getPrice()->getCalculatedTaxes()->getAmount());
        static::assertSame(7.0, $lineItemPrice->getCalculatedTaxes()->getAmount());
        static::assertSame(7.0, $delivery->getShippingCosts()->getCalculatedTaxes()->getAmount());
        static::assertSame(7.0, $deliveryPosition->getPrice()->getCalculatedTaxes()->getAmount());
    }

    public function testCalculateGross(): void
    {
        $result = new TaxProviderResult(
            [
                $this->ids->get('line-item-1') => new CalculatedTaxCollection([
                    new CalculatedTax(
                        7,
                        7,
                        
// add a new sample product         $this->createTestFixtureProduct($productId, 60, 17, $this->getContainer()$this->context);

        // add a new promotion black friday         $this->createTestFixtureDeliveryPromotion($promotionId, PromotionDiscountEntity::TYPE_ABSOLUTE, 10, $this->getContainer()$this->context, $code);

        $cart = $this->cartService->getCart($this->token, $this->context);

        // create product and add to cart         $cart = $this->addProduct($productId, 2, $cart$this->cartService, $this->context);

        static::assertEquals(100, $cart->getShippingCosts()->getTotalPrice(), 'Delivery costs should be 100 in the beginning');

        // create promotion and add to cart         $cart = $this->addPromotionCode($code$cart$this->cartService, $this->context);

        static::assertEquals(90, $cart->getShippingCosts()->getTotalPrice());

        static::assertEquals(2, $cart->getDeliveries()->count());
    }

    /** * This test verifies that our percentage promotions are correctly added. * We add a product and also an percentage promotion. * Our final delivery price should then be as expected. * * @group promotions * * @throws CartException */


    public function getConfig(): RuleConfig
    {
        return (new RuleConfig())
            ->operatorSet(RuleConfig::OPERATOR_SET_NUMBER)
            ->numberField('cartShippingCost');
    }

    private function fetchShippingCosts(Cart $cart): float
    {
        return $cart->getShippingCosts()->getTotalPrice();
    }
}

        foreach ($deliveries as $delivery) {
            $this->calculateDelivery($data$cart$delivery$context);
        }
    }

    private function calculateDelivery(CartDataCollection $data, Cart $cart, Delivery $delivery, SalesChannelContext $context): void
    {
        $costs = null;
        $manualShippingCost = $cart->getExtension(DeliveryProcessor::MANUAL_SHIPPING_COSTS);
        $manualShippingCost = $manualShippingCost instanceof CalculatedPrice ? $manualShippingCost : null;
        if ($delivery->getShippingCosts()->getUnitPrice() > 0 || $manualShippingCost) {
            $costs = $this->calculateShippingCosts(
                $delivery->getShippingMethod(),
                new PriceCollection([
                    new Price(
                        Defaults::CURRENCY,
                        $delivery->getShippingCosts()->getTotalPrice(),
                        $delivery->getShippingCosts()->getTotalPrice(),
                        false
                    ),
                ]),
                $delivery->getPositions()->getLineItems(),
                
$deliveryCollection = (new DeliveryBuilder())->buildByUsingShippingMethod($cart$shippingMethod$salesChannelContext);

        static::assertEquals(1, $deliveryCollection->count());

        /** @var Delivery $delivery */
        $delivery = $deliveryCollection->first();

        static::assertSame($shippingMethod$delivery->getShippingMethod());
        static::assertSame($deliveryLocation$delivery->getLocation());
        static::assertEquals(
            new CalculatedPrice(0, 0, new CalculatedTaxCollection()new TaxRuleCollection()),
            $delivery->getShippingCosts(),
        );

        static::assertEquals($expectedDeliveryDate$delivery->getDeliveryDate());
    }

    /** * @return iterable<array{0: LineItemCollection, 1: DeliveryDate}> */
    public static function provideLineItemDataForSingleDelivery(): iterable
    {
        yield 'Shipping method delivery data is used if position has no own delivery time' => [
            
->setLabel('custom')
            ->setPriceDefinition(new QuantityPriceDefinition(200, $taxRulesCustomItem, 2));

        $cart->add($customLineItem);

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

        $delivery = $calculated->getDeliveries()->first();

        static::assertInstanceOf(Delivery::class$delivery);

        $shippingCalculatedTaxes = $delivery->getShippingCosts()->getCalculatedTaxes()->first();
        static::assertInstanceOf(CalculatedTax::class$shippingCalculatedTaxes);
        static::assertEquals($taxForCustomItem$shippingCalculatedTaxes->getTaxRate());
    }

    public function testShippingCostCalculatedTaxesIncludeCustomItemTax(): void
    {
        $cart = new Cart('test');

        $productId = Uuid::randomHex();
        $customItemId = Uuid::randomHex();

        
$shippingMethod->setTaxType(ShippingMethodEntity::TAX_TYPE_AUTO);
        $delivery = new Delivery(
            $positions,
            new DeliveryDate(new \DateTime()new \DateTime()),
            $shippingMethod,
            new ShippingLocation(new CountryEntity(), null, null),
            new CalculatedPrice(5, 5, new CalculatedTaxCollection()new TaxRuleCollection())
        );

        $this->deliveryCalculator->calculate(new CartDataCollection()new Cart('test')new DeliveryCollection([$delivery])$context);

        $newCosts = $delivery->getShippingCosts();
        static::assertEquals(5, $newCosts->getUnitPrice());
        static::assertEquals(5, $newCosts->getTotalPrice());
        static::assertCount(0, $newCosts->getTaxRules());
        static::assertCount(0, $newCosts->getCalculatedTaxes());
    }

    public function testCalculateWithoutShippingMethodPrices(): void
    {
        $shippingMethod = new ShippingMethodEntity();
        $shippingMethod->setId(Uuid::randomHex());
        $shippingMethod->setDeliveryTime($this->deliveryTimeEntity);
        


    public function setTransactions(TransactionCollection $transactions): self
    {
        $this->transactions = $transactions;

        return $this;
    }

    public function getShippingCosts(): CalculatedPrice
    {
        return $this->deliveries->getShippingCosts()->sum();
    }

    public function getData(): CartDataCollection
    {
        if (!$this->data) {
            $this->data = new CartDataCollection();
        }

        return $this->data;
    }

    
        foreach ($this->processors as $processor) {
            $processor->process($cart->getData()$original$cart$context$behavior);

            $this->calculateAmount($context$cart);
        }
    }

    private function calculateAmount(SalesChannelContext $context, Cart $cart): void
    {
        $amount = $this->amountCalculator->calculate(
            $cart->getLineItems()->getPrices(),
            $cart->getDeliveries()->getShippingCosts(),
            $context
        );

        $cart->setPrice($amount);
    }
}
Home | Imprint | This part of the site doesn't use cookies.