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 at 8:38
0

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;
            }
        }
     }

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.