2014-10-27 120 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); 
相关问题