1

I have a collection of cart price rules. Now I want to validate the rule without a quote, quote item, or any product. If it's valid then I want to do some customize based on it, or not then I want to skip that rule.

Thanks in advance.

1 Answer 1

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, privacy policy and cookie policy

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