Sunday, July 23, 2023

Prestashop 1.6, 1.7, 8 - Zobrazeni prepinace list/grid (List/Grid Switcher)

 

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:

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

add

1
2
<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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$(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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
.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:

1
<article class="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">

and change it to:

1
<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!

No comments:

Post a Comment

Thank you for your comment. Will try to react as soon as possible.

Regards,

Networ King