Learn to Validate Condition Rules in a Custom Module in Magento 2 Using ruleFactory
Are you looking for a method to validate condition rules in a custom module for Magento 2? Read this blog post to find the complete programmatic method.👇
Cart price rules in Magento 2 are useful for offering discounts based on conditions. It helps merchants get more sales by crafting discount offers strategically. The Magento platform validates the conditions and applies the discount offers on eligible orders. If you’re working on a custom Magento 2 module, which requires interacting with the checkout process, you may need to validate the condition rules.
Recently, I faced a similar situation while working on a custom module. I wanted to get all the cart price rules and validate them through custom modules in Magento 2. On my first try, I was able to get the list of custom price rules, but it does not seem to validate the rules. Later on, I found a working method to validate condition rules in Magento 2 custom module.
Let’s see how to do it.
Method to Validate Condition Rules in a Custom Module for Magento 2
In Magento 2, the Rule Factory class is used to create rule objects for various conditions, such as shopping cart price rules, catalogue price rules, and promotion rules. This can be done by using the Magento\Rule\Model\RuleFactory
class in Magento 2.
You need to create models to load the available rules and validate them through Rule Factory. This ensures that only a valid cart price is applied to the quote. To do that, download this rule.rar file, and extract it inside the app/code/Vendor/Module/Model/
directory. Further, you’ll also need to create message.php in the same directory with the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
<?php namespace Vendor\Module\Model; use Magento\Quote\Model\Quote\Address; use Magento\Rule\Model\AbstractModel; use Magento\Framework\Model\Context; use Magento\Framework\Registry; use Magento\Framework\Data\FormFactory; use Magento\Framework\Stdlib\DateTime\TimezoneInterface; use Magento\SalesRule\Model\Rule\Condition\CombineFactory; use Magento\SalesRule\Model\Rule\Condition\Product\CombineFactory as ProductCombineFactory; use Magento\Framework\Model\ResourceModel\AbstractResource; use Magento\Framework\Data\Collection\AbstractDb; /** * Class Message * @package Vendor\Module\Model */ class Message extends AbstractModel { /** * @var string */ protected $_eventPrefix = 'Vendor_Module'; /** * @var string */ protected $_eventObject = 'Vendor_message'; /** * @var CombineFactory */ protected $condCombineFactory; /** * @var ProductCombineFactory */ protected $condProdCombineF; /** * @var array */ protected $validatedAddresses = []; /** * Message constructor. * @param Context $context * @param Registry $registry * @param FormFactory $formFactory * @param TimezoneInterface $localeDate * @param CombineFactory $condCombineFactory * @param ProductCombineFactory $condProdCombineF * @param AbstractResource|null $resource * @param AbstractDb|null $resourceCollection * @param array $data */ public function __construct( Context $context, Registry $registry, FormFactory $formFactory, TimezoneInterface $localeDate, CombineFactory $condCombineFactory, ProductCombineFactory $condProdCombineF, AbstractResource $resource = null, AbstractDb $resourceCollection = null, array $data = [] ) { $this->condCombineFactory = $condCombineFactory; $this->condProdCombineF = $condProdCombineF; parent::__construct($context, $registry, $formFactory, $localeDate, $resource, $resourceCollection, $data); } /** * */ protected function _construct() { parent::_construct(); $this->_init('Vendor\Module\Model\ResourceModel\Message'); $this->setIdFieldName('id'); } /** * @return \Magento\Rule\Model\Condition\Combine|\Magento\SalesRule\Model\Rule\Condition\Combine */ public function getConditionsInstance() { return $this->condCombineFactory->create(); } /** * @return \Magento\Rule\Model\Action\Collection|\Magento\SalesRule\Model\Rule\Condition\Product\Combine */ public function getActionsInstance() { return $this->condProdCombineF->create(); } /** * @param $address * @return bool */ public function hasIsValidForAddress($address) { $addressId = $this->_getAddressId($address); return isset($this->validatedAddresses[$addressId]) ? true : false; } /** * @param $address * @param $validationResult * @return $this */ public function setIsValidForAddress($address, $validationResult) { $addressId = $this->_getAddressId($address); $this->validatedAddresses[$addressId] = $validationResult; return $this; } /** * @param $address * @return bool|mixed */ public function getIsValidForAddress($address) { $addressId = $this->_getAddressId($address); return isset($this->validatedAddresses[$addressId]) ? $this->validatedAddresses[$addressId] : false; } /** * @param $address * @return mixed */ private function _getAddressId($address) { if ($address instanceof Address) { return $address->getId(); } return $address; } } |
Once you’ve created the required models in the custom module, you can access and validate condition rules.
Now, let’s say we want to validate Magento 2 custom rule condition on order placement. We can use validate()
function, for example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?php namespace \Module\Observer; use Psr\Log\LoggerInterface; use Magento\Quote\Model\QuoteFactory; use Vendor\Module\Model\RuleFactory; use Magento\Store\Model\StoreManagerInterface; use Magento\Sales\Model\OrderFactory; /** * Class OrderPlace * @package Vendor\Module\Observer */ class OrderPlace implements \Magento\Framework\Event\ObserverInterface { /** * @var LoggerInterface */ protected $logger; /** * @var StoreManagerInterface */ protected $_storeManager; /** * @var RuleFactory */ protected $_ruleFactory; /** * @var QuoteFactory */ protected $_quoteFactory; protected $_order; /** * OrderPlace constructor. * @param LoggerInterface $logger * @param QuoteFactory $quoteFactory * @param Order $_order * @param RuleFactory $ruleFactory * @param Data $data * @param StoreManagerInterface $storeManager */ public function __construct( QuoteFactory $quoteFactory, RuleFactory $ruleFactory, OrderFactory $_order, LoggerInterface $logger, StoreManagerInterface $storeManager ) { $this->logger = $logger; $this->_storeManager = $storeManager; $this->_ruleFactory = $ruleFactory; $this->_quoteFactory = $quoteFactory; $this->_order = $_order; } /** * @param \Magento\Framework\Event\Observer $observer */ public function execute(\Magento\Framework\Event\Observer $observer) { try { $orderIds = $observer->getEvent()->getOrderIds(); $orderId = $orderIds[0]; $order = $this->_order->create()->load($orderId); $model = $this->_ruleFactory->create(); // custom rule condition model // load quote from quote id $quoteId = $order->getQuoteId(); $quote = $this->_quoteFactory->create()->load($quoteId); // load address from quote for validate with custom rule condition if ($quote->getShippingAddress()) { $address = $quote->getShippingAddress(); } else { $address = $quote->getBillingAddress(); } // load quote product item for validate with custom rule condition $items = $quote->getAllItems(); $ruleData = $model->getCollection(); // here we load all the rule from custom rule table from custom module and also validate the address and item with it foreach ($ruleData as $rule) { // return boolean value true or false as validate address and item from custom rule $validate = $rule->validate($address, $items); // add your code base on validate true or false } return true; } catch (\Exception $e) { $this->logger->info($e->getMessage()); } } } |
Note: Do not forget to replace Vendor
and Module
names in the above code.
So, that’s it.
Also you may need to add rule condition field in Magento 2 admin UI component form if you want to customize your own feature for making your shopping platform better because of the growing online stores.
You can use the above method to validate condition rules in a custom module for Magento 2.
Did this tutorial help you? Share it with your developer friends via social media.
In case you still face any queries, feel free to comment. I would be happy to help you! 😊
Thank you! 🍀
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
Prev
How to Transfer From Wix to Shopify [100% Manual Process]
Bing Search Algorithm: What Are The Top Ranking Factors & How it Works?
Next