How to Get All Coupon Code of Particular Cart Price Rules in Magento 2
Providing attractive discounts on weekends, festivals, first-order, etc., is one of the best tactics to boost sales quickly for the Magento 2 stores. The store owner can create coupon codes for offering discounts and promote them on various advertising mediums.
Coupon codes are generated and applied with Cart Price Rules – which allows applying discounts in the shopping cart based on a set of conditions and coupon codes. Discounts are applicable only when a customer enters a valid coupon code on the shopping cart page.
A store owner can easily create coupon codes as well as multiple ones but what if the requirement arises to get all coupon code of particular cart price rules in Magento 2?
For instance, an admin needs to get the list of coupon codes in the grid or CSV. Additionally, one may also need to provide all coupon codes to a marketing team for advertising purposes!
Also Read: How to Auto Apply Coupon Code in Magento 2
In such a scenario, use the below code and get all coupon codes of particular cart price rules and print according to the business requirement.
Method to Get All Coupon Code of Particular Cart Price Rules in Magento 2
Use the below code in your php file.
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 |
<?php ini_set('memory_limit', -1); use Magento\Framework\App\Bootstrap; require 'app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $state = $objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); $om = \Magento\Framework\App\ObjectManager::getInstance(); $couponFactory = $om->get('Magento\SalesRule\Model\CouponFactory'); $ruleId = 2296; $p = 0; while (1) { $p++; $couponCodeData = $couponFactory->create() ->getCollection() ->addFieldToFilter('rule_id', $ruleId) ->setPageSize(20) ->setCurPage($p); foreach ($couponCodeData as $item) { echo $item->getcode() . '</br>'; } if ($p >= $couponCodeData->getLastPageNumber()) { break; } } |
That’s it.
If you have questions regarding this post, feel free to ask in the comment section below.
I would be happy to answer.
Do consider sharing this post with Magento Community via social media.
Thank you.
Magento 2 Coupon Code Link extension makes it easy to apply coupon code using a link instead of manually typing the code.
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
2 Comments
I am VERY interested in this extention – However I have a few specific questions. We currently generate coupon codes on the backend based on product catagory. BUT when we offer sales or coupon codes we need to manage either by catagory ( which we are doing now) or by different discouints based on different products. When our vender releases a sales flyer we MUST adhear to MAP pricing rules, which means we cannont list the product of any other price than liosted in the sales flyer, so we use coupons. BUT – not every product in the sales flyer has the same discount to us, it can range for 10 percent to 30 percent so making a discount coupon for 10% doesn’t work – we need some products at 10% and others at 15%. Can this extention accounidtaae that?
Hey Douglas,
This code displays the coupon list only for a specific cart price rule.
Thank You.