2014-10-27 91 views
5

我需要在Prestashop的產品頁面上列出所有類別及其ID(我正在使用v 1.6.0.9)。顯示產品頁面上的所有分類Prestashop

我試圖做這樣的事情:

$other_categories = $something->getCategories($this->context->language->id, 1, 100); 

foreach($other_categories as something) 
{ 
    // now if the id of the category isnt "1", display name of category 
    if($category->id != "1") { $category->name } 
} 

但是,這是行不通的。

$category->name僅給出當前打開類別的名稱,而不是列表中每個類別的名稱。我不知道要放什麼,而不是something?只有當我使用$category->getProducts時,它纔有效。 Here你有我的商店(請參閱「相關產品」)。

這是我的第三家店,我在這個問題上掙扎了兩天。

回答

0

看看home categories module我測試了這個模塊與PS 1.6,它的工作原理。您可以根據需要修改模塊的鉤子。我已經做了一些個人修改來顯示子類別。 (對不起,我的母語不好)

這是我爲模塊定製的php代碼,將類別和子類別項存儲在smarty中,並鏈接到tpl文件。

class Homecategories extends Module 
{ 
    private $_html = ''; 
    private $_postErrors = array();  
    function __construct() 
    { 
     $this->name = 'homecategories'; 
     $this->tab = 'front_office_features'; 
     $this->version = 1.3; 
     $this->author = 'John Stocks'; 
     $this->need_instance = 0;  
     parent::__construct(); // The parent construct is required for translations  
     $this->page = basename(__FILE__, '.php'); 
     $this->displayName = $this->l('Homepage Categories for v1.5'); 
     $this->description = $this->l('Displays categories on your homepage'); 
    }  
    function install() 
    { 
     return (parent::install() AND $this->registerHook('home') AND $this->registerHook('header')); 
    } 
    public function hookHeader() 
    { 
     Tools::addCSS(($this->_path).'homecategories.css', 'all'); 
    } 
    function hookHome($params) 
    { 
     global $smarty, $cookie, $link; 
     $id_customer = (int)$params['cookie']->id_customer; 
     $id_group = $id_customer ? Customer::getDefaultGroupId($id_customer) : _PS_DEFAULT_CUSTOMER_GROUP_; 
     $id_lang = (int)$params['cookie']->id_lang; 
     $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' 
      SELECT c.*, cl.* 
      FROM `'._DB_PREFIX_.'category` c 
      LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND `id_lang` = '.$id_lang.') 
      LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON (cg.`id_category` = c.`id_category`) 
      WHERE level_depth > 1 And level_depth < 3 
      AND c.`active` = 1 
      AND cg.`id_group` = '.$id_group.' 
      ORDER BY `level_depth` ASC, c.`position` ASC');  
     $category = new Category(1); 
     $nb = intval(Configuration::get('HOME_categories_NBR'));  
     global $link; 
     $this->context->smarty->assign(array(
      'categories' => $result, Category::getRootCategories(intval($params['cookie']->id_lang), true), 
      'link' => $link));  
     $this->context->smarty->assign(array(
      'category' => $category, 
      'lang' => Language::getIsoById(intval($params['cookie']->id_lang)), 
     )); 
     $result2 = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' 
      SELECT c.*, cl.* 
      FROM ps_category c 
      LEFT JOIN `ps_category_lang` cl ON (c.`id_category` = cl.`id_category` AND `id_lang` = '.$id_lang.') 
      LEFT JOIN ps_category_group cg ON (cg.`id_category` = c.`id_category`) 
      WHERE level_depth > 2 And level_depth < 4 
      AND cg.`id_group` = '.$id_group.' 
      AND c.`active` = 1 
      ORDER BY `level_depth` ASC, c.`position` ASC '); 
     global $link; 
     $this->context->smarty->assign(array(
      'subcategories' => $result2, Category::getRootCategories(intval($params['cookie']->id_lang), true), 
      'sublink' => $link)); 

     $this->context->smarty->assign(array(
      'category' => $subcategory, 
      'lang' => Language::getIsoById(intval($params['cookie']->id_lang)), 
     ));  
     return $this->display(__FILE__, 'homecategories.tpl'); 
    } 
} 
+0

雖然這種聯繫可以回答這個問題,最好是包括的基本部分。這裏的答案和提供的鏈接供參考。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – Sasa 2014-10-31 15:07:44

6

在PS 1.6有一個Category類,它包含在你的控制器使用一些方便的靜態方法:getCategories(...)getNestedCategories(...)getSimpleCategories - 這些都是靜態的(公共)SOU你叫他們像Category::funcName(...)

你的目的我的事情最好的辦法是getNestedCategories()它有這個頭:

public static function getNestedCategories(
    $root_category = null, 
    $id_lang = false, 
    $active = true, 
    $groups = null, 
    $use_shop_restriction = true, 
    $sql_filter = '', 
    $sql_sort = '', 
    $sql_limit = '' 
) 

在你的控制器,你可以這樣做:

$allCategories = Category::getNestedCategories(null, $this->context->language->id); 
$this->context->smarty->assign('allCategories' , $allCategories); 

然後在你的模板文件類似

{foreach from=$allCategories item=mainCategory} 
    <div class="categoryBox"> 
    <h2>{$mainCategory.name}</h2> 
    <p>{$mainCategory.description}</p> 
    </div> 
    {foreach from=$mainCategory.children item=subCategory} 
    <div class="categoryBox"> 
     <h3>{$subCategory.name}</h3> 
     <p>{$subCategory.description}</p> 
    </div> 
    {/foreach} 

{/foreach} 

如果你想有家庭類的唯一的子類別,你可以使用getHomeCategories($id_lang, $active = true, $id_shop = false)

$allCategories = Category::getHomeCategories($this->context->language->id); 

還方便的一個是靜態功能getCategoryInformations($ids_category, $id_lang = null)
=>非常有用w母雞你有你想要得到類別的某些特定ID的列表 - 你只是將它們作爲陣列 - 例如用法:

$myCustomCatIDs = array(5 , 20 , 7); 
$myCustomCats = Category::getCategoryInformations($myCustomCatIDs); 
相關問題