Showcasing Configurable Product Options as Interactive Divs in Magento
In Magento, configurable products are typically displayed with dropdown menus for selecting options like size or color. However, sometimes it’s more visually appealing to present these options as clickable divs within a grid layout. Let’s explore how you can achieve this by modifying the frontend template and adding some custom JavaScript.
Understanding the Current Behavior
Magento uses JSON responses to populate the dropdown menus for configurable products. This configuration is managed through js/varien/product.js. The default behavior is defined in the configurable.phtml template located at template/catalog/product/view/type/options/configurable.phtml.
<?php if ($_product->isConfigurable()): ?>
<div class=" configurables">
<?php $_options = $this->getJsonConfig(); ?>
<?php echo '
<ul class="super-attribute-select">';
$resultado = json_decode($_options, true);
// Process the options here...
echo '</ul>
<div class="clear"></div>';
?>
</div>
<?php endif; ?>
Modifying the Configurable Options Template
To replace the dropdown menu with clickable divs, we’ll need to modify this template. We can iterate through the JSON configuration and output each option as an a element within a li.
<div class=" configurables">
<?php $_options = $this->getJsonConfig(); ?>
<ul class="super-attribute-select">
<?php
$resultado = json_decode($_options, true);
foreach ($resultado['attributes'] as $att) {
echo '<li>';
foreach($att['options'] as $option){
echo '<a value="'.$option['id'].'" price="'.$option['price'].'" href="javascript:void(0)" onclick="return assignValue(\''.$option['value'].'\')">'. $option['label'] .'</a>';
}
echo '</li>';
}
?>
</ul>
<div class="clear"></div>
</div>
Creating the Custom JavaScript Function
We’ll need to create a function called assignValue that will be triggered when an option is clicked. This function should assign the selected value to a hidden input field and apply a “selected” class to the clicked element.
<script>
function assignValue(value) {
// Assign the value
document.getElementById('super_attribute[162]').value = value;
// Apply the 'selected' class to the clicked item
this.addClassName("selected");
}
</script>
Ensuring the Value is Required
To ensure that a selected option is required (so users can’t leave it blank), you might want to add the required-entry class to the hidden input field. This will trigger Magento’s built-in validation.
<input type="hidden" name="super_attribute[162]" id="super_attribute[162]" value="" class="required-entry">
Conclusion
Modifying the frontend template and adding custom JavaScript allows for a more interactive display of configurable product options. By converting dropdown menus into clickable divs, you can create a more engaging user experience.
Remember to thoroughly test your changes, especially regarding validation and form submission, to ensure they work correctly across different browsers and devices.
For more advanced customizations and best practices in Magento development, feel free to explore our comprehensive guide at Laravel Company’s Magento Development Resources.
Happy coding!