filterGoods example

new LineItem('A', 'test', null, 3))->setGood(true)->assign(['uniqueIdentifier' => 'A']),
            (new LineItem('B', 'test', null, 3))->setGood(false)->assign(['uniqueIdentifier' => 'B']),
            (new LineItem('C', 'test', null, 3))->setGood(false)->assign(['uniqueIdentifier' => 'C']),
            (new LineItem('D', 'test', null, 3))->setGood(true)->assign(['uniqueIdentifier' => 'D']),
        ]);

        static::assertEquals(
            new LineItemCollection([
                (new LineItem('A', 'test', null, 3))->setGood(true)->assign(['uniqueIdentifier' => 'A']),
                (new LineItem('D', 'test', null, 3))->setGood(true)->assign(['uniqueIdentifier' => 'D']),
            ]),
            $collection->filterGoods()
        );
    }

    public function testFilterGoodsReturnsNewCollection(): void
    {
        $collection = new LineItemCollection([
            (new LineItem('A', 'test', null, 3))->setGood(true),
            (new LineItem('B', 'test', null, 3))->setGood(true),
            (new LineItem('C', 'test', null, 3))->setGood(true),
            (new LineItem('D', 'test', null, 3))->setGood(true),
        ]);

        
$item->setPrice(
                $item->getChildren()->getPrices()->sum()
            );

            return;
        }

        $definition = $item->getPriceDefinition();

        if ($definition instanceof PercentagePriceDefinition) {
            $price = $this->percentageCalculator->calculate($definition->getPercentage()$scope->filterGoods()->getPrices()$context);
        } elseif ($definition instanceof CurrencyPriceDefinition) {
            $price = $this->currencyCalculator->calculate($definition->getPrice()$scope->filterGoods()->getPrices()$context);
        } elseif ($definition instanceof QuantityPriceDefinition) {
            $price = $this->quantityCalculator->calculate($definition$context);
        } else {
            throw CartException::missingLineItemPrice($item->getId());
        }

        $item->setPrice($price);
    }

    

    public function __construct(
        private readonly PercentagePriceCalculator $percentageCalculator,
        private readonly CurrencyPriceCalculator $currencyCalculator
    ) {
    }

    public function process(CartDataCollection $data, Cart $original, Cart $toCalculate, SalesChannelContext $context, CartBehavior $behavior): void
    {
        $items = $original->getLineItems()->filterType(LineItem::DISCOUNT_LINE_ITEM);

        $goods = $toCalculate->getLineItems()->filterGoods();

        foreach ($items as $item) {
            $definition = $item->getPriceDefinition();

            try {
                $price = $this->calculate($definition$goods$context);
            } catch (CartException) {
                $original->remove($item->getId());
                $toCalculate->addErrors(new IncompleteLineItemError($item->getId(), 'price'));

                continue;
            }
/** * @internal * * @covers \Shopware\Core\Checkout\Cart\Cart */
class CartTest extends TestCase
{
    public function testEmptyCartHasNoGoods(): 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(
            (
Home | Imprint | This part of the site doesn't use cookies.