setQuantity example



        return $cart;
    }

    private function getOrder(string $toManipulate = ''): OrderEntity
    {
        // Order line items         $orderLineItem = new OrderLineItemEntity();
        $orderLineItem->setIdentifier('order-line-item-identifier');
        $orderLineItem->setId('order-line-item-id');
        $orderLineItem->setQuantity(1);
        $orderLineItem->setType('order-line-item-type');
        $orderLineItem->setLabel('order-line-item-label');
        $orderLineItem->setGood(true);
        $orderLineItem->setRemovable(false);
        $orderLineItem->setStackable(true);

        if ($toManipulate === 'order-add-line-item-download') {
            $orderLineItemDownload = new OrderLineItemDownloadEntity();
            $orderLineItemDownload->setId(Uuid::randomHex());
            $orderLineItemDownload->setMediaId(Uuid::randomHex());

            
// Create Guest cart         $previousCart = new Cart($currentContextToken);

        $productId1 = $this->createProduct($currentContext->getContext());
        $productId2 = $this->createProduct($currentContext->getContext());

        $productLineItem1 = new LineItem($productId1, LineItem::PRODUCT_LINE_ITEM_TYPE, $productId1);
        $productLineItem2 = new LineItem($productId2, LineItem::PRODUCT_LINE_ITEM_TYPE, $productId2);
        $productLineItem1->setStackable(true);
        $productLineItem2->setStackable(true);
        $productLineItem1->setQuantity(1);
        $guestProductQuantity = 5;
        $productLineItem2->setQuantity($guestProductQuantity);

        $previousCart->addLineItems(new LineItemCollection([$productLineItem1$productLineItem2]));
        $previousCart->markUnmodified();

        $cartMergedEvent = new CartMergedEvent(new Cart('customerToken')$currentContext$previousCart);

        $subscriber->addCartMergedNoticeFlash($cartMergedEvent);

        static::assertNotEmpty($infoFlash = $session->getFlashBag()->get('info'));

        
            // 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;
    }
}
 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());
            }
        }
    }

    /** * @throws CartException */
    private function validateChildQuantity(LineItem $child): void
    {
        $childQuantity = $child->getQuantity();
        $parentQuantity = $this->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 */
    


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

        if (!$this->item->isStackable()) {
            return null;
        }

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

        $new = clone $this->item;
        $new->setId($key ?? Uuid::randomHex());
        $new->setQuantity($quantity);

        $this->item->setQuantity(
            $this->item->getQuantity() - $quantity
        );

        return new ItemFacade($new$this->priceStubs, $this->helper, $this->context);
    }

    /** * `getId()` returns the id of the line-item. * * @return string Returns the id. */
private function buildOrderLineItem(?string $id, string $type, int $qty): OrderLineItemEntity
    {
        $orderLineItemEntity = new OrderLineItemEntity();
        $orderLineItemEntity->setId(Uuid::randomHex());
        $orderLineItemEntity->setReferencedId($id);
        $orderLineItemEntity->setType($type);
        $orderLineItemEntity->setIdentifier($id ?? Uuid::randomHex());
        $orderLineItemEntity->setLabel(Uuid::randomHex());
        $orderLineItemEntity->setGood(true);
        $orderLineItemEntity->setRemovable(true);
        $orderLineItemEntity->setStackable(true);
        $orderLineItemEntity->setQuantity($qty);

        return $orderLineItemEntity;
    }
}
return $group;
    }

    /** * @return PriceDiscount */
    public function hydratePriceDiscount(array $data)
    {
        $discount = new PriceDiscount();
        $discount->setId((int) $data['__priceGroupDiscount_id']);
        $discount->setPercent((float) $data['__priceGroupDiscount_discount']);
        $discount->setQuantity((int) $data['__priceGroupDiscount_discountstart']);

        return $discount;
    }
}
new LineItem('A', 'type', null, -1);
    }

    /** * @throws CartException */
    public function testChangeLineItemToInvalidQuantity(): void
    {
        $this->expectException(CartException::class);

        $lineItem = new LineItem('A', 'type');
        $lineItem->setQuantity(0);
    }

    /** * @throws CartException */
    public function testChangeLineItemQuantity(): void
    {
        $lineItem = new LineItem('A', 'type');
        $lineItem->setStackable(true);
        $lineItem->setQuantity(5);
        static::assertSame(5, $lineItem->getQuantity());
    }

        $cart = $this->buildCart(0);

        $item1 = $this->createProductItem(10, 10);
        $item2 = $this->createProductItem(20, 10);
        $item3 = $this->createProductItem(50, 10);

        $item1->setReferencedId($item1->getId());
        $item2->setReferencedId($item2->getId());
        $item3->setReferencedId($item3->getId());

        $item1->setQuantity(10);
        $item2->setQuantity(10);
        $item3->setQuantity(10);

        $cart->addLineItems(new LineItemCollection([$item1$item2$item3]));

        $rules = new AndRule(
            [
                $this->getProductsRule([$item1->getReferencedId()$item2->getReferencedId()]),
            ]
        );

        
