OrRule example

public function buildDiscountLineItem(string $code, PromotionEntity $promotion, PromotionDiscountEntity $discount, string $currencyId, float $currencyFactor = 1.0): LineItem
    {
        // get the rules collection of discount         $discountRuleCollection = $discount->getDiscountRules();

        // this is our target Filter that may be null if discount has no filters         $targetFilter = null;

        // we do only need to build a target rule if user has allowed it         // and the rule collection is not empty         if ($discountRuleCollection instanceof RuleCollection && $discount->isConsiderAdvancedRules() && $discountRuleCollection->count() > 0) {
            $targetFilter = new OrRule();

            foreach ($discountRuleCollection as $discountRule) {
                /** @var Rule|string|null $rule */
                $rule = $discountRule->getPayload();

                if ($rule instanceof Rule) {
                    $targetFilter->addRule($rule);
                }
            }
        }

        
$currencyId2 = Uuid::randomHex();

        $data = [
            'id' => $id,
            'name' => 'test rule',
            'priority' => 1,
            'conditions' => [
                [
                    'type' => (new AndRule())->getName(),
                    'children' => [
                        [
                            'type' => (new OrRule())->getName(),
                            'children' => [
                                [
                                    'type' => (new CurrencyRule())->getName(),
                                    'value' => [
                                        'currencyIds' => [
                                            $currencyId,
                                            $currencyId2,
                                        ],
                                        'operator' => CurrencyRule::OPERATOR_EQ,
                                    ],
                                ],
                            ],
use Shopware\Core\Test\Stub\Rule\TrueRule;

/** * @covers \Shopware\Core\Framework\Rule\Container\OrRule * * @internal */
class OrRuleTest extends TestCase
{
    public function testTrue(): void
    {
        $rule = new OrRule([
            new TrueRule(),
            new FalseRule(),
        ]);

        static::assertTrue(
            $rule->match(
                new CheckoutRuleScope(
                    $this->createMock(SalesChannelContext::class)
                )
            )
        );
    }

class RuleCollectionTest extends TestCase
{
    public function testMetaCollecting(): void
    {
        $collection = new RuleCollection([
            new TrueRule(),
            new AndRule([
                new TrueRule(),
                new OrRule([
                    new TrueRule(),
                    new FalseRule(),
                ]),
            ]),
        ]);

        static::assertTrue($collection->has(FalseRule::class));
        static::assertTrue($collection->has(OrRule::class));
        static::assertEquals(
            new RuleCollection([
                new FalseRule(),
            ]),
$loadedEvent = new EntityLoadedEvent($this->ruleDefinition, [$rule$rule2]$this->context);

        static::assertNull($rule->getPayload());

        $this->updater
            ->expects(static::once())
            ->method('update')
            ->with([$id$id2])
                ->willReturn(
                    [
                        $id => ['payload' => serialize(new AndRule()), 'invalid' => false],
                        $id2 => ['payload' => serialize(new OrRule()), 'invalid' => false],
                    ]
                );
        $this->rulePayloadSubscriber->unserialize($loadedEvent);

        static::assertNotNull($rule->getPayload());
        static::assertInstanceOf(AndRule::class$rule->getPayload());
        static::assertFalse($rule->isInvalid());
        static::assertNotNull($rule2->getPayload());
        static::assertInstanceOf(OrRule::class$rule2->getPayload());
        static::assertFalse($rule2->isInvalid());
    }

    
$personaRuleEntity = new RuleEntity();
        $personaRuleEntity->setId('R1');
        $personaRuleEntity->setPayload($fakePersonaRule);

        $promotion = new PromotionEntity();
        $promotion->setCustomerRestriction(false);

        $promotion->setPersonaRules(new RuleCollection([$personaRuleEntity]));

        $expected = new AndRule(
            [
                new OrRule(
                    [$fakePersonaRule]
                ),
            ]
        );

        static::assertEquals($expected$promotion->getPreconditionRule());
    }

    /** * This test verifies, that we have the correct * persona customer rules inside our precondition filter. * Thus we simulate a list of assigned customers, that will then be * converted into CustomerNumberRules and added to our main condition. * Why do we need separate customer rules? Because we don't want to match * a list of customer numbers, but only 1 single customer number...and thus only 1 single * rule should match within a list of rules, based on an OR condition. * * @group promotions */
$discountFilter = $this->getFakeRule($amount$operator);

        $discountRuleEntity = new RuleEntity();
        $discountRuleEntity->setId('foo');
        $discountRuleEntity->setPayload($discountFilter);

        $ruleCollection = new RuleCollection();
        $ruleCollection->add($discountRuleEntity);
        $discount->setDiscountRules($ruleCollection);

        $expectedRule = new OrRule();
        $expectedRule->addRule($discountFilter);

        $item = $builder->buildDiscountLineItem('', $this->promotion, $discount, 'C1', $currencyFactor);

        static::assertInstanceOf(AbsolutePriceDefinition::class$item->getPriceDefinition());
        static::assertEquals($expectedRule$item->getPriceDefinition()->getFilter());
    }

    /** * This test verifies that the correct discount filter * is set in the discountItemBuilder * * @group promotions */

        $this->ruleRepository = $this->getContainer()->get('rule.repository');
        $this->conditionRepository = $this->getContainer()->get('rule_condition.repository');
        $this->context = Context::createDefaultContext();
    }

    public function testValidateWithInvalidRulesType(): void
    {
        try {
            $this->conditionRepository->create([
                [
                    'type' => (new OrRule())->getName(),
                    'ruleId' => Uuid::randomHex(),
                    'value' => [
                        'rules' => ['Rule'],
                    ],
                ],
            ]$this->context);
            static::fail('Exception was not thrown');
        } catch (WriteException $stackException) {
            $exceptions = iterator_to_array($stackException->getErrors());
            static::assertCount(1, $exceptions);
            static::assertSame('/0/value/rules', $exceptions[0]['source']['pointer']);
            

        $id = Uuid::randomHex();
        $currencyId1 = Uuid::randomHex();
        $currencyId2 = Uuid::randomHex();

        $data = [
            'id' => $id,
            'name' => 'test rule',
            'priority' => 1,
            'conditions' => [
                [
                    'type' => (new OrRule())->getName(),
                    'children' => [
                        [
                            'type' => (new CurrencyRule())->getName(),
                            'value' => [
                                'currencyIds' => [
                                    $currencyId1,
                                    $currencyId2,
                                ],
                                'operator' => CurrencyRule::OPERATOR_EQ,
                            ],
                        ],
                    ],
$productLowQuantityHighPrice->setId($productLowQuantity1Id);
        $productLowQuantityHighPrice->setReferencedId($productLowQuantity1Id);
        $productLowQuantityHighPrice->setQuantity($minQuantity - 1);

        $productLowQuantityLowPrice = $this->createProductItem($minPrice - 0.1, 0);
        $productLowQuantityLowPrice->setId($productLowQuantity2Id);
        $productLowQuantityLowPrice->setReferencedId($productLowQuantity2Id);
        $productLowQuantityLowPrice->setQuantity($minQuantity - 1);

        $rulesMinPrice = new RuleEntity();
        $rulesMinPrice->setId(Uuid::randomBytes());
        $rulesMinPrice->setPayload(new OrRule(
            [
                $this->getMinQuantityRule($productHighQuantity1Id$minQuantity),
                $this->getMinQuantityRule($productHighQuantity2Id$minQuantity),
                $this->getMinQuantityRule($productLowQuantity1Id$minQuantity),
                $this->getMinQuantityRule($productLowQuantity2Id$minQuantity),
            ]
        ));

        $rulesMinQuantity = new RuleEntity();
        $rulesMinQuantity->setId(Uuid::randomBytes());
        $rulesMinQuantity->setPayload(new AndRule([$this->getMinPriceRule($minPrice)]));

        

        );

        // first check if we either restrict our persona         // with direct customer assignments or with persona rules         if ($this->isCustomerRestriction()) {
            // we use assigned customers             // check if we have customers.             // if so, create customer rules for it and add that also as             // a separate OR condition to our main persona rule             if ($this->getPersonaCustomers() !== null) {
                $personaCustomerOR = new OrRule();

                foreach ($this->getPersonaCustomers()->getElements() as $customer) {
                    // build our new rule for this                     // customer and his/her customer number                     $custRule = new CustomerNumberRule();
                    $custRule->assign(['numbers' => [$customer->getCustomerNumber()], 'operator' => CustomerNumberRule::OPERATOR_EQ]);

                    $personaCustomerOR->addRule($custRule);
                }

                // add the rule to our main rule
$decoded = $serializer->decode(
            new PriceDefinitionField('test', 'test'),
            $encoded['test']
        );

        static::assertEquals($definition$decoded);
    }

    public static function serializerProvider()
    {
        $rule = new AndRule([
            new OrRule([
                new CurrencyRule(CurrencyRule::OPERATOR_EQ, [Defaults::CURRENCY]),
            ]),
            new CurrencyRule(CurrencyRule::OPERATOR_EQ, [Defaults::CURRENCY]),
        ]);

        yield 'Test quantity price definition' => [
            new QuantityPriceDefinition(100, new TaxRuleCollection([new TaxRule(19, 50)new TaxRule(7, 50)]), 3),
        ];

        yield 'Test absolute price definition' => [
            new AbsolutePriceDefinition(20, $rule),
        ];
$classes = array_column($rules, 'rule');
            $names = array_column($rules, 'name');

            $ruleData = [
                'id' => Uuid::randomHex(),
                'priority' => $i,
                'name' => implode(' + ', $names),
                'description' => $context->getFaker()->text(),
            ];

            $ruleData['conditions'][] = $this->buildChildRule(null, (new OrRule())->assign(['rules' => $classes]));

            $payload[] = $ruleData;
        }

        // nested condition         $nestedRule = new OrRule();

        $nestedRuleData = [
            'id' => Uuid::randomHex(),
            'priority' => 20,
            'name' => 'nested rule',
            
Home | Imprint | This part of the site doesn't use cookies.