1

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
2

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 | |
1

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();
        $objectManager->create('Magento\Catalog\Model\Session');
        $objectManager->create('Magento\Catalog\Model\Session');
        $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());
$item->setProduct($product);
$validate = $rules->getActions()->validate($product);
            if ($validate == true) {

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

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] )

https://magento.stackexchange.com/a/303127/85990

| 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.