1

I'm having an issue with creating a shopping cart rule in Magento 2.1. Is there a way to create shopping cart rule programmatically and then apply it to specific group of customers?

2 Answers 2

5

According to the best practices in Magento 2 you need to use Repositories to create/save entities. However, there are still some issues with repositories for particular entities, including the SalesRule and that's why I will describe here how to achieve it directly via model (the same way how it's implemented in Magento 2 core).

In your class constructor inject the Rule Factory

...
protected $ruleFactory

public function __construct(\Magento\SalesRule\Model\RuleFactory $ruleFactory) {
    $this->rulesFactory = $ruleFactory
}
...

After this, inside of the method, where you are going to create the rule, init the rule data, assign the date to the rule model and save the model:

$ruleData = [
            "name" => "Buy 3 tee shirts and get the 4th free",
            "description" => "Buy 3 tee shirts and get the 4th free",
            "from_date" => null,
            "to_date" => null,
            "uses_per_customer" => "0",
            "is_active" => "1",
            "stop_rules_processing" => "0",
            "is_advanced" => "1",
            "product_ids" => null,
            "sort_order" => "0",
            "simple_action" => "buy_x_get_y",
            "discount_amount" => "1.0000",
            "discount_qty" => null,
            "discount_step" => "3",
            "apply_to_shipping" => "0",
            "times_used" => "0",
            "is_rss" => "1",
            "coupon_type" => "NO_COUPON",
            "use_auto_generation" => "0",
            "uses_per_coupon" => "0",
            "simple_free_shipping" => "0",
            "customer_group_ids" => [0, 1, 2, 3],
            "website_ids" => [1],
            "coupon_code" => null,
            "store_labels" => [],
            "conditions_serialized" => '',
            "actions_serialized" => ''
        ];

$ruleModel = $this->ruleFactory->create();
$ruleModel->setData($ruleData);
$ruleModel->save();

All conditions and actions can be assigned via conditions_serialized and actions_serialized fields. They are empty in this example since they have large values usually. To get the corresponding serialized values for your rule go to admin panel, create a rule with all values you want, save it and check the POST parameters of the save request. You will be able to extract the serialized values from the POST request.

1
  • Sir i try your code it work create rule perfect. From admin panel i make condition and check value in salesrule table where conditions_serialized column has my condition i have copy this condition and then set in ruleModel->setConditionsSerialized('copied_value_from_table') it not working
    – HaFiz Umer
    Commented Nov 1, 2019 at 5:14
1

Please use the following line of code and make your array according to your need.

public function getRuleData(){
      $ruleModel = $this->ruleFactory->create();
      $ruleModel->setData($this->prepareRuleData());
      $ruleModel->save();
    }

public function prepareRuleData(){
        return [
            'rule_id' => '1',
            'name' => 'testrule',
            'is_active' => '1',
            'conditions_serialized' => json_encode([
                'type' => \Magento\SalesRule\Model\Rule\Condition\Combine::class,
                'attribute' => null,
                'operator' => null,
                'value' => '1',
                'is_value_processed' => null,
                'aggregator' => 'all',
                'conditions' => [
                    [
                        'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class,
                        'attribute' => 'base_subtotal',
                        'operator' => '>=',
                        'value' => '100',
                        'is_value_processed' => false,
                    ],
                ],
            ]),
            'actions_serialized' => json_encode([
                'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Combine::class,
                'attribute' => null,
                'operator' => null,
                'value' => '1',
                'is_value_processed' => null,
                'aggregator' => 'all',
                'conditions' => [
                    [
                        'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
                        'attribute' => 'attribute_set_id',
                        'operator' => '==',
                        'value' => '4',
                        'is_value_processed' => false,
                    ],
                ],
            ]),
            'coupon_type' => '1',
            'coupon_code' => '',
            'store_labels' => [
                0 => 'TestRule',
                1 => 'TestRuleForDefaultStore',
            ],
            'extension_attributes' => [
                'some_extension_attributes' => 123,
            ],
        ];
    }

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.