getQuantity example

$calculatedPrice = $calculator->calculate(
            $calculation->getDiscount(),
            $calculation->getPrices(),
            Generator::createSalesChannelContext()
        );

        static::assertEquals($calculation->getExpected()->getCalculatedTaxes()$calculatedPrice->getCalculatedTaxes());
        static::assertEquals($calculation->getExpected()->getTaxRules()$calculatedPrice->getTaxRules());
        static::assertEquals($calculation->getExpected()->getTotalPrice()$calculatedPrice->getTotalPrice());
        static::assertEquals($calculation->getExpected()->getUnitPrice()$calculatedPrice->getUnitPrice());
        static::assertEquals($calculation->getExpected()->getQuantity()$calculatedPrice->getQuantity());
    }

    /** * @return array<string, list<AbsoluteCalculation>> */
    public static function provider(): array
    {
        return [
            'small-discounts' => [self::getSmallDiscountCase()],
            '100%' => [self::getOneHundredPercentageDiscountCase()],
        ];
    }


        if ($definition instanceof PercentagePriceDefinition) {
            // reduce line items for provided filter             $prices = $this->filterLineItems($calculated$definition->getFilter()$context)
                ->getPrices();

            return $this->percentagePriceCalculator->calculate($definition->getPercentage()$prices$context);
        }

        if ($definition instanceof QuantityPriceDefinition) {
            $definition->setQuantity($lineItem->getQuantity());

            return $this->quantityPriceCalculator->calculate($definition$context);
        }

        throw CartException::missingLineItemPrice($lineItem->getId());
    }
}
$item2 = new LineItemQuantity('B', 3);
        $item3 = new LineItemQuantity('C', 1);
        $item4 = new LineItemQuantity('A', 5);
        $item5 = new LineItemQuantity('B', 2);

        $collection = new LineItemQuantityCollection([$item1$item2$item3$item4$item5]);

        $collection->compress();

        static::assertCount(3, $collection);

        static::assertEquals(7, $collection->getElements()[0]->getQuantity());
        static::assertEquals(5, $collection->getElements()[1]->getQuantity());
        static::assertEquals(1, $collection->getElements()[2]->getQuantity());
    }
}
public function add($lineItem): void
    {
        $this->validateType($lineItem);

        $exists = $this->get($lineItem->getId());

        if ($exists && $exists->getType() !== $lineItem->getType()) {
            throw CartException::mixedLineItemType($lineItem->getId()$lineItem->getType());
        }

        if ($exists) {
            $exists->setQuantity($lineItem->getQuantity() + $exists->getQuantity());

            return;
        }

        $this->elements[$this->getKey($lineItem)] = $lineItem;
    }

    /** * @param int|string $key * @param LineItem $lineItem */
    
$stubs = $this->createMock(ScriptPriceStubs::class);
        $helper = $this->createMock(CartFacadeHelper::class);
        $context = $this->createMock(SalesChannelContext::class);

        $facade = new ItemFacade($item$stubs$helper$context);

        static::assertEquals('foo', $facade->getId());
        static::assertEquals('type', $facade->getType());
        static::assertEquals('reference', $facade->getReferencedId());
        static::assertEquals('label', $facade->getLabel());
        static::assertEquals(2, $facade->getQuantity());

        static::assertInstanceOf(PriceFacade::class$facade->getPrice());
        static::assertEquals(10, $facade->getPrice()->getUnit());
        static::assertEquals(10, $facade->getPrice()->getTotal());

        static::assertEquals('bar', $facade->getPayload()->offsetGet('foo'));
        // @phpstan-ignore-next-line         static::assertEquals('bar', $facade->getPayload()['foo']);
        static::assertEquals('nested', $facade->getPayload()->offsetGet('nested')['foo']);

        // @phpstan-ignore-next-line


        $id = $productPriceGroup->getId();
        if (!isset($priceGroups[$id])) {
            return null;
        }

        $priceGroup = $priceGroups[$id];

        $highest = null;
        foreach ($priceGroup->getDiscounts() as $discount) {
            if ($discount->getQuantity() > $quantity && !$this->config->get('useLastGraduationForCheapestPrice')) {
                continue;
            }

            if (!$highest) {
                $highest = $discount;
                continue;
            }

            if ($highest->getPercent() < $discount->getPercent()) {
                $highest = $discount;
            }
        }

        $deliveryPositionCollection = new DeliveryPositionCollection();

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

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

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

        $lineItem->setDeliveryInformation(new DeliveryInformation($lineItem->getQuantity(), 0, false));
    }
}
$newPackages = [];

        foreach ($packages as $package) {
            $foundItems = [];

            foreach ($package->getMetaData() as $item) {
                $lineItem = $package->getCartItem($item->getLineItemId());

                if ($this->isRulesFilterValid($lineItem$priceDefinition$context)) {
                    $item = new LineItemQuantity(
                        $lineItem->getId(),
                        $lineItem->getQuantity()
                    );

                    $foundItems[] = $item;
                }
            }

            if (\count($foundItems) > 0) {
                $newPackages[] = new DiscountPackage(new LineItemQuantityCollection($foundItems));
            }
        }

        
