{"id":1815,"date":"2021-07-05T07:23:54","date_gmt":"2021-07-05T07:23:54","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/get-all-coupon-code-of-particular-cart-price-rules-in-magento-2\/"},"modified":"2025-07-16T17:37:07","modified_gmt":"2025-07-16T12:07:07","slug":"get-all-coupon-code-of-particular-cart-price-rules-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-all-coupon-code-of-particular-cart-price-rules-in-magento-2\/","title":{"rendered":"How to Get All Coupon Code of Particular Cart Price Rules in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Providing attractive discounts on weekends, festivals, first-order, etc., is one of the best tactics to boost sales quickly for the\u00a0Magento 2\u00a0stores. The store owner can create coupon codes for offering discounts and promote them on various advertising mediums.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Coupon codes are generated and applied with Cart Price Rules \u2013 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A store owner can easily&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/create-coupon-code-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">create coupon codes<\/a>&nbsp;as well as multiple ones but what if the requirement arises to&nbsp;<em><strong>get all coupon code of particular cart price rules in Magento 2<\/strong><\/em>?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Also Read:&nbsp;<\/strong><a href=\"https:\/\/meetanshi.com\/blog\/magento-2-auto-apply-coupon-code\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Auto Apply Coupon Code in Magento 2<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Get All Coupon Code of Particular Cart Price Rules in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use the below code in your php file.<\/p>\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\n\nini_set('memory_limit', -1);\n\nuse Magento\\Framework\\App\\Bootstrap;\n\nrequire 'app\/bootstrap.php';\n\n$bootstrap = Bootstrap::create(BP, $_SERVER);\n$objectManager = $bootstrap->getObjectManager();\n$state = $objectManager->get('Magento\\Framework\\App\\State');\n$state->setAreaCode('frontend');\n\n$om = \\Magento\\Framework\\App\\ObjectManager::getInstance();\n$couponFactory = $om->get('Magento\\SalesRule\\Model\\CouponFactory');\n$ruleId = 2296;\n$p = 0;\n\nwhile (1) {\n    $p++;\n    $couponCodeData = $couponFactory->create()\n        ->getCollection()\n        ->addFieldToFilter('rule_id', $ruleId)\n        ->setPageSize(20)\n        ->setCurPage($p);\n\n    foreach ($couponCodeData as $item) {\n        echo $item->getcode() . '&lt;\/br>';\n    }\n\n    if ($p >= $couponCodeData->getLastPageNumber()) {\n        break;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I would be happy to answer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do consider sharing this post with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Providing attractive discounts on weekends, festivals, first-order, etc., is one of the best tactics to boost sales quickly for the\u00a0Magento 2\u00a0stores. The store owner can&#8230;<\/p>\n","protected":false},"author":13,"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-1815","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1815","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\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1815"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1815\/revisions"}],"predecessor-version":[{"id":18038,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1815\/revisions\/18038"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}