Display Supplier Name in Category (Product List) Page in Prestashop


Prestashop Displays supplier name in product detail page by default.

But to display supplier name in product list page, you have to modify some code to do so.

In this post, i will share you some tips to display supplier name in product list page in prestashop.

To add supplier name inproduct list page, you have to modify CategoryController

For better coding standard first copy your CategoryController file from controllers/front/CategoryController.php and put it into

override/controllers/front/CategoryController.php

in initContent function add supplier_name in it.

Also add new getProductSupplierName function just above it.

And finally assign supplier_name as a smarty variable.

in override/controllers/front/CategoryController.php

public function getProductSupplierName($catproducts) {
      
    $suppliername = array();
 
    foreach($catproducts as $product)
    {
        // load product object
        $supplier = new Supplier($product['id_supplier'],$this->context->language->id);
  
        $suppliername[$product['id_product']][$product['id_supplier']] = $supplier->name;
    }
 
    return $suppliername;
}

public function initContent()
    {
        parent::initContent();

        $this->setTemplate(_PS_THEME_DIR_.'category.tpl');

        if ($this->errors) {
            return;
        }

        if (!$this->customer_access) {
            return;
        }

        if (isset($this->context->cookie->id_compare)) {
            $this->context->smarty->assign('compareProducts', CompareProduct::getCompareProducts((int)$this->context->cookie->id_compare));
        }

        // Product sort must be called before assignProductList()
        $this->productSort();

        $this->assignScenes();
        $this->assignSubcategories();
        $this->assignProductList();
  
  $supplier_name = $this->getProductSupplierName((isset($this->cat_products) && $this->cat_products) ? $this->cat_products : null);
  
        $this->context->smarty->assign(array(
            'category'             => $this->category,
            'supplier_name'        => $supplier_name,
            'description_short'    => Tools::truncateString($this->category->description, 350),
            'products'             => (isset($this->cat_products) && $this->cat_products) ? $this->cat_products : null,
            'id_category'          => (int)$this->category->id,
            'id_category_parent'   => (int)$this->category->id_parent,
            'return_category_name' => Tools::safeOutput($this->category->name),
            'path'                 => Tools::getPath($this->category->id),
            'add_prod_display'     => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
            'categorySize'         => Image::getSize(ImageType::getFormatedName('category')),
            'mediumSize'           => Image::getSize(ImageType::getFormatedName('medium')),
            'thumbSceneSize'       => Image::getSize(ImageType::getFormatedName('m_scene')),
            'homeSize'             => Image::getSize(ImageType::getFormatedName('home')),
            'allow_oosp'           => (int)Configuration::get('PS_ORDER_OUT_OF_STOCK'),
            'comparator_max_item'  => (int)Configuration::get('PS_COMPARATOR_MAX_ITEM'),
            'suppliers'            => Supplier::getSuppliers(),
            'body_classes'         => array($this->php_self.'-'.$this->category->id, $this->php_self.'-'.$this->category->link_rewrite)
        ));
    }

To display supplier name in product list page, add below code in your theme's prduct-list.tpl file.

in themes/your-theme/product-list.tpl

{foreach from=$supplier_name key=supplier_key item=supplier}
 {if $product.id_product == $supplier_key }
  {foreach from=$supplier key=supplier_only_key item=supplier_only_value}
   <h1>{$supplier_only_value}</h1>
  {/foreach}
 {/if}
{/foreach}

Post a Comment

0 Comments