Limiting Thumbnail Sliders on Magento 2 Product Pages
As you delve into Magento 2 development, customizing the user interface is an essential aspect of creating a unique shopping experience for your customers. One common requirement is to limit the number of thumbnails displayed in the product image slider.
In this guide, we’ll walk you through the steps to restrict the thumbnail slider to display only four images instead of the default five on the product page using Magento’s default Fotorama slider module.
Understanding the Default Behavior
Magento 2 comes with a pre-configured product view layout that handles image thumbnails using the Fotorama library. By default, it displays five thumbnails for each product.
The thumbnail size can be increased through your theme’s view.xml file as you mentioned in your question. However, modifying the number of displayed thumbnails requires a different approach since this configuration is not directly controlled by the template files.
Customizing the Thumbnail Slider
To limit the number of thumbnails, we need to create a custom module that overrides the default behavior. Here’s how to achieve this:
- Create a new Magento 2 module: In your project root directory, navigate to
app/code. Inside theapp/codefolder, create a new folder with your vendor name and the module name (e.g.,MyCompany_ThumbnailLimit).
cd app/code
mkdir MyCompany/ThumbnailLimit
- Generate the module structure: Run the following command in the terminal to generate the necessary files for your custom module:
php bin/magento setup:upgrade
-
Override the default view logic:
Open
app/code/MyCompany/ThumbnailLimit/etc/module.xmland ensure it contains the following content:<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="MyCompany_ThumbnailLimit" setup_version="1.0.0"> <sequence> <module name="Magento_Catalog"/> </sequence> </module> </config> -
Create a custom view file:
Navigate to
app/code/MyCompany/ThumbnailLimit/view/frontend/templates/product/view/thumbnail.phtml. Create this file and add the following content:<?php $imageCollection = $block->getGalleryImages($product); $count = count($imageCollection->getItems()); if ($count > 4) { $imageCollection->setSize(4); } ?> <div class="fotorama" data-nav="thumbs"> <?php foreach ($imageCollection as $_image): ?> <a href="<?php /* @escapeNotVerified */ echo $this->helper('Magento\CatalogInventory\Helper\Data')->getStockItem($_product)->isSaleable() ? $_image->getFile() : 'javascript:void(0)'; ?>" class="fotorama__img"> <?php if ($_image->getLabel()): ?> <span class="fotorama__caption"><?= /* @escapeNotVerified */ $_helper->productAttribute($_product, $_image->getLabel(), 'label') ?></span> <?php endif; ?> <img src="<?php /* @escapeNotVerified */ echo $this->helper('Magento\CatalogInventory\Helper\Data')->getStockItem($_product)->isSaleable() ? $_image->getFile() : $this->getNoProductImageUrl(); ?>" alt="<?= /* @escapeNotVerified */ $block->getImageLabel($_image) ?>" title="<?= /* @escapeNotVerified */ $block->getImageAlt($_image) ?>"> </a> <?php endforeach; ?> </div> -
Clear the cache and reindex:
After saving your changes, run the following commands in the terminal:
php bin/magento cache:clean php bin/magento indexer:reindex
Conclusion
In this guide, we demonstrated how to limit the number of thumbnails displayed in a Magento 2 product image slider using a custom module. By following these steps, you can override the default behavior and create a customized shopping experience for your customers.
Remember that working with Magento requires a solid understanding of PHP, XML, and Twig templates. Developing custom modules like this one is an excellent way to learn the platform’s architecture and extend its functionality beyond the built-in features.
For more advanced customization techniques and real-world examples, consider exploring the resources available at Laravel Company. They offer comprehensive tutorials and guides for Magento developers looking to take their skills to the next level.