Displaying Configurable Swatches on Custom Category Pages in Magento 1.9
If you’re trying to display configurable swatches on custom category pages in your Magento 1.9 store, you’ve come to the right place! In this post, we’ll walk through a step-by-step guide to help you achieve this.
The community edition of Magento provides built-in support for displaying configurable swatches on product and category view pages. However, if you want to display these swatches on a custom ‘Shop’ page that lists all products, you might encounter some challenges.
Understanding the Current Approach
You’ve already set up your local.xml file to list products on the custom Shop page:
<mystore_site_index_shop>
<reference name="content">
<block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
... (other blocks)
</block>
</reference>
</mystore_site_index_shop>
You’ve also added a reference to the page in configurableswatches.xml:
<mystore_site_index_shop>
<update handle="product_list"/>
</mystore_site_index_shop>
This should load the necessary JavaScript files for the swatches. However, when you render the page, you notice that no swatches are displayed.
The Missing Piece
The issue lies in how Magento loads the swatch data. By default, it loads the data based on the category and product IDs used in the URL. When you create a custom Shop page listing all products, Magento doesn’t have this context to work with.
To fix this, we need to modify the way Magento loads the swatch data for our custom page. We’ll do this by creating a new block that extends the core Mage_ConfigurableSwatches_Block_Catalog_Product_List_Swatch block and overriding its _getOptionCollection method.
Creating a Custom Swatch Block
-
Create a new module, let’s call it ‘MyCustomSwatches’.
-
In your module’s config.xml file, add the following:
<config>
<modules>
<MyCustomSwatches>
<version>0.1.0</version>
</MyCustomSwatches>
</modules>
<global>
<blocks>
<mycustomswatches>
<class>MyCustomSwatches_Block</class>
</mycustomswatches>
</blocks>
</global>
</config>
-
Create a new file
app/code/local/MyCustomSwatches/Block/Catalog/Product/List/Swatch.php. -
In this file, extend the core swatch block and override the
_getOptionCollectionmethod:
class MyCustomSwatches_Block_Catalog_Product_List_Swatch extends Mage_ConfigurableSwatches_Block_Catalog_Product_List_Swatch
{
public function _getOptionCollection()
{
$product = $this->getProduct();
// Load all products from the database to get swatch data
$products = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->load();
foreach ($products as $product) {
if ($product->isObjectNew()) {
continue;
}
$options = $this->_getProductOptions($product);
if (!empty($options)) {
foreach ($options as $optionId => $_options) {
if (!$this->hasOptionCollection($optionId)) {
$this->setOptionCollection($optionId, $_options);
}
}
}
}
return parent::_getOptionCollection();
}
}
Updating the Layout XML
- Update your local.xml file to use the new swatch block:
<mystore_site_index_shop>
<reference name="content">
<block type="mycustomswatches/catalog_product_list_swatch" name="product_list_swatch" template="catalog/product/list/swatch.phtml"/>
</reference>
</mystore_site_index_shop>
Conclusion
By following the steps outlined above, you should now be able to display configurable swatches on your custom Shop page in Magento 1.9.
Remember that this approach relies on a custom block to load all product data from the database. If your store has a large number of products, this could potentially impact performance. Always consider optimization techniques like caching and indexing when working with large datasets in Magento.
For more advanced customizations and performance optimizations, be sure to check out our Magento Development Services at Laravel Company!
Happy coding!