DeliveryDate example


        return $this->restockTime;
    }

    public function setRestockTime(?int $restockTime): void
    {
        $this->restockTime = $restockTime;
    }

    public function getDeliveryDate(): DeliveryDate
    {
        return new DeliveryDate(
            (new \DateTime())
                ->add(new \DateInterval('P' . 1 . 'D')),
            (new \DateTime())
                ->add(new \DateInterval('P' . 1 . 'D'))
                ->add(new \DateInterval('P' . 1 . 'D'))
        );
    }

    public function getRestockDeliveryDate(): DeliveryDate
    {
        $deliveryDate = $this->getDeliveryDate();

        

        return $this->earliest;
    }

    public function getLatest(): \DateTimeImmutable
    {
        return $this->latest;
    }

    public function add(\DateInterval $interval): self
    {
        return new DeliveryDate(
            $this->earliest->add($interval),
            $this->latest->add($interval)
        );
    }

    public function getApiAlias(): string
    {
        return 'cart_delivery_date';
    }

    private static function create(string $interval): \DateTime
    {
// detect the latest delivery date             $earliest = $max->getEarliest() > $date->getEarliest() ? $max->getEarliest() : $date->getEarliest();

            $latest = $max->getLatest() > $date->getLatest() ? $max->getLatest() : $date->getLatest();

            // if earliest and latest is same date, add one day buffer             if ($earliest->format('Y-m-d') === $latest->format('Y-m-d')) {
                $latest = $latest->add(new \DateInterval('P1D'));
            }

            $max = new DeliveryDate($earliest$latest);
        }

        return $max;
    }

    private function buildPositions(
        LineItemCollection $items,
        DeliveryPositionCollection $positions,
        ?DeliveryTime $default
    ): void {
        foreach ($items as $item) {
            
$originalCart = new Cart('original');
        $deliveryTime = $this->generateDeliveryTimeDummy();

        $shippingMethod = new ShippingMethodEntity();
        $shippingMethod->setId('1');
        $shippingMethod->setName('Express');
        $shippingMethod->addTranslated('name', 'Express');
        $shippingMethod->setDeliveryTime($deliveryTime);
        $shippingMethod->setAvailabilityRuleId(Uuid::randomHex());
        $shippingMethod->setTaxType(ShippingMethodEntity::TAX_TYPE_AUTO);
        $deliveryDate = new DeliveryDate(new \DateTime()new \DateTime());

        $delivery = new Delivery(
            new DeliveryPositionCollection(),
            $deliveryDate,
            $shippingMethod,
            new ShippingLocation(new CountryEntity(), null, null),
            new CalculatedPrice(10, 10, new CalculatedTaxCollection()new TaxRuleCollection())
        );

        $originalCart->setDeliveries(new DeliveryCollection([$delivery]));

        
$lineItem->setPrice($calculatedPrice);
        $cart->add($lineItem);

        $deliveries = new DeliveryCollection([
            new Delivery(
                new DeliveryPositionCollection([
                    new DeliveryPosition(
                        $this->ids->get('delivery-position-1'),
                        $lineItem,
                        1,
                        clone $calculatedPrice,
                        new DeliveryDate(new \DateTimeImmutable()new \DateTimeImmutable())
                    ),
                ]),
                new DeliveryDate(new \DateTimeImmutable()new \DateTimeImmutable()),
                new ShippingMethodEntity(),
                new ShippingLocation(new CountryEntity()new CountryStateEntity(), null),
                clone $calculatedPrice
            ),
        ]);

        $cart->addDeliveries($deliveries);

        
private function createCartDummy(): Cart
    {
        $lineItemCollection = new LineItemCollection([
            $this->createLineItemWithDeliveryInfo(false, 3, 10, 40, 3, 0.5),
            $this->createLineItemWithDeliveryInfo(true, 3, 10, 40, 3, 0.5),
        ]);

        $cart = $this->createCart($lineItemCollection);

        $deliveryPositionCollection = new DeliveryPositionCollection();
        $calculatedPrice = new CalculatedPrice(1.0, 1.0, new CalculatedTaxCollection()new TaxRuleCollection());
        $deliveryDate = new DeliveryDate(new \DateTimeImmutable('now')new \DateTimeImmutable('now'));

        foreach ($cart->getLineItems() as $lineItem) {
            $deliveryPositionCollection->add(new DeliveryPosition(
                Uuid::randomHex(),
                $lineItem,
                $lineItem->getQuantity(),
                $calculatedPrice,
                $deliveryDate
            ));
        }

        
$lineItem = $this->getLineItem(10, 2);

        /** @var CalculatedPrice $price */
        $price = $lineItem->getPrice();
        $deliveryPositionCollection->add(
            new DeliveryPosition(
                Uuid::randomHex(),
                $lineItem,
                $lineItem->getQuantity(),
                $price,
                new DeliveryDate(new \DateTimeImmutable('2020-01-01')new \DateTimeImmutable('2020-01-01')),
            )
        );

        static::assertEquals(20, $deliveryPositionCollection->getWeight());
    }

    public function testCalculateWithMultipleLineItems(): void
    {
        $deliveryPositionCollection = new DeliveryPositionCollection();

        $lineItem1 = $this->getLineItem(10, 1);
        
public function testValidateWithoutRules(): void
    {
        $deliveryTime = $this->generateDeliveryTimeDummy();

        $cart = new Cart('test');
        $context = $this->createMock(SalesChannelContext::class);
        $shippingMethod = new ShippingMethodEntity();
        $shippingMethod->setId('1');
        $shippingMethod->setAvailabilityRuleId('1');
        $shippingMethod->setDeliveryTime($deliveryTime);
        $shippingMethod->setActive(true);
        $deliveryDate = new DeliveryDate(new \DateTime()new \DateTime());
        $delivery = new Delivery(
            new DeliveryPositionCollection(),
            $deliveryDate,
            $shippingMethod,
            new ShippingLocation(new CountryEntity(), null, null),
            new CalculatedPrice(5, 5, new CalculatedTaxCollection()new TaxRuleCollection())
        );
        $cart->setDeliveries(new DeliveryCollection([$delivery]));
        $context->expects(static::once())->method('getRuleIds')->willReturn(['1']);

        $validator = new DeliveryValidator();
        
$price = $lineItem->getPrice();
        static::assertNotNull($price);

        $delivery->expects(static::once())->method('getPositions')->willReturn(
            new DeliveryPositionCollection(
                [
                    new DeliveryPosition(
                        Uuid::randomHex(),
                        $lineItem,
                        1,
                        $price,
                        new DeliveryDate(new \DateTime()new \DateTime())
                    ),
                ]
            )
        );

        $data = new CartDataCollection();

        $cart = new Cart('test');
        $cartBehavior = new CartBehavior([
            DeliveryProcessor::SKIP_DELIVERY_PRICE_RECALCULATION => true,
        ]);
        
$context = $this->createMock(SalesChannelContext::class);
        $context->method('getContext')->willReturn(Context::createDefaultContext());
        $context->method('getItemRounding')->willReturn(new CashRoundingConfig(2, 0.01, true));

        $positions = new DeliveryPositionCollection();
        $positions->add(
            new DeliveryPosition(
                Uuid::randomHex(),
                new LineItem('test', 'test'),
                1,
                new CalculatedPrice(0, 0, new CalculatedTaxCollection()new TaxRuleCollection()),
                new DeliveryDate(new \DateTime()new \DateTime())
            )
        );

        $shippingMethod = new ShippingMethodEntity();
        $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())
        );
private static function itemDelivery(LineItem $lineItem): DeliveryCollection
    {
        $delivery = new Delivery(
            new DeliveryPositionCollection(
                [
                    new DeliveryPosition(
                        $lineItem->getId(),
                        $lineItem,
                        1,
                        new CalculatedPrice(1.0, 1.0, new CalculatedTaxCollection()new TaxRuleCollection()),
                        new DeliveryDate(new \DateTimeImmutable()new \DateTimeImmutable())
                    ),
                ]
            ),
            new DeliveryDate(new \DateTimeImmutable()new \DateTimeImmutable()),
            new ShippingMethodEntity(),
            new ShippingLocation(new CountryEntity(), null, null),
            new CalculatedPrice(1.0, 1.0, new CalculatedTaxCollection()new TaxRuleCollection())
        );

        return new DeliveryCollection([$delivery]);
    }
}
$event = new SalesChannelContextAssembledEvent($order$salesChannelContext);
        $this->eventDispatcher->dispatch($event);

        return $salesChannelContext;
    }

    private function convertDeliveries(OrderDeliveryCollection $orderDeliveries, LineItemCollection $lineItems): DeliveryCollection
    {
        $cartDeliveries = new DeliveryCollection();

        foreach ($orderDeliveries as $orderDelivery) {
            $deliveryDate = new DeliveryDate(
                $orderDelivery->getShippingDateEarliest(),
                $orderDelivery->getShippingDateLatest()
            );

            $deliveryPositions = new DeliveryPositionCollection();

            if ($orderDelivery->getPositions() === null) {
                continue;
            }

            foreach ($orderDelivery->getPositions() as $position) {
                


    private function createCartDummyWithShippingCosts(CalculatedPrice $calculatedPrice): Cart
    {
        $lineItemCollection = new LineItemCollection([
            $this->createLineItemWithDeliveryInfo(false, 3, 10, 40, 3, 0.5),
            $this->createLineItemWithDeliveryInfo(true, 3, 10, 40, 3, 0.5),
        ]);

        $cart = $this->createCart($lineItemCollection);
        $deliveryPositionCollection = new DeliveryPositionCollection();
        $deliveryDate = new DeliveryDate(new \DateTimeImmutable('now')new \DateTimeImmutable('now'));

        foreach ($cart->getLineItems() as $lineItem) {
            $deliveryPositionCollection->add(new DeliveryPosition(
                Uuid::randomHex(),
                $lineItem,
                $lineItem->getQuantity(),
                $calculatedPrice,
                $deliveryDate
            ));
        }

        
$shippingMethod = new ShippingMethodEntity();
        $shippingMethod->setId('shipping-method-id');

        $shippingLocation = new ShippingLocation($country, null, null);
        if (!$withoutAddress) {
            $shippingLocation = new ShippingLocation($country$countryState$this->getCustomerAddress());
        }

        $deliveryCollection = new DeliveryCollection();
        $delivery = new Delivery(
            new DeliveryPositionCollection(),
            new DeliveryDate(new \DateTimeImmutable()new \DateTimeImmutable()),
            $shippingMethod,
            $shippingLocation,
            new CalculatedPrice(1, 1, new CalculatedTaxCollection()new TaxRuleCollection())
        );
        $deliveryCollection->add($delivery);

        return $deliveryCollection;
    }

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