private function lineItemMatches(LineItem $lineItem): bool
    {
        if ($lineItem->getReferencedId() !== $this->id && $lineItem->getPayloadValue('parentId') !== $this->id) {
            return false;
        }

        if ($this->quantity === null) {
            return true;
        }

        return RuleComparison::numeric($lineItem->getQuantity()$this->quantity, $this->operator);
    }
}
$total = $this->results[$key]['total'];

        foreach ($group->getItems() as $tuple) {
            // either create new entries             // or just increase the quantity of an existing entry in             // the result set of our group definition.             if (!\array_key_exists($tuple->getLineItemId()$total)) {
                // add as new entry to avoid pointer references                 // to our single groups list                 $total[$tuple->getLineItemId()] = new LineItemQuantity(
                    $tuple->getLineItemId(),
                    $tuple->getQuantity()
                );
            } else {
                $package = $total[$tuple->getLineItemId()];
                $package->setQuantity($package->getQuantity() + $tuple->getQuantity());
            }
        }

        $this->results[$key]['total'] = $total;
    }
}

    private function buildDiscountGraduations(
        PriceRule $reference,
        Group $customerGroup,
        array $discounts
    ): array {
        $prices = [];

        $firstDiscount = $discounts[0];

        $previous = null;
        if ($firstDiscount->getQuantity() > 1) {
            $firstGraduation = clone $reference;
            $previous = $firstGraduation;

            $prices[] = $firstGraduation;
        }

        foreach ($discounts as $discount) {
            $rule = clone $reference;

            $percent = (100 - $discount->getPercent()) / 100;

            


    /** * @throws CartException */
    private function refreshChildQuantity(
        LineItemCollection $lineItems,
        int $oldParentQuantity,
        int $newParentQuantity
    ): void {
        foreach ($lineItems as $lineItem) {
            $newQuantity = intdiv($lineItem->getQuantity()$oldParentQuantity) * $newParentQuantity;

            if ($lineItem->hasChildren()) {
                $this->refreshChildQuantity($lineItem->getChildren()$lineItem->getQuantity()$newQuantity);
            }

            $lineItem->quantity = $newQuantity;
            $priceDefinition = $lineItem->getPriceDefinition();
            if ($priceDefinition && $priceDefinition instanceof QuantityPriceDefinition) {
                $priceDefinition->setQuantity($lineItem->getQuantity());
            }
        }
    }
$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
            ));
        }

        $cart->setDeliveries(new DeliveryCollection(
            [
                new Delivery(
                    $deliveryPositionCollection,
                    $deliveryDate,
                    new ShippingMethodEntity(),
                    
private function orderProduct(string $id, int $quantity): string
    {
        $factory = new ProductLineItemFactory(new PriceDefinitionFactory());
        $lineItem = $factory->create(['id' => $id, 'referencedId' => $id, 'quantity' => $quantity]$this->context);

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

        $cart = $this->cartService->add($cart$lineItem$this->context);

        $item = $cart->get($id);
        static::assertInstanceOf(LineItem::class$item);
        static::assertSame($quantity$item->getQuantity());

        return $this->cartService->order($cart$this->context, new RequestDataBag());
    }

    private function transitionOrder(string $orderId, string $transition): void
    {
        $registry = $this->getContainer()->get(StateMachineRegistry::class);
        $transitionObject = new Transition('order', $orderId$transition, 'stateId');

        $registry->transition($transitionObject, Context::createDefaultContext());
    }
}
Home | Imprint | This part of the site doesn't use cookies.