0

how to display list of valid coupon code on cart page in magento 2

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

Above code is not working.

0

You have to filter the collection with "is_acrive" filed.

<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();  
    $SalesRule = $objectManager->create('Magento\SalesRule\Model\Rule');
    $getRules = $SalesRule->getCollection();
    $getRules->addFieldToFilter('is_active', 1);
    $activerulecoll = $getRules->getData();

    foreach ($activerulecoll as $rule) {
        print_r($rule['rule_id']);

    }
?>
  • getting same out put. I need coupon code which can apply to the cart product wise or cart wise not all list of coupon code. – 55840 Apr 13 at 6:24
  • I got all coupon code of other brand and category too. I want to show product/cart specific coupon code which is valid to apply. – 55840 Apr 13 at 6:31

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.