Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to Add Owl Carousel Slide into Magento 2

Vinh Jacker | 11-11-2024

How to Add Owl Carousel Slide into Magento 2

One of the most well-liked image carousels is the Owl Carousel, which is fully responsive, supports touch, and highly configurable. One excellent method to design your website in a way that attracts visitors and generates leads is to incorporate a Magento 2 Carousel.

Note: This tutorial contains multiple file paths. Make sure you are using the exact same file path for this to function. To ensure that the code runs properly, make sure to create any missing folders.

Step 1: Download and add the CSS and JS files to your website

First, you have to download the CSS and JS files from the Owl Carousel site in order to add your Magento 2.

Then, add the CSS to this folder located in the directory of your website:

app/design/frontend/package_name/theme_name/Magento_Catalog/web/css

The package and theme name for your website are determined by the theme you select. You can simply use “custom” if you are using a custom theme. The “Magento_Catalog” reference can also be left out.

One of the best practices is to call the CSS within a layout file. Go to:

app/design/frontend/package_name/theme_name/Magento_Theme/layout/default_head_blocks.xml

Use this code:

<head>

        <css src="css/owl.carousel.min.css" rel="stylesheet" type="text/css"  />

        <css src="css/owl.theme.default.min.css" rel="stylesheet" type="text/css"  />

</head>

Then, in the directory of your website, add the JS file to this folder:

app/design/frontend/package_name/theme_name/Magento_Catalog/web/js

You will need to create a require-config.js file if you do not already have one. Using this JS file for all site configurations is standard procedure. This file can be found at:

app/design/frontend/package_name/theme_name/Magento_Catalog/requre-config.js

Copy the code below and paste it into this file:

var config = {

paths: {            

        'owlcarousel': "Magento_Theme/js/owl.carousel"

    },  

shim: {

    'owlcarousel': {

        deps: ['jquery']

    }

}

};

Step 3: Include the Slider Block on your page:

To continue, you must first access your page content file. The name of the page to which you wish to add the slider will appear in this phtml file. It’s probably in your site directory over here:

app/design/frontend/package_name/theme_name/Magento_Catalog/name_of_page.phtml

Paste the following code into the phtml file:


<!DOCTYPE html>

<html>

    <body>

        <div class="owl-carousel owl-theme">

            <div class="item">

                <img src="{Image path}" />

            </div>

            <div class="item">

                <img src="{Image path}" />

            </div>

            <div class="item">

                <img src="{Image path}" />

            </div>

            <div class="item">

                <img src="{Image path}" />

            </div>

            <div class="item">

                <img src="{Image path}" />

            </div>

            <div class="item">

                <img src="{Image path}" />

            </div>

        </div>

    </body>

</html>

This has now called the carousel to appear and added a block to your website.

Step 4: Include the RequireJS module

Next, you’ll need to include this JavaScript code to make sure your carousel works properly. You can add this code either directly to the same page where you’ve placed your carousel or into your website’s custom JavaScript file. There are various ways to write this code, and here’s an example of what it might look like in a JavaScript file. If you’re adding it to a PHTML file, the syntax would be a bit different, and you’ll need to remember to include the

require(['jquery', 'owlcarousel'], function($){

    $(document).ready(function() {

        $('.owl-carousel').owlCarousel({

            loop:true,

            margin:10,

            nav:true,

            responsive:{

                0:{

                    items:1

                },

                600:{

                    items:3

                },

                1000:{

                    items:4

                }

            }

        });

    });

});

It’s worth mentioning that there are numerous customization possibilities available for this Owl Carousel code snippet. These consist of the items’ size, quantity, and autoplay. This code snippet will display the maximum number of items at four, filling the entire width of the page.

In order to improve the clarity of the images on smaller screens, fewer items will be displayed. Three images would be displayed on a medium screen and only one on a mobile device. Adjust these configurations according to your tastes. For any website, a banner slider or an Owl Carousel in Magento 2 are both fantastic design options.

Table of content
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services