isStackable example

->create([$product]$context->getContext());
        $this->addTaxDataToSalesChannel($context$product['tax']);

        $lineItem = (new ProductLineItemFactory(new PriceDefinitionFactory()))->create(['id' => $productId, 'referencedId' => $productId]$context);
        $cart = $cartService->getCart($context->getToken()$context);
        $cart = $cartService->add($cart$lineItem$context);

        $lineItem = $cart->getLineItems()->get($productId);

        static::assertInstanceOf(LineItem::class$lineItem);
        static::assertEquals(1, $lineItem->getQuantity());
        static::assertTrue($lineItem->isStackable());
        static::assertTrue($lineItem->isRemovable());

        $cart = $cartService->update($cart['foo' => [
            'id' => $productId,
            'quantity' => 20,
            'payload' => ['foo' => 'bar'],
            'stackable' => false,
            'removable' => false,
        ]]$context);

        static::assertEquals(20, $lineItem->getQuantity());
        
'id' => $id,
            'identifier' => $lineItem->getId(),
            'productId' => $productId,
            'promotionId' => $promotionId,
            'referencedId' => $lineItem->getReferencedId(),
            'quantity' => $lineItem->getQuantity(),
            'type' => $lineItem->getType(),
            'label' => $lineItem->getLabel(),
            'description' => $lineItem->getDescription(),
            'good' => $lineItem->isGood(),
            'removable' => $lineItem->isRemovable(),
            'stackable' => $lineItem->isStackable(),
            'position' => $position,
            'price' => $lineItem->getPrice(),
            'priceDefinition' => $definition,
            'parentId' => $parentId,
            'coverId' => $lineItem->getCover() ? $lineItem->getCover()->getId() : null,
            'payload' => $lineItem->getPayload(),
            'states' => $lineItem->getStates(),
        ];

        $downloads = $lineItem->getExtensionOfType(OrderConverter::ORIGINAL_DOWNLOADS, OrderLineItemDownloadCollection::class);
        if ($downloads instanceof OrderLineItemDownloadCollection) {
            


        if (!$customerContext->getDomainId()) {
            $customerContext->setDomainId($currentContext->getDomainId());
        }

        return $this->enrichCustomerContext($customerContext$currentContext$currentContext->getToken()$customerId);
    }

    private function mergeCart(Cart $customerCart, Cart $guestCart, SalesChannelContext $customerContext): Cart
    {
        $mergeableLineItems = $guestCart->getLineItems()->filter(fn (LineItem $item) => ($item->getQuantity() > 0 && $item->isStackable()) || !$customerCart->has($item->getId()));

        $this->eventDispatcher->dispatch(new BeforeCartMergeEvent(
            $customerCart,
            $guestCart,
            $mergeableLineItems,
            $customerContext
        ));

        $errors = $customerCart->getErrors();
        $customerCart->setErrors(new ErrorCollection());

        
return $newRestOfCart;
    }

    /** * @throws CartException */
    private function splitQuantities(LineItemCollection $cartItems, SalesChannelContext $context): LineItemFlatCollection
    {
        $items = [];

        foreach ($cartItems as $item) {
            $isStackable = $item->isStackable();

            $item->setStackable(true);

            for ($i = 1; $i <= $item->getQuantity(); ++$i) {
                $tmpItem = $this->quantitySplitter->split($item, 1, $context);

                $items[] = $tmpItem;
            }

            $item->setStackable($isStackable);
        }

        

    public function take(int $quantity, ?string $key = null): ?ItemFacade
    {
        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);

        


    /** * @throws CartException */
    public function setQuantity(int $quantity): self
    {
        if ($quantity < 1) {
            throw CartException::invalidQuantity($quantity);
        }

        if (!$this->isStackable()) {
            throw CartException::lineItemNotStackable($this->id);
        }

        if ($this->hasChildren()) {
            $this->refreshChildQuantity($this->children, $this->quantity, $quantity);
        }

        if ($this->priceDefinition instanceof QuantityPriceDefinition) {
            $this->price = null;
        }

        
public function getDecorated(): DiscountPackager
    {
        throw new DecorationPatternException(self::class);
    }

    /** * Gets all product line items of the entire cart that * match the rules and conditions of the provided discount item. */
    public function getMatchingItems(DiscountLineItem $discount, Cart $cart, SalesChannelContext $context): DiscountPackageCollection
    {
        $allItems = $cart->getLineItems()->filter(fn (LineItem $lineItem) => $lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE && $lineItem->isStackable());

        $priceDefinition = $discount->getPriceDefinition();
        if ($priceDefinition instanceof FilterableInterface && $priceDefinition->getFilter()) {
            $allItems = $allItems->filter(fn (LineItem $lineItem) => $priceDefinition->getFilter()->match(new LineItemScope($lineItem$context)));
        }

        $discountPackage = $this->getDiscountPackage($allItems);
        if ($discountPackage === null) {
            return new DiscountPackageCollection([]);
        }

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