Magento 2 Create Coupon Code Programmatically
Magento 2 Create Coupon Code Programmatically It is very simple to create coupon code programmatically in magento2. Here in this article we are going to explain how you can create coupons in Magento2.
Magento 2 Create Coupon Code Programmatically | Script | Example
You can create coupon code simply as below-
Inject Rule Factory in Constructor
Magento 2 Create Coupon Code Programmatically Example:
protected $ruleFactory public function __construct(\Magento\SalesRule\Model\RuleFactory $ruleFactory) { $this->rulesFactory = $ruleFactory } |
Create Coupon Code
Now use the below code to create coupon programmatically-
Create Coupon Programmatically Example:
$ruleData = [ "name" => "Custom Discount", "description" => "Get Custom Discount", "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" => "fixed_amount", "discount_amount" => "50", "discount_qty" => null, "discount_step" => "3", "apply_to_shipping" => "0", "times_used" => "0", "is_rss" => "1", "coupon_type" => "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" => 'FLAT_50', "store_labels" => [], "conditions_serialized" => '', "actions_serialized" => '' ]; $ruleModel = $this->ruleFactory->create(); $ruleModel->setData($ruleData); $ruleModel->save(); |
Advertisements