setCartItems example


    public function testAffectedPricesFromAllPackages(): void
    {
        $product1 = $this->createProductItem(29, 19);
        $product2 = $this->createProductItem(14, 19);

        $package1 = new DiscountPackage(new LineItemQuantityCollection());
        $package1->setCartItems(new LineItemFlatCollection([$product1]));

        $package2 = new DiscountPackage(new LineItemQuantityCollection());
        $package2->setCartItems(new LineItemFlatCollection([$product2]));

        $collection = new DiscountPackageCollection([$package1$package2]);

        static::assertEquals(2, $collection->getAffectedPrices()->count());
    }

    /** * This test verifies that our object collects the * line items from all existing packages. * * @group promotions */
/** * @dataProvider sortingProvider * * @param array<LineItem> $items * @param array<LineItemQuantity> $meta * @param array<string> $expected */
    public function testSorting(AbstractPriceSorter $sorter, array $meta, array $items, array $expected): void
    {
        $package = new DiscountPackage(new LineItemQuantityCollection($meta));

        $package->setCartItems(new LineItemFlatCollection($items));

        $sorter->sort(new DiscountPackageCollection([$package]));

        $ordered = $package->getMetaData()->fmap(fn (LineItemQuantity $item) => $item->getLineItemId());

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

    public static function sortingProvider(): \Generator
    {
        yield 'Test ascending sorting' => [
            
$discount = new DiscountLineItem('foo', $priceDefinition['discountScope' => 'foo', 'discountType' => 'bar'], null);

        $lineItem = new LineItem(Uuid::randomHex(), LineItem::PRODUCT_LINE_ITEM_TYPE, Uuid::randomHex(), 2);
        $lineItem->setPrice(new CalculatedPrice($packageSum / 2, $packageSumnew CalculatedTaxCollection()new TaxRuleCollection()));

        $package = new DiscountPackage(
            new LineItemQuantityCollection([
                new LineItemQuantity($lineItem->getId(), 1),
                new LineItemQuantity($lineItem->getId(), 1),
            ])
        );
        $package->setCartItems(new LineItemFlatCollection([$lineItem]));

        $price = $discountCalculator->calculate($discountnew DiscountPackageCollection([$package])$context);

        static::assertEquals($discountOut$price->getPrice()->getTotalPrice());
    }

    public function testInvalidPriceDefinitionThrow(): void
    {
        $context = Generator::createSalesChannelContext();

        $rounding = new CashRounding();

        

        // set the line item from the cart for each unit         foreach ($result as $package) {
            $cartItemsForUnit = new LineItemFlatCollection();

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

                $cartItemsForUnit->add($this->splitted[$lineItemId]);
            }

            $package->setCartItems($cartItemsForUnit);
        }

        return $result;
    }

    private function isAutomaticDiscount(LineItem $discountItem): bool
    {
        return empty($discountItem->getPayloadValue('code'));
    }
}

    public function testCartItemsAreCorrectlyAdded(): void
    {
        $cartItems = new LineItemFlatCollection();
        $product = $this->createProductItem(29, 19);
        $cartItems->add($product);

        $package = new DiscountPackage(new LineItemQuantityCollection());
        $package->setCartItems($cartItems);

        static::assertEquals(1, $package->getCartItems()->count());
    }

    /** * This test verifies that we dont get an exception * when requesting the price without any items. * We have to get 0,00 in this case. * * @group promotions */
    
Home | Imprint | This part of the site doesn't use cookies.