I have create a custom attribute for a product. The attribute name is "hide" When product load at product detail page, if this product custom attribute is "hide", it will apply a css to hide certain class label in the product details page.

If I want to build this as a custom module, what is the proper way to do it? anyone can give me samples or reference that I can follow?

1 Answer 1


It is unclear from your question where the html element is on the product page that you wish to show/hide. If it is in the details.phtml template you can extend this in your module or theme at


Check for your custom attribute and apply the css class name to the html element you want to hide/show:




<div class="<?= ($product->getCustomAttributeName()==='hide' ? 'hide':'') ?>"</div>

You will find many examples on how to create custom modules for Magento 2. One useful tool is the Mage2Gen module creator which lets you quickly build the files you need for your module template.

Your Answer

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

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