{"id":786,"date":"2020-01-27T12:13:24","date_gmt":"2020-01-27T12:13:24","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/01\/27\/programmatically-create-coupon-code-in-magento-2\/"},"modified":"2025-05-22T15:05:41","modified_gmt":"2025-05-22T09:35:41","slug":"programmatically-create-coupon-code-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/programmatically-create-coupon-code-in-magento-2\/","title":{"rendered":"How To Programmatically Create Coupon Code In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Discount is one serious hack in online stores and that&#8217;s how people do businesses in a competitive market.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">People tend to love a good deal, it&#8217;s human nature. When you are an online business, sometimes, offering the right coupon code to the right person can push conversion.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, if you are a Magento 2 store owner and want to offer a personalized coupon code to a potential customer, you might get lucky.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The post gives the method to <strong>programmatically create coupon code in Magento 2<\/strong> store and attract potential customers to use the coupon code, resulting in conversion!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method To Programmatically Create Coupon Code In Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nuse Magento\\Framework\\App\\Bootstrap; \n\nrequire __DIR__ . '\/app\/bootstrap.php';\n\n$params = $_SERVER;\n$bootstrap = Bootstrap::create(BP, $params);\n$obj = $bootstrap->getObjectManager();\n$state = $obj->get('Magento\\Framework\\App\\State');\n$state->setAreaCode('adminhtml');  \n\n\n$coupon['name'] = 'Test Rule';\n$coupon['desc'] = 'Test Rule';\n$coupon['start'] = date('Y-m-d');\n$coupon['end'] = '';\n$coupon['max_redemptions'] = 1;\n$coupon['discount_type'] ='by_percent';\n$coupon['discount_amount'] = 50;\n$coupon['flag_is_free_shipping'] = 'no';\n$coupon['redemptions'] = 1;\n$coupon['code'] ='OFF50'; \n\n$shoppingCartPriceRule = $obj->create('Magento\\SalesRule\\Model\\Rule');\n$shoppingCartPriceRule->setName($coupon['name'])\n        ->setDescription($coupon['desc'])\n        ->setFromDate($coupon['start'])\n        ->setToDate($coupon['end'])\n        ->setUsesPerCustomer($coupon['max_redemptions'])\n        ->setCustomerGroupIds(array('0','1','2','3',))\n        ->setIsActive(1)\n        ->setSimpleAction($coupon['discount_type'])\n        ->setDiscountAmount($coupon['discount_amount'])\n        ->setDiscountQty(1)\n        ->setApplyToShipping($coupon['flag_is_free_shipping'])\n        ->setTimesUsed($coupon['redemptions'])\n        ->setWebsiteIds(array('1'))\n        ->setCouponType(2)\n        ->setCouponCode($coupon['code'])\n        ->setUsesPerCoupon(NULL);\n$shoppingCartPriceRule->save();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Generate coupon codes in Magento 2 store and effectively use the discount strategies to benefit the business.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Help Magento store owners manage discounts and coupon codes by sharing this solution via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Discount is one serious hack in online stores and that&#8217;s how people do businesses in a competitive market. People tend to love a good deal,&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-786","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=786"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/786\/revisions"}],"predecessor-version":[{"id":15114,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/786\/revisions\/15114"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}