getPositions example

/** * add a discount delivery item to DeliveryCollection $deliveries */
    private function addDiscountDeliveryItem(DeliveryCollection $deliveries, Delivery $delivery, CalculatedPrice $price): void
    {
        if ($price->getTotalPrice() >= 0) {
            return;
        }

        $delivery = new Delivery(
            $delivery->getPositions(),
            $delivery->getDeliveryDate(),
            $delivery->getShippingMethod(),
            $delivery->getLocation(),
            $price
        );

        $deliveries->add($delivery);
    }

    /** * if we have a discount with scope delivery we add a lineItem in cart with price 0 * * @throws CartException */
public function cleanupCart(Cart $cart): void
    {
        $customFieldAllowList = $this->connection->fetchFirstColumn('SELECT `name` FROM `custom_field` WHERE `allow_cart_expose` = 1;');

        $event = new CartBeforeSerializationEvent($cart$customFieldAllowList);
        $this->eventDispatcher->dispatch($event);

        $this->cleanupLineItems($cart->getLineItems()$event->getCustomFieldAllowList());

        foreach ($cart->getDeliveries() as $delivery) {
            $this->cleanupLineItems($delivery->getPositions()->getLineItems()$event->getCustomFieldAllowList());
        }
    }

    /** * @param array<mixed> $customFieldAllowList */
    private function cleanupLineItems(LineItemCollection $lineItems, array $customFieldAllowList): void
    {
        foreach ($lineItems as $lineItem) {
            $this->cleanupLineItem($lineItem$customFieldAllowList);
        }
    }
