1

I want to show cart price rule on product page. How to get collection of cart price rules?

1

try this way.. this code working for me

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product_discamt = 0;
$objrules = $objectManager->create('Magento\SalesRule\Model\RuleFactory')->create();
$rules = $objrules->getCollection();
foreach ($rules as $tmprule) {
          $rule = $objectManager->create('Magento\SalesRule\Model\Rule')->load($tmprule->getId());
          $product = $objectManager->get('Magento\Catalog\Model\Product')->load($_product->getId());
          $item = $objectManager->create('Magento\Catalog\Model\Product');
          $item->setProduct($product);                                
          if ($rule->getActions()->validate($item) && $rule->getIsActive()) {
              $product_discamt=$rule->getDiscountAmount();
          }
  }

  if($product_discamt>0){?>

  <span class="cart-rule">-<?php echo round($product_discamt);?><span class="cart-rule-percentage">%</span></span>
</div>
}?>
0
/**
     * @var RuleCollection
     */
<?php

namespace Veera\Sales\Controller\Index;

use Magento\Framework\App\Action\Context;
use Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory as RuleCollection;

class Validatecode extends \Magento\Framework\App\Action\Action
{
    /**
     * @var RuleCollection
     */
    protected $_ruleCollection;

    public function __construct(
        Context $context,
        RuleCollection $ruleCollection
    ) {
        parent::__construct($context);
        $this->_ruleCollection = $ruleCollection;
    }

    public function execute()
    {
        /** @var Collection $collection */
        $rules = $this->_ruleCollection->create();
        $rules->setValidationFilter($websiteId, $customer->getGroupId(), trim($post['code']))
            ->addFieldToFilter('rule_coupons.code', ['notnull' => true])
            ->addFieldToFilter('rule_coupons.times_used', 0)
            ->load();

        return false;        
    }
}

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.