Magento – Magento 2 how to check serialized conditions against product in frontend

catalog-rulesfrontendmagento2.3product-listingproduct-view-page

I have created an admin form which contains name and catalog price rule conditions. I have saved these values in a custom table. The conditions are saved as serialized. Now I want to check these conditions against each product in catalog product list page, product page, widgets etc. If the condition satisfied then need to display the name in each product.

How can I do this. Please help me.

Best Answer

You can use as example the standard Magento class Magento\Rule\Model\AbstractModel or just extend it in your custom class. The algorithm is pretty simple:

1) Load conditions data ($conditionsSerialized = $this->getConditionsSerialized())) and unserialize it ($conditions = $this->serializer->unserialize($conditionsSerialized);)

2) Get the conditions object (instance of \Magento\Rule\Model\Condition\Combine or what ever you use) and load it using recently loaded data as array ($this->_conditions->loadArray($conditions);)

3) Validate your object using this condition ($this->_conditions->validate($object);)

Related Topic