How To Programmatically Create Coupon Code In Magento 2
Discount is one serious hack in online stores and that’s how people do businesses in a competitive market.
People tend to love a good deal, it’s human nature. When you are an online business, sometimes, offering the right coupon code to the right person can push conversion.
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.
The post gives the method to programmatically create coupon code in Magento 2 store and attract potential customers to use the coupon code, resulting in conversion!
Method To Programmatically Create Coupon Code In Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?php use Magento\Framework\App\Bootstrap; require __DIR__ . '/app/bootstrap.php'; $params = $_SERVER; $bootstrap = Bootstrap::create(BP, $params); $obj = $bootstrap->getObjectManager(); $state = $obj->get('Magento\Framework\App\State'); $state->setAreaCode('adminhtml'); $coupon['name'] = 'Test Rule'; $coupon['desc'] = 'Test Rule'; $coupon['start'] = date('Y-m-d'); $coupon['end'] = ''; $coupon['max_redemptions'] = 1; $coupon['discount_type'] ='by_percent'; $coupon['discount_amount'] = 50; $coupon['flag_is_free_shipping'] = 'no'; $coupon['redemptions'] = 1; $coupon['code'] ='OFF50'; $shoppingCartPriceRule = $obj->create('Magento\SalesRule\Model\Rule'); $shoppingCartPriceRule->setName($coupon['name']) ->setDescription($coupon['desc']) ->setFromDate($coupon['start']) ->setToDate($coupon['end']) ->setUsesPerCustomer($coupon['max_redemptions']) ->setCustomerGroupIds(array('0','1','2','3',)) ->setIsActive(1) ->setSimpleAction($coupon['discount_type']) ->setDiscountAmount($coupon['discount_amount']) ->setDiscountQty(1) ->setApplyToShipping($coupon['flag_is_free_shipping']) ->setTimesUsed($coupon['redemptions']) ->setWebsiteIds(array('1')) ->setCouponType(2) ->setCouponCode($coupon['code']) ->setUsesPerCoupon(NULL); $shoppingCartPriceRule->save(); |
Generate coupon codes in Magento 2 store and effectively use the discount strategies to benefit the business.
If you are stuck in between, feel free to mention your doubts in the Comments section below. I’d be happy to help.
Help Magento store owners manage discounts and coupon codes by sharing this solution via social media.
Thanks.
Related Articles:
Magento 2 Coupon Code Link extension makes it easy to apply coupon code using a link instead of manually typing the code.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
4 Comments
Hi, nice post. Really informational about
Hello,
Thank you for your appreciation.
Thanks for sharing this method. I was facing very hard time in creating the magento coupon code. But, thanks to this resource I was able to implement it on my affiliate websites where I enlisted the Cloudways magento hosting black friday deals.
Hi Julie,
I’m glad the post was useful to you.
Thanks.