{"id":6893,"date":"2021-07-23T10:25:47","date_gmt":"2021-07-23T04:55:47","guid":{"rendered":"https:\/\/www.innovationm.com\/blog\/?p=6893"},"modified":"2021-08-02T12:21:51","modified_gmt":"2021-08-02T06:51:51","slug":"how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\/","title":{"rendered":"How can we create coupon or discount codes programmatically in magento 2 ?"},"content":{"rendered":"<p><span style=\"font-weight: 400;\"><br \/>\nAs we know coupon codes are the combination of letters and digits which are given to the customer to avail a special discount offer at the checkout page of an online store. Nowadays it&#8217;s the basic technique to attract customers to online stores.<\/span><\/p>\n<h3><strong>In this blog, we will create coupon codes programmatically.\u00a0<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">For this we need a model class <\/span><b>Magento\\SalesRule\\Model\\Rule<\/b><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">With the help of this class, we will create coupon code programmatically.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In this, we will create our own coupon code with the \u2018CR\u2019 prefix and then some random suffix.<\/span><\/p>\n<h3><strong>The coupon name will have the prefix \u201ccoupon\u201d.<\/strong><\/h3>\n<p><strong>Let&#8217;s start with the code:<\/strong><\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n\r\nnamespace Innovationm\\CreditCoupon\\Controller\\Index;\r\n\r\nuse Magento\\Framework\\App\\Action\\Action;\r\n\r\nuse Magento\\Framework\\App\\Action\\Context;\r\n\r\nuse Magento\\Framework\\Controller\\ResultFactory;\r\n\r\nuse Magento\\SalesRule\\Model\\Rule;\r\n\r\n\r\nclass Index extends Action\r\n\r\n{\r\n\r\n\u00a0\u00a0\u00a0\u00a0\/**\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0* @var PageFactory\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0*\/\r\n\r\n\u00a0\u00a0\u00a0\u00a0protected $_resultPageFactory;\r\n\r\n\r\n\u00a0\u00a0\u00a0\u00a0\/**\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0* @param Context \u00a0 \u00a0 $context\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0* @param Rule $rule\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0* @param PageFactory $resultPageFactory\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0*\/\r\n\r\n\u00a0\u00a0\u00a0\u00a0public function __construct(\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Context $context,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ResultFactory $resultFactory,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Rule $rule\r\n\r\n\u00a0\u00a0\u00a0\u00a0) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$this-&gt;resultFactory = $resultFactory;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$this-&gt;shoppingCartPriceRule = $rule;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0parent::__construct($context);\r\n\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\r\n\u00a0\u00a0\u00a0\u00a0public function execute()\r\n\r\n\u00a0\u00a0\u00a0\u00a0{\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$coupon = [] ;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$str_result = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; \/\/random string to generate the code\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$ranStr = substr(str_shuffle($str_result), 0, 5);\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$coupon['name'] = \"coupon \".$ranStr; \/\/Generate a rule name\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$coupon['desc'] = \"Discount amount coupon\";\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$coupon['start'] = date('Y-m-d'); \/\/Coupon use start date\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$coupon['end'] = ''; \/\/coupon use end date\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$coupon['max_redemptions'] = 1; \/\/Uses per Customer\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$coupon['discount_type'] ='cart_fixed'; \/\/for discount type\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$coupon['discount_amount'] = 100; \/\/discount amount\/percentage\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$coupon['redemptions'] = 1;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$coupon['code'] = 'CR'.\"_\".$ranStr; \/\/generate a random coupon code\r\n\r\n\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$this-&gt;shoppingCartPriceRule-&gt;setName($coupon['name'])\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-&gt;setDescription($coupon['desc'])\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-&gt;setFromDate($coupon['start'])\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-&gt;setToDate($coupon['end'])\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-&gt;setUsesPerCustomer($coupon['max_redemptions'])\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-&gt;setCustomerGroupIds(array('0','1','2','3')) \/\/select customer group\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-&gt;setIsActive(1)\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-&gt;setSimpleAction($coupon['discount_type'])\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-&gt;setDiscountAmount($coupon['discount_amount'])\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-&gt;setDiscountQty()\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-&gt;setStopRulesProcessing(0)\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-&gt;setTimesUsed($coupon['redemptions'])\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-&gt;setWebsiteIds(array('1'))\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-&gt;setCouponType(2)\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-&gt;setCouponCode($coupon['code'])\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-&gt;setUsesPerCoupon(1)\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-&gt;setSimpleFreeShipping(1);\r\n\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$this-&gt;shoppingCartPriceRule-&gt;save(); \/\/Coupon code will be generated;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n}<\/pre>\n<h3><b>Explanation of methods used:<\/b><\/h3>\n<ul>\n<li><b>setName()<\/b><span style=\"font-weight: 400;\"> Method used for set\u00a0 <\/span><b>Rule Name<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li><b>setDescription() <\/b><span style=\"font-weight: 400;\">is used for set rule <\/span><b>Description<\/b><\/li>\n<li aria-level=\"1\"><b>setFromDate() <\/b><span style=\"font-weight: 400;\">is used to set coupon start dates.<\/span><\/li>\n<li aria-level=\"1\"><b>setToDate() <\/b><span style=\"font-weight: 400;\">is used to set coupon expiry dates.<\/span><\/li>\n<li aria-level=\"1\"><b>setUsesPerCustomer() <\/b><span style=\"font-weight: 400;\">is used to set the maximum number of uses of coupons per customer.<\/span><\/li>\n<li aria-level=\"1\"><b>setIsActive()<\/b><span style=\"font-weight: 400;\"> is used for Enabled disabled Rules. Its value is <\/span><b>true<\/b><span style=\"font-weight: 400;\"> or <\/span><b>false<\/b><\/li>\n<li aria-level=\"1\"><b>setCustomerGroupIds()<\/b><span style=\"font-weight: 400;\"> Method used for <\/span><b>\u00a0Customer Groups<\/b><span style=\"font-weight: 400;\"> field value. Parameters type Should Array-like <\/span><i><span style=\"font-weight: 400;\">[0, 1].\u00a0<\/span><\/i><\/li>\n<li aria-level=\"1\"><b>setCouponType()<\/b><span style=\"font-weight: 400;\">, Using this method you can set Coupon type It value will be\u00a0<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>1 (No Coupon) or<\/b><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>2 (Specific Coupon).<\/b><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>setSimpleAction() <\/b><span style=\"font-weight: 400;\">is used for set Coupon discount type means Value of\u00a0 <\/span><b>Apply<\/b><span style=\"font-weight: 400;\"> field at <\/span><b>Action<\/b><span style=\"font-weight: 400;\"> tab. Its value may be\u00a0<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>by_percent<\/b><span style=\"font-weight: 400;\"> (i.e Percent of product price discount),\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>by_fixed<\/b><span style=\"font-weight: 400;\">(i.e Fixed amount discount) oR\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>cart_fixed<\/b><span style=\"font-weight: 400;\">(i.e Fixed amount discount for the whole cart)Or\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>buy_x_get_y<\/b><span style=\"font-weight: 400;\">(i.e Buy X get Y free (discount amount is Y))<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>setDiscountAmount() <\/b><span style=\"font-weight: 400;\">is used for <\/span><b>set field Discount Amount value.<\/b><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>setDiscountQty(<\/b><span style=\"font-weight: 400;\">) is used for set\u00a0 <\/span><b>Maximum Qty Discount is Applied To <\/b><span style=\"font-weight: 400;\">field value. Its value will be an integer.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>setSimpleFreeShipping() <\/b><span style=\"font-weight: 400;\">is used to set the F<\/span><b>ree Shipping field<\/b><span style=\"font-weight: 400;\">.Its value should be string and value will be\u00a0<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>0<\/b><span style=\"font-weight: 400;\"> (I.e No),<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>1<\/b><span style=\"font-weight: 400;\"> (I.e For matching items only),<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>2<\/b><span style=\"font-weight: 400;\"> ( I.e For shipment with matching items).<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">This coupon code can be created in many scenarios like in refund process, offer section, audience gather to plan, promotions, etc<\/span><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As we know coupon codes are the combination of letters and digits which are given to the customer to avail a special discount offer at the checkout page of an online store. Nowadays it&#8217;s the basic technique to attract customers to online stores. In this blog, we will create coupon codes programmatically.\u00a0 For this we [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6912,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[681],"tags":[683,685,682,684,686],"class_list":["post-6893","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento-2","tag-generate-coupon-codes-in-magento-2","tag-how-to-setup-coupon-promotion-codes","tag-magento-2-generate-coupon-code","tag-set-up-discounts-in-magento","tag-setup-coupon-promotion-codes-in-magento-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Create Coupon or Discount Codes Programmatically in Magento2 ?<\/title>\n<meta name=\"description\" content=\"Create Coupon Programmatically in Magento 2 is the process of first create a Sales rule with basic details for generate the new coupon.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.innovationm.com\/blog\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Create Coupon or Discount Codes Programmatically in Magento2 ?\" \/>\n<meta property=\"og:description\" content=\"Create Coupon Programmatically in Magento 2 is the process of first create a Sales rule with basic details for generate the new coupon.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-23T04:55:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-02T06:51:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2021\/07\/how-can-we-create-codes-1024x576.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"576\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"How can we create coupon or discount codes programmatically in magento 2 ?\",\"datePublished\":\"2021-07-23T04:55:47+00:00\",\"dateModified\":\"2021-08-02T06:51:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\\\/\"},\"wordCount\":359,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/how-can-we-create-codes.png\",\"keywords\":[\"Generate coupon codes in Magento 2\",\"How to Setup Coupon\\\/Promotion Codes\",\"magento 2 generate coupon code\",\"Set Up Discounts in Magento\",\"Setup Coupon\\\/Promotion Codes in Magento 2\"],\"articleSection\":[\"Magento 2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\\\/\",\"name\":\"How to Create Coupon or Discount Codes Programmatically in Magento2 ?\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/how-can-we-create-codes.png\",\"datePublished\":\"2021-07-23T04:55:47+00:00\",\"dateModified\":\"2021-08-02T06:51:51+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"description\":\"Create Coupon Programmatically in Magento 2 is the process of first create a Sales rule with basic details for generate the new coupon.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/how-can-we-create-codes.png\",\"contentUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/how-can-we-create-codes.png\",\"width\":3556,\"height\":2000},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How can we create coupon or discount codes programmatically in magento 2 ?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"InnovationM - Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"http:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Create Coupon or Discount Codes Programmatically in Magento2 ?","description":"Create Coupon Programmatically in Magento 2 is the process of first create a Sales rule with basic details for generate the new coupon.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.innovationm.com\/blog\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\/","og_locale":"en_US","og_type":"article","og_title":"How to Create Coupon or Discount Codes Programmatically in Magento2 ?","og_description":"Create Coupon Programmatically in Magento 2 is the process of first create a Sales rule with basic details for generate the new coupon.","og_url":"https:\/\/www.innovationm.com\/blog\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\/","og_site_name":"InnovationM - Blog","article_published_time":"2021-07-23T04:55:47+00:00","article_modified_time":"2021-08-02T06:51:51+00:00","og_image":[{"width":1024,"height":576,"url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2021\/07\/how-can-we-create-codes-1024x576.png","type":"image\/png"}],"author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"How can we create coupon or discount codes programmatically in magento 2 ?","datePublished":"2021-07-23T04:55:47+00:00","dateModified":"2021-08-02T06:51:51+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\/"},"wordCount":359,"commentCount":0,"image":{"@id":"https:\/\/www.innovationm.com\/blog\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2021\/07\/how-can-we-create-codes.png","keywords":["Generate coupon codes in Magento 2","How to Setup Coupon\/Promotion Codes","magento 2 generate coupon code","Set Up Discounts in Magento","Setup Coupon\/Promotion Codes in Magento 2"],"articleSection":["Magento 2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\/","url":"https:\/\/www.innovationm.com\/blog\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\/","name":"How to Create Coupon or Discount Codes Programmatically in Magento2 ?","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\/#primaryimage"},"image":{"@id":"https:\/\/www.innovationm.com\/blog\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2021\/07\/how-can-we-create-codes.png","datePublished":"2021-07-23T04:55:47+00:00","dateModified":"2021-08-02T06:51:51+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"description":"Create Coupon Programmatically in Magento 2 is the process of first create a Sales rule with basic details for generate the new coupon.","breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.innovationm.com\/blog\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\/#primaryimage","url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2021\/07\/how-can-we-create-codes.png","contentUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2021\/07\/how-can-we-create-codes.png","width":3556,"height":2000},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/how-can-we-create-coupon-or-discount-codes-programmatically-in-magento-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How can we create coupon or discount codes programmatically in magento 2 ?"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"InnovationM - Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["http:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/6893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=6893"}],"version-history":[{"count":0,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/6893\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media\/6912"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=6893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=6893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=6893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}