0

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

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

Magento_Catalog\templates\product\view\details.phtml

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

<?php

$productView=$block->getLayout()->createBlock('Magento\Catalog\Block\Product\View');
$product=$productView->getProduct();

?>

<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 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.