2

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

2 Answers 2

2

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

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

    /**
     * @return false
     * @throws \Zend_Db_Select_Exception
     */
    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 and acknowledge you have read our privacy policy.

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