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.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityTry 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.