Grid - list switcher in PrestaShop 1.7

This tutorial is about creating grid / list products view switcher in PrestaShop 1.7.x classic template. Modification creates special buttons to change the type of list of products and remember selected option. Modification does not require any core change, we will only change the .tpl files, js files and css styles. Example of grid / list switcher you can see below (screenshot).

 

grid list switcher in PrestaShop 1.7

 

Modification of classic theme files

With this modification we will create special buttons to switch the type of products list. This list will appear above the list of products as you can see on the screenshot above. We will modify file file: /themes/classic/templates/catalog/_partials/products-top.tpl. Please open this file and right after the code:

      {block name='sort_by'}
        {include file='catalog/_partials/sort-orders.tpl' sort_orders=$listing.sort_orders}
      {/block}

add

      <i class="material-icons show_list">&#xE8EF;</i>
      <i class="material-icons show_grid">&#xE8F0;</i>

This  code is a code that adds two material icons code to switch the design of products list.

 

JavaScript that will handle the design type change

Now its time to define the JavaScript code that will add special functions to our two new icons. We must apply this script code to theme's custom.js file that is located here: themes/classic/assets/js/custom.js. Please open this file and at the end of file paste code:

$(document).ready(function(){
    $('.show_list').click(function(){
        document.cookie = "show_list=true; expires=Thu, 30 Jan 2100 12:00:00 UTC; path=/";
        $('#js-product-list .product-miniature').addClass('product_show_list');
    });
    
    $('.show_grid').click(function(){
        document.cookie = "show_list=; expires=Thu, 30 Jan 1970 12:00:00 UTC; path=/";
        $('#js-product-list .product-miniature').removeClass('product_show_list');
    });
    
    prestashop.on('updateProductList', function (event) {
        $('.show_list').click(function(){
            $('#js-product-list .product-miniature').addClass('product_show_list');
        });
        
        $('.show_grid').click(function(){
            $('#js-product-list .product-miniature').removeClass('product_show_list');
        });
    });
});

Code above adds to our new buttons feature to change the list style to list / grid. The main idea of script is 'action' when we press on the buttons that adds 'product_show_list" css class to each instance of product. This script has feature to create cookie with information that we want to use list style (or not) - so we will be able to "remember" selected type of products list. There is also a script that updates the buttons when we apply some filters from layered search tool.

 

Css styles for our 'product_show_list' class

Now its time to deifne styles to our new design of list of products. In this case we must open the file: custom.css - it is located in theme directory: themes/classic/assets/css/custom.css. At the end of this file please paste code:

.show_list, .show_grid {
    cursor:pointer;
    opacity:1.0;
}

.show_list:hover, .show_grid:hover {
    opacity:0.7;
}

.product_show_list {
    width:100%;
}

.product_show_list .highlighted-informations, .product_show_list .product-description, .product_show_list .thumbnail-container {
    width:100%!important;
}

.product_show_list .product-thumbnail {
    text-align:center;
}

 

Remember selected design type

As you know - javascript code that we added to buttons has feature to remember selected design type. It is based on cookie. When we select "list" design type - script will create cookie named show_list. Now its time to apply modification to theme file product.tpl located here: themes/classic/templates/catalog/_partials/miniatures/product.tpl where we must add code that will identify the cookie - and if it will exist - modification will apply 'product_show_list' class to product element. So - open the file product.tpl and find code:

<article class="zproduct-miniature js-product-miniature" data-id-product="{$product.id_product}" data-id-product-attribute="{$product.id_product_attribute}" itemscope itemtype="http://schema.org/Product">

and change it to:

<article class="{if isset($smarty.cookies.show_list)}product_show_list {/if}product-miniature js-product-miniature" data-id-product="{$product.id_product}" data-id-product-attribute="{$product.id_product_attribute}" itemscope itemtype="http://schema.org/Product">

 

That's all

Your grid / list switcher feature should work now! If somehow you dont see the changes it might be necessary to clear shop cache and recompile the theme. In some cases also browser clear cache may be requried (browser remembers old js and css files).

 

author milos myszczuk
Article by Milosz Myszczuk PrestaShop expert, official PrestaShop community moderator. PHP developer, specialist in relative and spatial databases management, GIS Analyst, CEO & founder of VEKIA interactive agency. Read more about VEKIA company
If you like my articles and want much more valuable tips, feel free to send me donation
1.4 version 1.4.11 1.6 404 addon admin advertise ahref ajax alpha animation api app application authentication back office backup badge banner basics block bootstrap button cache carrier cart catalog category certificate changelog chat class clear client clip cms code colors columns comments configuration contact container content controller cookie counter country coupon css csv currency customer dashboard database debug default delete delivery desktop developer device disable discount displayNav displayTop download dynamic editor effect empty encrypt engine error exchange exclude export facebook faceshop fade fancoupon fancybox fanpage fatal feature feed field file fix fixed font footer free friendly url front ftp full gallery generate gift global godaddy google google+ gray grid groupon header help hide highlight homefeatured homepage hook hosting hover howto htaccess html html5 ID image import include input instagram installation integration iPhone issue javascript jquery kgb knowhow languages law left likebox link list livingsocial loading log login logo loyality mail mailing maintenance manufacturer marketing marquee mcrypt menu meta mobile modification module movie moving multilanguage multiupload must have mysql news newsletter notification number open graph order override page password performance PHP phpmyadmin picture pinterest plugin popup post prestashop prestashop 1.0 prestashop 1.1 prestashop 1.2 prestashop 1.3 prestashop 1.4 prestashop 1.5 price rules problem product profile promotion proslider purifier quantity query quick tip random rates register reinsurance release reporting reset responsive restore results ribbon rich text right sales search security seo service shadow share shipping shop shopmania slider smarty social networks SQL SSL statistics stock store style subcategory superuser support switcher tab tablet tag tax template text theme tinyMCE tips and tricks tpl tracking translations tree trends trigger tumblr tutorial twitter update upgrade upload variables video visits voucher vulnerability web2print wide widget width window wishlist wysiwyg youtube zip zopim