Search code examples
phpsmartyprestashop

Call blocklayered Module in manufacture tpl file in prestashop


I want to use blocklayered(Layered navigation block) module in my manufacture-list.tpl file . In that I used this code:

{include file='./modules/blocklayered/blocklayered.tpl'}

But it's not working I want layered filter in left column in manufacture page, which is already appearing in category page.


Solution

  • It's not possible. This question has been asked many times and the response is always the same.

    Blocklayered module is build to work with categories and can't be used for anything else.

    A solution to this is to keep using the manufacturers list page, but change the link of each manufacturer to point to your root category with a filter defined for your manufacturer. The user will then be redirected to your root category with the blocklayered active and filtered on the wanted manufacturer.

    I did it for my client in this way: (I've added some code to sort the manufacturers alphabetically and group it by their first letter with a fast access menu).

    file /override/controllers/front/ManufacturerController.php:

    class ManufacturerController extends ManufacturerControllerCore
    {
        protected $blocklayered = null;
    
        public function __construct()
        {
            parent::__construct();
    
            require_once(_PS_MODULE_DIR_ . 'blocklayered/blocklayered.php');
            $this->blocklayered = new BlockLayered();
        }
    
        /**
         * Assign template vars if displaying the manufacturer list
         */
        protected function assignAll()
        {
            if (Configuration::get('PS_DISPLAY_SUPPLIERS')) {
                $data = Manufacturer::getManufacturers(true, $this->context->language->id, true, false, false, false);
    
                $no_products = array();
                foreach ($data as $key => $item) {
                    $data[$key]['link'] = Context::getContext()->link->getCategoryLink(MY_CATEGORY_ROOT, null, null, str_replace($this->getAnchor(), '_', Tools::link_rewrite($this->blocklayered->l('Manufacturer'))) . $this->getAnchor() . str_replace($this->getAnchor(), '_', Tools::link_rewrite($data[$key]['name'])));
                    $data[$key]['image'] = (!file_exists(_PS_MANU_IMG_DIR_.$item['id_manufacturer'].'-'.ImageType::getFormatedName('medium').'.jpg')) ? false : $item['id_manufacturer'];
                    if ($item['nb_products'] == 0)
                    {
                        $no_products[] = $key;
                    }
                }
    
                foreach ($no_products as $key)
                {
                    unset($data[$key]);
                }
    
                $manufacturersByLetters = array();
    
                foreach ($data as $item)
                {
                    $letter = strtoupper(substr($item['name'], 0, 1));
                    if (! isset($manufacturersByLetters[$letter]))
                    {
                        $manufacturersByLetters[$letter] = array();
                    }
                    $manufacturersByLetters[$letter][] = $item;
                }
    
                ksort($manufacturersByLetters, SORT_STRING);
                foreach ($manufacturersByLetters as &$manufacturers)
                {
                    usort($manufacturers, function($manu1, $manu2) {
                        return strcmp(strtolower($manu1['name']), strtolower($manu2['name']));
                    });
                }
                unset($manufacturers);
    
                $this->context->smarty->assign(array(
                    'nbManufacturers' => count($data),
                    'mediumSize' => Image::getSize(ImageType::getFormatedName('medium')),
                    'manufacturersByLetters' => $manufacturersByLetters
                ));
            } else {
                $this->context->smarty->assign('nbManufacturers', 0);
            }
        }
    
        protected function getAnchor()
        {
            static $anchor = null;
            if ($anchor === null)
                if (!$anchor = Configuration::get('PS_ATTRIBUTE_ANCHOR_SEPARATOR'))
                    $anchor = '-';
            return $anchor;
        }
    }
    

    file /themes/my_theme/manufacturer-list.tpl:

    {capture name=path}{l s='Nos marques'}{/capture}
    
    <h1>
        <span>{l s='Nos Marques'}</span>
    </h1>
    {if isset($errors) AND $errors}
        {include file="$tpl_dir./errors.tpl"}
    {else}
        {if $nbManufacturers > 0}
            <div id="manufacturer_menu">
                <span id="manufacturer_menu_title">{l s='Accéder directement à une lettre'}</span>
                <ul>
                    {foreach from=$manufacturersByLetters item=manufacturers key=letter name=manufacturersByLetters}
                        <li{if $smarty.foreach.manufacturersByLetters.first} class="active"{/if}><a href="#manufacturer_list_letter_{$letter}">{$letter}</a></li>
                    {/foreach}
                </ul>
            </div>
    
            {foreach from=$manufacturersByLetters item=manufacturers key=letter name=manufacturersByLetters}
                <h2 class="manufacturer_list_letter_wrapper{if $smarty.foreach.manufacturersByLetters.first} first{/if}">
                    <span class="manufacturer_list_letter" id="manufacturer_list_letter_{$letter}">{$letter}</span>
                </h2>
                <ul class="manufacturer_list row">
                {strip}
                    {foreach from=$manufacturers item=manufacturer name=manufacturers}
                        <li class="col-lg-1 col-md-2 col-sm-3 col-xs-6">
                            <div class="row">
                                <div class="col-xs-12">
                                    <a class="manufacturer_list_name_wrapper" href="{$manufacturer.link|escape:'html':'UTF-8'}" title="{$manufacturer.name|escape:'html':'UTF-8'}">
                                        <div>
                                            {if isset($manufacturer.image) && $manufacturer.image}
                                                <img src="{$img_manu_dir}{$manufacturer.image|escape:'html':'UTF-8'}-medium_default.jpg" alt="{$manufacturer.name|escape:'html':'UTF-8'}" />
                                            {else}
                                                <span class="manufacturer_list_name">{$manufacturer.name|escape:'html':'UTF-8'}</span>
                                            {/if}
                                        </div>
                                    </a>
                                    {if isset($manufacturer.nb_products) && $manufacturer.nb_products > 0}
                                        <a class="manufacturer_list_link" href="{$manufacturer.link|escape:'html':'UTF-8'}" title="{$manufacturer.name|escape:'html':'UTF-8'}">
                                            <span class="manufacturer_list_link_name">{$manufacturer.name|escape:'html':'UTF-8'}</span>
                                            <span class="manufacturer_list_link_action">
                                                {if isset($manufacturer.nb_products) && $manufacturer.nb_products == 1}
                                                    {l s='Afficher le produit'}
                                                {elseif isset($manufacturer.nb_products) && $manufacturer.nb_products > 0}
                                                    {l s='Afficher les %d produits' sprintf=$manufacturer.nb_products|intval}
                                                {/if}
                                                 <i class="icon-chevron-right right"></i>
                                            </span>
                                        </a>
                                    {/if}
                                </div>
                            </div>
                        </li>
                    {/foreach}
                {/strip}
                </ul>
            {/foreach}
            <script type="text/javascript">
                $("#manufacturer_menu a").on('click', function() {
                    var $a = $(this)
                    $('html, body').animate({
                        scrollTop: $($a.attr('href')).offset().top
                    }, 750);
                    return false;
                });
            </script>
        {/if}
    {/if}