Displaying Magento Products with Owl Carousel: A Step-by-Step Guide
Owl Carousel is an excellent jQuery plugin for creating responsive carousel sliders. In this post, we’ll demonstrate how to integrate it with your Magento store to display products from a specific category on the homepage.
What’s Wrong with the Current Approach?
The current approach using a CMS block with catalog/product_list type and list.phtml template is causing issues because:
- The default product list template (
list.phtml) isn’t designed for use within a carousel. - Using CMS blocks with product lists adds extra load on the CMS page.
A Better Approach: Custom Block and Template
To achieve this, we’ll create a custom block that fetches products from a specific category and uses a dedicated template suited for the Owl Carousel.
Step 1: Create a Custom Product List Block
In your Magento theme directory (app/design/frontend/[Vendor]/[Theme]/), navigate to etc/modules folder. Copy and rename an existing XML file (e.g., X_Magento_SampleModule.xml) as follows:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/config.xsd">
<type>
<block type="Magento\Catalog\Block\Product\ListProduct" name="laravel_carousel_product_list">
<arguments>
<argument name="template" xsi:type="string">Laravel_Theme::product/list/owl.phtml</argument>
</arguments>
</block>
</type>
</config>
Replace Laravel_Theme with your theme’s code.
Step 2: Create the Dedicated Template
Navigate to app/design/frontend/[Vendor]/[Theme]/template Magento_Catalog/product/list/ folder. Here, create a new file called owl.phtml.
In this template, you’ll fetch products from a specific category and use Owl Carousel to display them.
<?php
$categoryId = 112; // Your desired category ID
$products = $block->getProductCollection()->addCategoryFilter($categoryId);
if (count($products)): ?>
<div class="owl-carousel owl-theme">
<?php foreach ($products as $product): ?>
<div>
<?= /* @escapeNotVerified */ $block->getImageHtml($product, 'category_page_list') ?>
<h2><?= /* @escapeNotVerified */ $this->helper('Magento\Catalog\Helper\Data')->getProductName($_product) ?></h2>
<p><?= /* @escapeNotVerified */ $block->formatPrice($_product->getFinalPrice()) ?></p>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
Step 3: Initialize Owl Carousel
In your theme’s layout XML file (usually located at app/design/frontend/[Vendor]/[Theme]/layout/default.xml), add the following code:
<referenceContainer name="content">
<block type="laravel_carousel_product_list" template="Magento_Catalog::product/list/owl.phtml" before="-"/>
</referenceContainer>
Step 4: Style and Initialize with JavaScript
Ensure you have included the Owl Carousel CSS and JavaScript files in your theme’s head section. You can use a CDN like this:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js"></script>
In your theme’s JavaScript file, initialize the Owl Carousel:
require(['jquery'], function($) {
$('.owl-carousel').owlCarousel({
items: 4,
loop: true,
margin: 10,
nav: true,
dots: false
});
});
Conclusion
By following this approach, you can display products from a specific category using the Owl Carousel on your Magento homepage. This method reduces server load and provides better control over the product layout within the carousel.
Remember to replace placeholders like [Vendor], [Theme], and 112 with your actual values. Also, ensure that you have set up your theme properly by following best practices and using a child theme when necessary.
For detailed guides on Magento theme development, feel free to explore our blog at https://laravelcompany.com/magento-themes/.