在产品页面Prestashop上显示所有类别

时间:2014-10-27 06:37:33

标签: php prestashop categories

我需要在Prestashop的产品页面上获取所有类别及其ID的列表(我使用的是v 1.6.0.9)。

我试着这样做:

$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你有我的店铺(见“相关产品”)。

这是我的第三家商店,我两天都在努力解决这个问题。

3 个答案:

答案 0 :(得分:7)

在PS 1.6中有一个Category类,它包含一些在控制器中可用的方便的静态方法:getCategories(...)getNestedCategories(...)getSimpleCategories - 这些都是静态的(和公众)你称之为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)
=&GT;当你有一个你想要获得的特定类别ID列表时非常有用 - 你只需将它们作为数组传递 - 使用示例:

$myCustomCatIDs = array( 5 , 20 , 7);
$myCustomCats = Category::getCategoryInformations( $myCustomCatIDs );

答案 1 :(得分: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');
    }
}

答案 2 :(得分:0)

GET /employees/100?include=manager,salary