0

I'm trying to create conditions in the coupon rules, but when creating the conditions they are not aggregated correctly

Im doing:

        $rule = $this->couponRule;

        $cond['conditions']['1']['type']= 'Magento\SalesRule\Model\Rule\Condition\Combine';
        $cond['conditions']['1']['new_child']= '';
        $cond['conditions']['1']['aggregator']= 'all';
        $cond['conditions']['1']['value']= 1;
        $cond['conditions']['1--1']['type']= 'Magento\SalesRule\Model\Rule\Condition\Product\Found';
        $cond['conditions']['1--1']['new_child']= '';
        $cond['conditions']['1--1']['value']= 1;
        $cond['conditions']['1--1']['aggregator']= 'all';
        $cond['conditions']['1--1--1']['type']= 'Magento\SalesRule\Model\Rule\Condition\Product';
        $cond['conditions']['1--1--1']['attribute']= 'category_ids';
        $cond['conditions']['1--1--1']['attribute_scope']= 'parent';
        $cond['conditions']['1--1--1']['operator']= '!{}';
        $cond['conditions']['1--1--1']['value']= 10;

        try {
            $rule->setName('Cupom Dinâmico')
                ->setDescription('Criado dinamicamente')
                ->setUsesPerCustomer(1)
                ->setUsesPerCoupon(1)
                ->setIsActive(1)
                ->setSimpleAction('by_percent')
                ->setDiscountAmount($discount_amount)
                ->setDiscountQty(1)
                ->setApplyToShipping('no')
                ->setTimesUsed(1)
                ->setCouponType(2)
                ->setCustomerGroupIds('0,1,2,3')
                ->setWebsiteIds('1,2,4')
                ->setConditionsSerialized($this->serializer->serialize($cond))
                ->setCouponCode($coupon_code);


            $rule->save();
            return [
                'error' => false,
                'coupon_code' => $coupon_code,
                'discount_amount' => $discount_amount
            ];

        } catch (\Exception $e){
            return false;
        }

As the rule is being displayed:

enter image description here

How it should be: enter image description here

1 Answer 1

0

I was actually making a mistake in assembling the vector. The correct is:

$cond = array (
            'type' => 'Magento\\SalesRule\\Model\\Rule\\Condition\\Combine',
            'attribute' => NULL,
            'operator' => NULL,
            'value' => '1',
            'is_value_processed' => NULL,
            'aggregator' => 'all',
            'conditions' =>
                array (
                    0 =>
                        array (
                            'type' => 'Magento\\SalesRule\\Model\\Rule\\Condition\\Product\\Found',
                            'attribute' => NULL,
                            'operator' => NULL,
                            'value' => '1',
                            'is_value_processed' => NULL,
                            'aggregator' => 'all',
                            'conditions' =>
                                array (
                                    0 =>
                                        array (
                                            'type' => 'Magento\\SalesRule\\Model\\Rule\\Condition\\Product',
                                            'attribute' => 'category_ids',
                                            'operator' => '!{}',
                                            'value' => '10',
                                            'is_value_processed' => false,
                                            'attribute_scope' => 'parent',
                                        ),
                                ),
                        ),
                ),
        );
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.