Magento 2.2.5 : How to place a check on prouduct page on product attributes
-
26-01-2021 - |
Question
How can we can place an if condition on proudct attibutes in a frontend .phtml file
In front end .phtml which is associated with our handle in custom module layout catalog_product_view , we get serialized_condition as
{"type":"Vendor\Module\Model\Rule\Condition\Combine","attribute":null,"operator":null,"value":"1","is_value_processed":null,"aggregator":"all","conditions":[{"type":"Vendor\Module\Model\Rule\Condition\Product","attribute":"category_ids","operator":"==","value":"2","is_value_processed":false}]}
How to get all attibutes and place an if condition on the attributes which matches our values in db table column name serialized_conditions like , if either the Sku ,climate,collar etc matches with the current product- do certain operation? Even if someone can provide directions about how it is done in the core files would be appreciated.
La solution
You can use magento2 core condition validate function , try the below code in your block or wherever you need it.
$id = 10;
$rule = $this->rule->create()->load($id);
if ($rule->getConditions()->validate($this->getCurrentProduct())) {
return true;
}