setStackable example

100,
                100.0,
                false,
                null,
                $this->deliveryTime,
                10,
                10,
                1,
            )
        );
        $lineItem1->setPrice(new CalculatedPrice($price$pricenew CalculatedTaxCollection()new TaxRuleCollection()));
        $lineItem1->setStackable(true);
        $lineItem1->setQuantity($quantity);

        $lineItem2 = new LineItem(Uuid::randomHex(), 'product');
        $lineItem2->setDeliveryInformation(
            new DeliveryInformation(
                100,
                100.0,
                true,
                null,
                $this->deliveryTime,
                10,
                
// @phpstan-ignore-next-line         static::assertEquals('nested', $facade->getPayload()['nested']['foo']);

        static::assertCount(0, $facade->getChildren());
    }

    public function testICanTakeItems(): void
    {
        $item = new LineItem('foo', 'type', 'reference', 5);
        $item->setLabel('label');
        $item->setStackable(true);

        $stubs = $this->createMock(ScriptPriceStubs::class);
        $helper = $this->createMock(CartFacadeHelper::class);
        $context = $this->createMock(SalesChannelContext::class);

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

        $new = $facade->take(2);

        static::assertInstanceOf(ItemFacade::class$new);

        
static::assertCount(1, $facade);
        static::assertTrue($facade->has('item-1'));
        static::assertInstanceOf(ItemFacade::class$facade->get('item-1'));

        $facade->remove('item-1');

        static::assertCount(0, $facade);
        static::assertFalse($facade->has('item-1'));
        static::assertNull($facade->get('item-1'));

        $facade->add(
            $this->item((new LineItem('duplicate', 'item', 'reference'))->setStackable(true))
        );
        $facade->add(
            $this->item((new LineItem('duplicate', 'item', 'reference'))->setStackable(true))
        );

        static::assertEquals(1, $facade->count());
        static::assertTrue($facade->has('duplicate'));
        static::assertInstanceOf(ItemFacade::class$facade->get('duplicate'));
        static::assertEquals(2, $facade->get('duplicate')->getQuantity());

        static::assertTrue(
            
static::assertSame('test', $lineItem->getReferencedId());
        static::assertSame(LineItem::PRODUCT_LINE_ITEM_TYPE, $lineItem->getType());
        static::assertSame(1, $lineItem->getQuantity());
        static::assertInstanceOf(QuantityPriceDefinition::class$lineItem->getPriceDefinition());
        static::assertSame(100.0, $lineItem->getPriceDefinition()->getPrice());
    }

    public function testUpdateDisabledStackable(): void
    {
        $id = Uuid::randomHex();
        $lineItem = new LineItem($id, LineItem::PRODUCT_LINE_ITEM_TYPE, Uuid::randomHex(), 1);
        $lineItem->setStackable(false);

        $cart = new Cart('test');
        $cart->add($lineItem);

        $this->expectException(CartException::class);

        $this->service->update($cart['id' => $id, 'quantity' => 2]$this->context);
    }

    public function testChangeQuantity(): void
    {
        

        $cart = new Cart('test');
        static::assertCount(0, $cart->getLineItems()->filterGoods());
    }

    public function testCartWithLineItemsHasGoods(): void
    {
        $cart = new Cart('test');
        $cart->add(
            (new LineItem('A', 'test'))
                ->setGood(true)
                ->setStackable(true)
        );
        $cart->add(
            (new LineItem('A', 'test'))
                ->setGood(false)
                ->setStackable(true)
        );

        static::assertCount(1, $cart->getLineItems()->filterGoods());
    }

    public function testCartHasNoGoodsIfNoLineItemDefinedAsGoods(): void
    {
        if ($packages->count() <= 0) {
            return new DiscountCalculatorResult(
                new CalculatedPrice(0, 0, new CalculatedTaxCollection()new TaxRuleCollection(), 1),
                []
            );
        }

        // remember our initial package count         $originalPackageCount = $packages->count();

        foreach ($calculatedCart->getLineItems() as $item) {
            $item->setStackable(true);
            $this->splitted[$item->getId()] = $this->lineItemQuantitySplitter->split($item, 1, $context);
        }

        $packages = $this->enrichPackagesWithCartData($packages$calculatedCart$context);

        // every scope packager can have an additional         // list of rules that can be used to filter out items.         // thus we enrich our current package with items         // and run it through the advanced rules if existing         if ($discount->getScope() !== PromotionDiscountEntity::SCOPE_SETGROUP) {
            $packages = $this->advancedRules->filter($discount$packages$context);
        }
$packages = $packager->getMatchingItems($discount$cart$context);

        $ids = $packages->first()?->getMetaData()->map(fn (LineItemQuantity $item) => $item->getLineItemId());

        static::assertEquals($expected$ids);
    }

    public static function buildPackagesProvider(): \Generator
    {
        $stackable = new LineItem('stackable', LineItem::PRODUCT_LINE_ITEM_TYPE, null, 1);
        $stackable->setPrice(new CalculatedPrice(100, 100, new CalculatedTaxCollection()new TaxRuleCollection()));
        $stackable->setStackable(true);

        $other = new LineItem('other', LineItem::PRODUCT_LINE_ITEM_TYPE, null, 2);
        $other->setPrice(new CalculatedPrice(100, 100, new CalculatedTaxCollection()new TaxRuleCollection()));
        $other->setStackable(true);

        $none = new LineItem('none', LineItem::PRODUCT_LINE_ITEM_TYPE, null, 1);
        $none->setPrice(new CalculatedPrice(100, 100, new CalculatedTaxCollection()new TaxRuleCollection()));
        $none->setStackable(false);

        $credit = new LineItem('credit', LineItem::CREDIT_LINE_ITEM_TYPE, null, 1);
        $credit->setPrice(new CalculatedPrice(100, 100, new CalculatedTaxCollection()new TaxRuleCollection()));
        
#[Package('checkout')] trait PromotionLineItemTestFixtureBehaviour
{
    /** * Create a simple product line item with the provided price. */
    private function createProductItem(float $price, float $taxRate): LineItem
    {
        $product = new LineItem(Uuid::randomBytes(), LineItem::PRODUCT_LINE_ITEM_TYPE);

        // allow quantity change         $product->setStackable(true);

        $taxValue = $price * ($taxRate / 100.0);

        $calculatedTaxes = new CalculatedTaxCollection();
        $calculatedTaxes->add(new CalculatedTax($taxValue$taxRate$taxValue));

        $product->setPrice(new CalculatedPrice($price$price$calculatedTaxesnew TaxRuleCollection()));

        return $product;
    }
}
public function testUpdateWithMissingLineItem(): void
    {
        $this->expectException(CartException::class);
        $this->service->update(new Cart('test')['id' => Uuid::randomHex(), 'quantity' => 2]$this->context);
    }

    public function testUpdateLineItem(): void
    {
        $id = Uuid::randomHex();
        $lineItem = new LineItem($id, LineItem::PRODUCT_LINE_ITEM_TYPE, Uuid::randomHex(), 1);
        $lineItem->setStackable(true);

        $cart = new Cart('test');

        $this->factory->expects(static::once())->method('supports')->with('product')->willReturn(true);
        $this->eventDispatcher->expects(static::never())->method('dispatch');
        $this->factory->expects(static::once())->method('update')->with($lineItem['id' => $id, 'type' => LineItem::PRODUCT_LINE_ITEM_TYPE]$this->context);

        $this->service->updateLineItem($cart['id' => $id]$lineItem$this->context);
    }

    public function testUpdateLineItemWithQuantityEvent(): void
    {
public function testCalculateAddsValidPromotionToCalculatedCart(): void
    {
        $promotionId = $this->getPromotionId();
        $discountItem = $this->getDiscountItem($promotionId);

        $discountItems = new LineItemCollection([$discountItem]);
        $original = new Cart(Uuid::randomHex());

        $productLineItem = new LineItem(Uuid::randomHex(), LineItem::PRODUCT_LINE_ITEM_TYPE);
        $productLineItem->setPrice(new CalculatedPrice(100.0, 100.0, new CalculatedTaxCollection()new TaxRuleCollection()));
        $productLineItem->setStackable(true);

        $toCalculate = new Cart(Uuid::randomHex());
        $toCalculate->add($productLineItem);
        $toCalculate->setPrice(new CartPrice(84.03, 100.0, 100.0, new CalculatedTaxCollection()new TaxRuleCollection(), CartPrice::TAX_STATE_GROSS));

        $this->promotionCalculator->calculate($discountItems$original$toCalculate$this->salesChannelContext, new CartBehavior());
        static::assertCount(2, $toCalculate->getLineItems());
        $promotionLineItems = $toCalculate->getLineItems()->filterType(PromotionProcessor::LINE_ITEM_TYPE);
        static::assertCount(1, $promotionLineItems);

        $promotionLineItem = $promotionLineItems->first();

        
$salesChannelIds = $this->connection->fetchFirstColumn('SELECT LOWER(HEX(id)) FROM sales_channel');
        $productIds = $this->connection->fetchFirstColumn('SELECT LOWER(HEX(id)) as id FROM `product` ORDER BY RAND() LIMIT 1000');
        $promotionCodes = $this->connection->fetchFirstColumn('SELECT `code` FROM `promotion` ORDER BY RAND() LIMIT 1000');
        $customerIds = $this->connection->fetchFirstColumn('SELECT LOWER(HEX(id)) as id FROM customer LIMIT 10');
        $tags = $this->getIds('tag');
        $writeContext = WriteContext::createFromContext($context->getContext());

        $context->getConsole()->progressStart($numberOfItems);

        $productLineItems = array_map(
            fn ($productId) => (new LineItem($productId, LineItem::PRODUCT_LINE_ITEM_TYPE, $productId$this->faker->randomDigit() + 1))
                ->setStackable(true)
                ->setRemovable(true),
            $productIds
        );
        $promotionLineItems = array_map(
            function D$promotionCode) {
                $uniqueKey = 'promotion-' . $promotionCode;

                return (new LineItem($uniqueKey, LineItem::PROMOTION_LINE_ITEM_TYPE))
                    ->setLabel($uniqueKey)
                    ->setGood(false)
                    ->setReferencedId($promotionCode)
                    
$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());
    }

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

        
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;
    }
}
$isMerged = $addedEvent->isMerged();
        });

        $cartService = $this->getContainer()->get(CartService::class);

        $context = $this->getSalesChannelContext();

        $cartId = Uuid::randomHex();
        $cart = $cartService->getCart($cartId$context);
        $cartService->add(
            $cart,
            (new LineItem('test', 'test'))->setStackable(true),
            $context
        );

        static::assertNotNull($isMerged);
        static::assertFalse($isMerged);

        $cartService->add(
            $cart,
            new LineItem('test', 'test'),
            $context
        );

        
'salesChannelId' => TestDefaults::SALES_CHANNEL, 'visibility' => ProductVisibilityDefinition::VISIBILITY_ALL],
            ],
        ];

        $this->getContainer()->get('product.repository')
            ->create([$product], Context::createDefaultContext());

        $this->addTaxDataToSalesChannel($this->context, $tax);

        $cart->add(
            (new LineItem($id, LineItem::PRODUCT_LINE_ITEM_TYPE, $id, 1))
                ->setStackable(true)
                ->setRemovable(true)
        );

        $calculated = $this->processor->process($cart$this->context, new CartBehavior());

        static::assertCount(1, $calculated->getLineItems());
        static::assertTrue($calculated->has($id));
        $item = $calculated->get($id);
        static::assertInstanceOf(LineItem::class$item);
        static::assertInstanceOf(CalculatedPrice::class$item->getPrice());
        static::assertSame(119.99, $item->getPrice()->getTotalPrice());

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