5

How can we can place an if condition on proudct attibutes in a frontend .phtml file

enter image description here

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.

1 Answer 1

3

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;
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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