Displaying Color and Size for Simple Products in Magento: A Step-by-Step Guide
As an experienced Magento developer, I often receive queries related to displaying product attributes like color and size on the frontend. While Magento provides built-in options for configuring these attributes with configurable products, it can be more challenging with simple products.
In this guide, we’ll explore how you can easily display color and size information for your simple products in a Magento 2 storefront.
Step 1: Create Custom Attributes
Firstly, ensure that the color and size attributes are created in your Magento admin panel. Go to Stores > Attributes > Product > Add New Attribute.
- For Color: Create an attribute named “Color” with type “Text”.
- For Size: Create an attribute named “Size” with type “Text”.
Remember to save these new attributes after defining their properties.
Step 2: Assign the Attributes
Next, assign these custom attributes to your simple product:
- Go to the product you want to edit.
- Scroll down to the “Attributes and Variations” section.
- Click on “Add Attribute”.
- Select both “Color” and “Size” from the dropdown menu.
- Enter the appropriate values for each attribute (e.g., Size: S, Color: Red).
- Save your changes.
Step 3: Displaying Attributes in Product View
To display these attributes on your product page, you’ll need to modify your theme’s templates. Here’s a simple way to do it:
-
Navigate to your Magento installation directory.
-
Locate the directory for your current theme (e.g., app/design/frontend/your_vendor/your_theme).
-
Find or create the template file catalog_product_view_type_simple.xml within the layout folder.
-
Add the following code in this XML file:
<referenceBlock name="product.info.main">
<block class="Magento\Catalog\Block\Product\View" name="color_size_attributes" as="color_size_attributes" template="catalog/product/view/attribute.phtml"/>
</referenceBlock>
-
Create a new PHTML file named attribute.phtml in the following directory: app/design/frontend/your_vendor/your_theme/Magento_Catalog/templates/catalog/product/view.
-
Add the following code to this PHTML file:
<?php if ($block->getProduct()->hasOptions()): ?>
<div class="product-attributes">
<strong><?php echo __('Color:'); ?></strong>
<span><?php echo $this->escapeHtml($block->getProduct()->getAttributeText('color')); ?></span><br/>
<strong><?php echo __('Size:'); ?></strong>
<span><?php echo $this->escapeHtml($block->getProduct()->getAttributeText('size')); ?></span>
</div>
<?php endif; ?>
Conclusion
With these steps, you should be able to display color and size attributes for your simple products seamlessly in the Magento frontend. This approach allows you to showcase multiple options without resorting to configurable or grouped products.
Remember, customizing themes requires a good understanding of Magento’s template system and XML layout updates. For more advanced customizations, I recommend checking out our comprehensive guide on Magento theme development at Laravel Company.
Don’t hesitate to reach out if you need further assistance with your Magento store setup or any other development needs!