Reputation: 1
I want to have 2 template categories in prestashop. I think it's possible in controller category
if ($category->id == 3){
$this->setTemplate(_PS_THEME_DIR_.'category2.tpl');
} else {
$this->setTemplate(_PS_THEME_DIR_.'category.tpl');
}
Upvotes: 0
Views: 669
Reputation: 53
Open your category.tpl file and change the code like so (19 is the id of your custom category):
{if $category->id == "19"}
\\ create in theme dir a file named category-19.tpl and add custom code in it
{include file="$tpl_dir./category-19.tpl"}
{else}
\\ default category.tpl code
{/if}
Upvotes: 1
Reputation: 4337
Another (cleaner - no overrides) way is to create a module that uses hook displayOverrideTemplate
and serve a proper template.
public function hookDisplayOverrideTemplate($params)
{
$controller = $params['controller'];
if ($controller->php_self != 'category') {
return false;
}
$id_category = (int)Tools::getValue('id_category');
if ($id_category == 3) {
return 'path_to_tpl1.tpl';
}
else {
return 'path_to_tpl2.tpl'; // or return false to use default template
}
}
Upvotes: 1
Reputation: 1273
Good morning,
Yes this is possible here is the override to put in Override / Controller / front / CategoryController
<?php
class CategoryController extends CategoryControllerCore
{
public function initContent()
{
parent::initContent();
if ($this->category->id == 3) {
$this->setTemplate(_PS_THEME_DIR_.'category2.tpl');
} else {
$this->setTemplate(_PS_THEME_DIR_.'category.tpl');
}
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));
$this->productSort(); // Product sort must be called before assignProductList()
$this->assignScenes();
$this->assignSubcategories();
$this->assignProductList();
$this->context->smarty->assign(array(
'category' => $this->category,
'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)
));
}
}
Regards
Upvotes: 1