How to get all coupon code which can apply on cart page in magento 2

$getRules = $objectManager->create('Magento\SalesRule\Model\Rule')->getCollection();

using the above code i get the all list of coupon codes. I want to display coupon code which can apply

  • I want to show list of valid coupon on cart page – 55840 Apr 5 '19 at 8:38

There is already a full fledged free extension available on magento's marketplace and github

magento: https://marketplace.magento.com/ambab-module-couponlist.html

github: https://github.com/ambab-infotech/couponlist

This might help someone.

| improve this answer | |

I have created block to get cart items using checkout session file name is CustomSession.php

protected $_checkoutSession;

public function __construct (
    \Magento\Checkout\Model\Session $_checkoutSession
    ) {
    $this->_checkoutSession = $_checkoutSession;

public function getProducts()
        $item = $this->_checkoutSession->getQuote()->getAllVisibleItems();
       return $item;

than overriding the coupon.phtml file

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $CatalogSession = $objectManager->get('\Magento\Catalog\Model\Session');
        $blockObj= $block->getLayout()->createBlock('Vendor\Contact\Block\CustomSession');  
        $SalesRule = $objectManager->create('Magento\SalesRule\Model\Rule');
        $getRules = $SalesRule->getCollection();
        $item = $objectManager->create('Magento\Catalog\Model\Product');

        $products = $blockObj->getProducts(); // this will also work using custom session block
foreach ($products as $product) {
        foreach ($getRules as $rule) {
$rules = $SalesRule->load($rule->getId());
$validate = $rules->getActions()->validate($product);
            if ($validate == true) {

                echo "coupon -".$rule->getCode()."'>".$rule->getCode()." - ".$rule->getDescription()." coupon";
                 echo "<br>";
                # code...
| improve this answer | |

Try this one as mentioned above by Yash I am already using this on my project this one is really cool. (Awesome work Yash [Ambab] )


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.