$minQuantity = 3;

        $productHighQuantity1Id = Uuid::randomBytes();
        $productHighQuantity2Id = Uuid::randomBytes();
        $productLowQuantity1Id = Uuid::randomBytes();
        $productLowQuantity2Id = Uuid::randomBytes();

        // create our test product line items         $productHighQuantityHighPrice = $this->createProductItem($minPrice, 0);
        $productHighQuantityHighPrice->setId($productHighQuantity1Id);
        $productHighQuantityHighPrice->setReferencedId($productHighQuantity1Id);
        $productHighQuantityHighPrice->setQuantity($minQuantity);

        $productHighQuantityLowPrice = $this->createProductItem($minPrice - 0.1, 0);
        $productHighQuantityLowPrice->setId($productHighQuantity2Id);
        $productHighQuantityLowPrice->setReferencedId($productHighQuantity2Id);
        $productHighQuantityLowPrice->setQuantity($minQuantity);

        $productLowQuantityHighPrice = $this->createProductItem($minPrice, 0);
        $productLowQuantityHighPrice->setId($productLowQuantity1Id);
        $productLowQuantityHighPrice->setReferencedId($productLowQuantity1Id);
        $productLowQuantityHighPrice->setQuantity($minQuantity - 1);

        
        $basketData = $this->db->fetchAll(
            'SELECT id, modus, quantity FROM s_order_basket WHERE sessionID = ?',
            [$this->session->get('sessionId')]
        );
        $cartItems = [];
        foreach ($basketData as $basketContent) {
            if (empty($basketContent['modus'])) {
                $cartItem = new CartItemStruct();
                $cartItem->setId((int) $basketContent['id']);
                $cartItem->setQuantity((int) $basketContent['quantity']);
                $cartItems[] = $cartItem;
            }
        }
        $this->updateCartItems($cartItems);

        // Check, if we have some free products for the client         $this->sInsertPremium();

        // Delete previous given discounts         $premiumShipping = $this->config->get('sPREMIUMSHIPPIUNG');
        if (empty($premiumShipping)) {
            
public function update(LineItem $lineItem, array $data, SalesChannelContext $context): void
    {
        if (isset($data['referencedId'])) {
            $lineItem->setReferencedId($data['referencedId']);
        }

        if (isset($data['payload'])) {
            $lineItem->setPayload($data['payload'] ?? []);
        }

        if (isset($data['quantity'])) {
            $lineItem->setQuantity((int) $data['quantity']);
        }

        if (isset($data['priceDefinition']) && !$context->hasPermission(ProductCartProcessor::ALLOW_PRODUCT_PRICE_OVERWRITES)) {
            throw CartException::insufficientPermission();
        }

        if (isset($data['priceDefinition'])) {
            $lineItem->addExtension(ProductCartProcessor::CUSTOM_PRICE, new ArrayEntity());
            $lineItem->setPriceDefinition($this->priceDefinitionFactory->factory($context->getContext()$data['priceDefinition']$data['type']));
        }
    }
}
private function buildOrderLineItemEntity(string $id, string $type, ?string $parentId, int $quantity = 1): OrderLineItemEntity
    {
        $orderLineItemEntity = new OrderLineItemEntity();
        $orderLineItemEntity->setId($id);
        $orderLineItemEntity->setType($type);
        $orderLineItemEntity->setPosition($this->position === 1 ? $this->position : ++$this->position);
        $orderLineItemEntity->setIdentifier($id);
        $orderLineItemEntity->setLabel(Uuid::randomHex());
        $orderLineItemEntity->setGood(true);
        $orderLineItemEntity->setRemovable(true);
        $orderLineItemEntity->setStackable(false);
        $orderLineItemEntity->setQuantity($quantity);

        if ($parentId === null) {
            return $orderLineItemEntity;
        }

        $orderLineItemEntity->setParentId($parentId);

        return $orderLineItemEntity;
    }
}
Home | Imprint | This part of the site doesn't use cookies.