How to Hide or Show Custom Attributes Based on Another Attribute in Magento 2.4.x Product Detail/Edit Page
Are you looking for a way to dynamically hide or show custom attributes based on the value of another attribute in your Magento 2.4.x product detail or edit page? You’ve come to the right place! In this comprehensive guide, we’ll walk you through the process step by step.
Understanding the Problem
Let’s say you have created custom attributes programmatically and want to tie their visibility to a toggle button. When the user clicks “Yes” on the Enable attribute, you want to show the Label, Allow Add To Cart, and Disclose Product Price fields. If they click “No”, these fields should disappear.
Here are the relevant code snippets from your Setup/InstallData.php file:
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
// ... (other attribute setup codes)
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'inquiry_attribute_enable',
[
'group' => 'Product Inquiry',
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Enable',
'input' => 'boolean',
// ... (other attribute configuration)
]
);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'inquiry_attribute_label',
[
'group' => 'Product Inquiry',
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'Label',
// ... (other attribute configuration)
]
);
// ... (other attributes setup codes)
// add attribute to group
$eavSetup->addAttributeToGroup(
\Magento\Catalog\Model\Product::ENTITY,
$attributeSetId,
$attributeGroupName,
'inquiry_attribute_enable',
10
);
$eavSetup->addAttributeToGroup(
\Magento\Catalog\Model\Product::ENTITY,
$attributeSetId,
$attributeGroupName,
'inquiry_attribute_label',
20
);
// ... (other attributes adding to group)
$setup->endSetup();
}
The Solution: Using Magento’s Built-in Features
The good news is that Magento 2.4.x provides built-in functionality to handle this kind of logic. We can use the visible configuration option for each attribute and update it based on the value of the Enable attribute.
Here’s how you can achieve this:
Step 1: Modify the Attribute Configuration
In your Setup/InstallData.php, modify the ‘inquiry_attribute_label’, ‘inquiry_attribute_allow_cart’, and ‘inquiry_attribute_disclose_price’ attributes as follows:
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'inquiry_attribute_label',
[
// ... (other configurations)
'visible' => function ($product) {
return $product->getData('inquiry_attribute_enable') == 1;
},
// ... (other configurations)
]
);
// Repeat the above pattern for 'inquiry_attribute_allow_cart' and 'inquiry_attribute_disclose_price'
In each attribute, we are setting the visible option to a closure function. This function will be executed whenever Magento needs to determine if this attribute should be visible or not.
The function checks the value of the ‘inquiry_attribute_enable’. If it is 1 (which means “Yes”), the attribute will be visible; otherwise, it will be hidden.
Step 2: Update Your Theme Files
Make sure your theme files are correctly configured to display these attributes based on their visibility. You can check this in the app/design/frontend/<Vendor>/<Theme>/Magento_Catalog/templates/product/view/form.phtml file:
<div class="admin__field field required">
<label for="inquiry_attribute_label" class="admin__field-label">
<span><?= $block->escapeHtml(__('Label')) ?></span>
</label>
<div class="admin__field-control">
<?= $block->getAttributeHtml('inquiry_attribute_label', 'edit') ?>
</div>
</div>
<!-- Repeat the above pattern for 'inquiry_attribute_allow_cart' and 'inquiry_attribute_disclose_price' -->
The getAttributeHtml method will only render the attribute field if it is visible.
Conclusion
In this guide, we’ve demonstrated a practical solution to your challenge. By leveraging Magento’s built-in features, we can easily hide or show custom attributes based on another attribute’s value without writing any additional code.
Remember, the key lies in correctly configuring the visible option in your attributes and ensuring that your theme files are properly set up to display these attributes based on their visibility status.
If you found this guide helpful, please consider sharing it with your fellow Magento developers. Also, feel free to explore our blog at Laravelcompany for more insightful articles on Laravel and other web development topics!
Happy coding!