2

I have the sales rules collection in a helper file got the rules collection from this bit of code. Now I want to validate if the product id needs to validate against sales rule.

Constructor contains this

\Magento\SalesRule\Model\RuleFactory $ruleFactory,
$this->ruleFactory = $ruleFactory;

and the public function contains this

$_rules = $this->ruleFactory->create()->getCollection();
foreach($_rules as $rule){
            $fromDate = $rule->getFromDate();
            $toDate = $rule->getToDate();
            if (isset($fromDate) && $_currentTime >= strtotime($fromDate)) {
                if (isset($toDate)) {
                    if (strtotime($toDate) >= $_currentTime) {
                        print_r($products_id);
                        print_r($rule->getData());
                    }
                }
            }
        }

Can any one help on this.

2 Answers 2

3

I found a solution on this below is the way I did and it worked.

$products_id = $id;
    $_rules = $this->ruleFactory->create()->getCollection();

    $_currentTime = strtotime($this->datetime->date());
    $objectManager = $this->_objectManager;

    $html = " ";
    foreach($_rules as $rule){
        $fromDate = $rule->getFromDate();
        $toDate = $rule->getToDate();
        if (isset($fromDate) && $_currentTime >= strtotime($fromDate)) {
            if (isset($toDate)) {
                if (strtotime($toDate) >= $_currentTime) {
                    $product = $objectManager->get('Magento\Catalog\Model\Product')->load($products_id);
                    $item = $objectManager->create('Magento\Catalog\Model\Product');
                    $item->setProduct($product);
                    //Return True if Sales Rule validate product
                    $validate = $rule->getActions()->validate($item);
                    if($validate){
                        if($rule->getId()=='8') $html .= '<span class="coupon-label"><span>'.$rule->getName().'</span></span>';
                    }
                }
            }
        }
    }
1
  • It will not always work, as it is checking the entire quote Item, Once you set the condition with subselect it will fail :)
    – Keyur Shah
    Sep 19, 2019 at 9:29
0

Try This Code

protected $quoteRepository;
protected $ruleData;

public function __construct(
    ...................................................................
    \VendoreName\ModuleName\Model\CustomRuleDataFactory $quoteRepository,
    \Magento\Quote\Api\CartRepositoryInterface $ruleData,
    ...................................................................
) {
    ...................................................................
    $this->quoteRepository = $quoteRepository;
    $this->ruleData = $ruleData;
    ...................................................................
}

public function execute()
{
    $address = 0;
    $quotesVal = 15
    $quotesData = $this->quoteRepository->get($quotesVal);
    $customerId = (int) $quotesData->getCustomerId();
    $productCount = 0;
    foreach ($quotesData->getAllItems() as $quotesDataKey => $quotesDataVal) {
        $address = $quotesDataVal->getAddress();
        $productCount++;
        break;
    }
    if ($productCount) {
        $address->setCachedItemsAll($quotesData->getAllItems());
        $cartRuleData = $this->ruleData->create()->getCollection();
        foreach ($cartRuleData as $ruleKey => $ruleVal) {
            if ($ruleVal->getConditions()->validate($address)){ // if ($ruleVal->getActions()->validate($address))
                echo "Rule Applied";
            }
        }
    }
}

I Hope This Helps You.

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.