/** * @param array<string, CalculatedTaxCollection>|null $taxes */
    private function applyDeliveryTaxes(DeliveryCollection $deliveries, ?array $taxes): void
    {
        if (!$taxes) {
            return;
        }

        foreach ($taxes as $deliveryPositionId => $deliveryTax) {
            foreach ($deliveries as $delivery) {
                $position = $delivery->getPositions()->get($deliveryPositionId);

                if (!$position) {
                    continue;
                }

                $position->getPrice()->setCalculatedTaxes($deliveryTax);
                $delivery->getShippingCosts()->setCalculatedTaxes($deliveryTax);
            }
        }
    }
}
/** @var Delivery $delivery */
        $delivery = $cart->getDeliveries()->first();
        if (!$delivery) {
            return;
        }

        $calculatedPrice = $item->getPrice();
        \assert($calculatedPrice instanceof CalculatedPrice);

        $position = new DeliveryPosition($item->getId()clone $item$item->getQuantity()$calculatedPrice$delivery->getDeliveryDate());

        $delivery->getPositions()->add($position);
    }

    private function fetchOrder(string $orderId, Context $context): ?OrderEntity
    {
        $criteria = (new Criteria([$orderId]))
            ->addAssociation('lineItems.downloads')
            ->addAssociation('transactions.stateMachineState')
            ->addAssociation('deliveries.shippingMethod')
            ->addAssociation('deliveries.positions.orderLineItem')
            ->addAssociation('deliveries.shippingOrderAddress.country')
            ->addAssociation('deliveries.shippingOrderAddress.countryState');

        
$cleaner = new CartSerializationCleaner($connection$dispatcher);
        $cleaner->cleanupCart($cart);

        $items = $cart->getLineItems()->getFlat();
        foreach ($items as $item) {
            static::assertArrayHasKey($item->getId()$payloads);
            static::assertEquals($payloads[$item->getId()]$item->getPayload());
        }

        $delivery = $cart->getDeliveries()->first();
        $deliveryItems = $delivery !== null ? $delivery->getPositions()->getLineItems()->getFlat() : [];

        foreach ($deliveryItems as $item) {
            static::assertArrayHasKey($item->getId()$payloads);
            static::assertEquals($payloads[$item->getId()]$item->getPayload());
        }
    }

    /** * @dataProvider cleanupCoversProvider */
    public function testLineItemCovers(Cart $cart, ?MediaEntity $expectedCover): void
    {
$this->_id = $id;
        $this->_config = $config;
        $this->_summaryNet = (bool) ($config['summaryNet'] ?? false);
        $this->_shippingCostsAsPosition = (bool) ($config['shippingCostsAsPosition'] ?? false);

        $this->getOrder();

        $this->initializeShopContext((int) $this->_order['language']);
        $this->countryGateway = Shopware()->Container()->get(CountryGatewayInterface::class);
        $this->structConverter = Shopware()->Container()->get(LegacyStructConverter::class);

        $this->getPositions();

        $this->getUser();
        $this->getBilling();
        $this->getShipping();
        $this->getDispatch();
        $this->getPayment();
        $this->getPaymentInstances();

        $this->processPositions();
        $this->processOrder();
    }

    
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(),
                $context,
                $manualShippingCost
            );

            $delivery->setShippingCosts($costs);

            return;
        }

        if (
            $this->hasDeliveryPriceRecalculationSkipWithZeroUnitPrice($cart->getBehavior()$delivery->getShippingCosts()->getUnitPrice())
            
'shippingDateEarliest' => $delivery->getDeliveryDate()->getEarliest()->format(Defaults::STORAGE_DATE_TIME_FORMAT),
            'shippingDateLatest' => $delivery->getDeliveryDate()->getLatest()->format(Defaults::STORAGE_DATE_TIME_FORMAT),
            'shippingMethodId' => $delivery->getShippingMethod()->getId(),
            'shippingOrderAddress' => $shippingAddress,
            'shippingCosts' => $delivery->getShippingCosts(),
            'positions' => [],
            'stateId' => $stateId,
        ];

        $deliveryData = array_filter($deliveryDatafn ($item) => $item !== null);

        foreach ($delivery->getPositions() as $position) {
            $deliveryData['positions'][] = [
                'id' => self::getId($position),
                'price' => $position->getPrice(),
                'orderLineItemId' => $lineItems[$position->getIdentifier()]['id'],
                'orderLineItemVersionId' => $context->getVersionId(),
            ];
        }

        return $deliveryData;
    }

    

    private function loadBasketPlugins(BasketStruct $basket, array $positions): void
    {
        $context = new PluginsByTechnicalNameRequest(
            $this->getLocale(),
            $this->getVersion(),
            array_column($positions, 'technicalName')
        );

        $plugins = $this->get(PluginStoreService::class)->getPlugins($context);

        foreach ($basket->getPositions() as $position) {
            $name = $this->getTechnicalNameOfOrderNumber($position->getOrderNumber()$positions);

            if ($name === null) {
                continue;
            }

            $key = strtolower($name);
            $position->setPlugin($plugins[$key]);
        }
    }

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

    public function testIndependenceOfLineItemAndDeliveryPositionPrices(): void
    {
        $cart = $this->createCart();
        $firstDelivery = $this->getDeliveries($cart)->first();
        static::assertNotNull($firstDelivery);

        $firstPosition = $firstDelivery->getPositions()->first();

        static::assertNotSame($firstPosition?->getPrice()$cart->getLineItems()->first()?->getPrice());
    }

    public function testEmptyCart(): void
    {
        $cart = $this->createCart(true);
        $deliveries = $this->getDeliveries($cart);

        static::assertCount(0, $deliveries);
    }

    


            return $delivery;
        }

        return null;
    }

    public function contains(LineItem $item): bool
    {
        foreach ($this->getIterator() as $delivery) {
            if ($delivery->getPositions()->has($item->getId())) {
                return true;
            }
        }

        return false;
    }

    public function getShippingCosts(): PriceCollection
    {
        return new PriceCollection(
            $this->map(fn (Delivery $delivery) => $delivery->getShippingCosts())
        );
return (new RuleConfig())
            ->operatorSet(RuleConfig::OPERATOR_SET_NUMBER)
            ->numberField('volume', ['unit' => RuleConfig::UNIT_VOLUME]);
    }

    private function calculateCartVolume(Cart $cart): float
    {
        $volume = 0.0;

        foreach ($cart->getDeliveries() as $delivery) {
            if ($delivery instanceof Delivery) {
                $volume += $delivery->getPositions()->getVolume();
            }
        }

        return $volume;
    }
}
$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());
        }

        
return;
        }

        $sendTime = Shopware()->Config()->get('voteCallingTime', 10);
        $orders = $this->getOrders($sendTime);
        if (empty($orders)) {
            return 'No orders for rating mail found.';
        }

        $orderIds = array_keys($orders);
        $customers = $this->getCustomers($orderIds);
        $orderPositions = $this->getPositions($orderIds);

        $shopPositionBaseProducts = $this->structurePositionsArray($orderPositions);

        $shopPositionImages = $this->getPositionImages($shopPositionBaseProducts);

        $count = 0;
        foreach ($orders as $orderId => $order) {
            if (empty($customers[$orderId]['email']) || empty($orderPositions[$orderId])) {
                continue;
            }

            

            ]);
        }

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

            $deliveries[$positionId] = new CalculatedTaxCollection([
                new CalculatedTax(
                    $delivery->getShippingCosts()->getTotalPrice() * $this->taxRate,
                    
Home | Imprint | This part of the site doesn't use cookies.