LineItemOfTypeRule example


                [
                    'id' => $ruleId,
                    'name' => 'LineItemRule',
                    'priority' => 0,
                    'conditions' => [
                        [
                            'type' => (new GoodsCountRule())->getName(),
                            'ruleId' => $ruleId,
                            'children' => [
                                [
                                    'type' => (new LineItemOfTypeRule())->getName(),
                                    'value' => [
                                        'lineItemType' => 'test',
                                        'operator' => Rule::OPERATOR_EQ,
                                    ],
                                ],
                            ],
                            'value' => [
                                'count' => 100,
                                'operator' => Rule::OPERATOR_GTE,
                            ],
                        ],
                    ],
/** * @package business-ops * * @internal * * @covers \Shopware\Core\Checkout\Cart\Rule\LineItemOfTypeRule */
class LineItemOfTypeRuleTest extends TestCase
{
    public function testGetName(): void
    {
        $rule = new LineItemOfTypeRule(Rule::OPERATOR_EQ, 'test');

        static::assertEquals('cartLineItemOfType', $rule->getName());
    }

    public function testGetConstraints(): void
    {
        $rule = new LineItemOfTypeRule(Rule::OPERATOR_EQ, 'test');

        static::assertEquals(
            [
                'lineItemType' => RuleConstraints::string(),
                
$lineItem = (new LineItem('B', 'product', null, 2))
            ->setPriceDefinition(new QuantityPriceDefinition(100, new TaxRuleCollection(), 2));

        $cart->add($lineItem);

        $lineItem = (new LineItem('C', 'test'))
            ->setPriceDefinition(
                new PercentagePriceDefinition(
                    -10,
                    new AndRule([
                        (new LineItemOfTypeRule())->assign(['lineItemType' => 'product'])])
                )
            );

        $cart->add($lineItem);

        $calculated = $this->calculator->calculate($cart->getLineItems()$this->context, new CartBehavior());

        static::assertCount(3, $calculated);
        static::assertInstanceOf(LineItem::class$calculated->get('A'));
        static::assertInstanceOf(CalculatedPrice::class$calculated->get('A')->getPrice());
        static::assertSame(100.0, $calculated->get('A')->getPrice()->getTotalPrice());
        
/** * @covers \Shopware\Core\Checkout\Cart\Rule\LineItemOfTypeRule * * @internal */
#[Package('business-ops')] class LineItemOfTypeRuleTest extends TestCase
{
    public function testRuleWithProductTypeMatch(): void
    {
        $rule = (new LineItemOfTypeRule())->assign(['lineItemType' => LineItem::PRODUCT_LINE_ITEM_TYPE]);

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

        static::assertTrue(
            $rule->match(new LineItemScope(new LineItem('A', 'product')$context))
        );

        $cart = new Cart('test');
        $cart->add(new LineItem('A', 'product'));

        static::assertTrue(
            

                [
                    'id' => $ruleId,
                    'name' => 'LineItemRule',
                    'priority' => 0,
                    'conditions' => [
                        [
                            'type' => (new LineItemGoodsTotalRule())->getName(),
                            'ruleId' => $ruleId,
                            'children' => [
                                [
                                    'type' => (new LineItemOfTypeRule())->getName(),
                                    'value' => [
                                        'lineItemType' => 'test',
                                        'operator' => Rule::OPERATOR_EQ,
                                    ],
                                ],
                            ],
                            'value' => [
                                'count' => 100,
                                'operator' => Rule::OPERATOR_GTE,
                            ],
                        ],
                    ],


        $lineItem->setLabel($label);
        $lineItem->setDescription($description);
        $lineItem->setRemovable($removeable);
        $lineItem->setStackable($stackable);
        $lineItem->setPayload($payload);

        $lineItem->setPriceDefinition(
            new AbsolutePriceDefinition(
                (float) $priceDefinition['price'],
                new LineItemOfTypeRule(Rule::OPERATOR_NEQ, $type)
            )
        );

        $this->recalculationService->addCustomLineItem($orderId$lineItem$context);

        return new Response(null, Response::HTTP_NO_CONTENT);
    }

    #[Route(path: '/api/_action/order/{orderId}/lineItem', name: 'api.action.order.add-line-item', methods: ['POST'])]     public function addCustomLineItemToOrder(string $orderId, Request $request, Context $context): Response
    {
        

                [
                    'id' => $ruleId,
                    'name' => 'LineItemRule',
                    'priority' => 0,
                    'conditions' => [
                        [
                            'type' => (new GoodsPriceRule())->getName(),
                            'ruleId' => $ruleId,
                            'children' => [
                                [
                                    'type' => (new LineItemOfTypeRule())->getName(),
                                    'value' => [
                                        'lineItemType' => 'test',
                                        'operator' => Rule::OPERATOR_EQ,
                                    ],
                                ],
                            ],
                            'value' => [
                                'amount' => 100,
                                'operator' => Rule::OPERATOR_GTE,
                            ],
                        ],
                    ],
Home | Imprint | This part of the site doesn't use cookies.