setGood example


    public function buildPlaceholderItem(string $code): LineItem
    {
        // void duplicate codes with other items         // that might not be from the promotion scope         $uniqueKey = self::PLACEHOLDER_PREFIX . $code;

        $item = new LineItem($uniqueKey, PromotionProcessor::LINE_ITEM_TYPE);
        $item->setLabel($uniqueKey);
        $item->setGood(false);

        // this is used to pass on the code for later usage         $item->setReferencedId($code);

        // this is important to avoid any side effects when calculating the cart         // a percentage of 0,00 will just do nothing         $item->setPriceDefinition(new PercentagePriceDefinition(0));

        return $item;
    }

    

    }

    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;
    }
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)
                    ->setPriceDefinition(new PercentagePriceDefinition(0));
            },
            $promotionCodes
        );

        $orders = [];

        for ($i = 1; $i <= $numberOfItems; ++$i) {
            $customerId = $context->getFaker()->randomElement($customerIds);

            
return $order;
    }

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


    public function testOnlyMatchesGoods(): void
    {
        $this->rule->assign([
            'categoryIds' => ['1'],
            'operator' => Rule::OPERATOR_NEQ,
        ]);

        $lineItemCollection = new LineItemCollection([
            $this->createLineItemWithCategories(['1']),
            $this->createLineItem(LineItem::PROMOTION_LINE_ITEM_TYPE, 1, 'PROMO')->setGood(false),
        ]);

        $cart = $this->createCart($lineItemCollection);

        $match = $this->rule->match(new CartRuleScope(
            $cart,
            $this->createMock(SalesChannelContext::class)
        ));

        static::assertFalse($match);
    }

    
static::assertTrue(
            $rule->match(new CartRuleScope($cart$context))
        );
    }

    public function testRuleWithLowerThanEqualCountNotMatch(): void
    {
        $rule = (new GoodsCountRule())->assign(['count' => 0, 'operator' => Rule::OPERATOR_LTE]);

        $cart = new Cart('test');

        $cart->add((new LineItem('A', 'test'))->setGood(true));

        $context = $this->createMock(SalesChannelContext::class);

        static::assertFalse(
            $rule->match(new CartRuleScope($cart$context))
        );
    }

    public function testRuleWithGreaterThanEqualExactCountMatch(): void
    {
        $rule = (new GoodsCountRule())->assign(['count' => 1, 'operator' => Rule::OPERATOR_GTE]);

        
'not equal / match' => [[State::IS_PHYSICAL], Rule::OPERATOR_NEQ, State::IS_DOWNLOAD, true],
            'not equal / no match' => [[State::IS_PHYSICAL, State::IS_DOWNLOAD], Rule::OPERATOR_NEQ, State::IS_DOWNLOAD, false],
        ];
    }

    /** * @param array<int, string> $states */
    private function createLineItemWithStates(array $states): LineItem
    {
        return (new LineItem(Uuid::randomHex(), LineItem::PRODUCT_LINE_ITEM_TYPE))
            ->setGood(true)
            ->setStates($states);
    }
}
/** * @internal * * @covers \Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection */
class OrderLineItemCollectionTest extends TestCase
{
    public function testFilterGoodsFlat(): void
    {
        $lineItemA = new OrderLineItemEntity();
        $lineItemA->setId(Uuid::randomHex());
        $lineItemA->setGood(true);

        $lineItemB = new OrderLineItemEntity();
        $lineItemB->setId(Uuid::randomHex());
        $lineItemB->setGood(false);

        $lineItemC = new OrderLineItemEntity();
        $lineItemC->setId(Uuid::randomHex());
        $lineItemC->setGood(true);

        $lineItemD = new OrderLineItemEntity();
        $lineItemD->setId(Uuid::randomHex());
        
return $mock;
    }

    public static function createCart(): Cart
    {
        $cart = new Cart('test');
        $cart->setLineItems(
            new LineItemCollection([
                (new LineItem('A', 'product', 'A', 27))
                    ->setPrice(new CalculatedPrice(10, 270, new CalculatedTaxCollection()new TaxRuleCollection(), 27)),
                (new LineItem('B', 'test', 'B', 5))
                    ->setGood(false)
                    ->setPrice(new CalculatedPrice(0, 0, new CalculatedTaxCollection()new TaxRuleCollection())),
            ])
        );
        $cart->setPrice(
            new CartPrice(
                275.0,
                275.0,
                0,
                new CalculatedTaxCollection(),
                new TaxRuleCollection(),
                CartPrice::TAX_STATE_GROSS
            )
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(
            (new LineItem('A', 'test'))
                ->setGood(false)
                ->setStackable(true)
        );

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

    


        static::assertEquals(
            (new LineItem('C', 'test', null, 3))->assign(['uniqueIdentifier' => 'C']),
            $collection->get('C')
        );
    }

    public function testFilterGoodsReturnsOnlyGoods(): void
    {
        $collection = new LineItemCollection([
            (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()
        );
class QuantityItem extends LineItem
{
    public function __construct(
        float $price,
        TaxRuleCollection $taxes,
        bool $good = true,
        int $quantity = 1
    ) {
        parent::__construct(Uuid::randomHex(), LineItem::PRODUCT_LINE_ITEM_TYPE, null, $quantity);

        $this->priceDefinition = new QuantityPriceDefinition($price$taxes$quantity);
        $this->setGood($good);
    }
}
public static function orderProvider(): \Generator
    {
        yield 'no order found' => [null, []];

        $order = new OrderEntity();

        yield 'order without line items' => [$order[]];

        $order = new OrderEntity();

        $lineItem = new OrderLineItemEntity();
        $lineItem->setGood(true);
        $lineItem->setId(Uuid::randomHex());

        $order->setLineItems(new OrderLineItemCollection([$lineItem]));

        yield 'order without downloadable line items' => [$order[]];

        $order = new OrderEntity();

        $lineItem = new OrderLineItemEntity();
        $lineItem->setId(Uuid::randomHex());
        $lineItem->setGood(true);
        


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

            $orderLineItemDownloadCollection = new OrderLineItemDownloadCollection();
            $orderLineItemDownloadCollection->add($orderLineItemDownload);
            $orderLineItem->setDownloads($orderLineItemDownloadCollection);
        }
$index[$lineItem->getParentId()]->addChild($currentLineItem);
        }

        return $root;
    }

    private static function updateLineItem(LineItem $lineItem, OrderLineItemEntity $entity, string $id): void
    {
        $lineItem->setId($entity->getIdentifier())
            ->setLabel($entity->getLabel())
            ->setGood($entity->getGood())
            ->setRemovable($entity->getRemovable())
            ->setStackable($entity->getStackable())
            ->setStates($entity->getStates())
            ->addExtension(OrderConverter::ORIGINAL_ID, new IdStruct($id));

        if ($entity->getPayload() !== null) {
            $lineItem->setPayload($entity->getPayload());
        }

        if ($entity->getPrice() !== null) {
            $lineItem->setPrice($entity->getPrice());
        }
Home | Imprint | This part of the site doesn't use cookies.