Dav*_*dra 5 php categories prestashop
我需要在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 }
}
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.
$category->name只给出了当前打开类别的名称,而不是列表中每个类别的名称.我不知道该放什么而不是something?它只适用于我使用的时候$category->getProducts.在这里你有我的店铺(参见"相关产品").
这是我的第三家商店,我两天都在努力解决这个问题.
在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 = ''
)
Run Code Online (Sandbox Code Playgroud)
在您的控制器中,您可以执行以下操作:
$allCategories = Category::getNestedCategories(null, $this->context->language->id);
$this->context->smarty->assign( 'allCategories' , $allCategories );
Run Code Online (Sandbox Code Playgroud)
然后在你的模板文件中
{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}
Run Code Online (Sandbox Code Playgroud)
如果您只想拥有Home类别的子类别,您可以使用getHomeCategories($id_lang, $active = true, $id_shop = false):
$allCategories = Category::getHomeCategories( $this->context->language->id );
Run Code Online (Sandbox Code Playgroud)
另一个方便的是静态函数getCategoryInformations($ids_category, $id_lang = null)
=>当你有一个你想要获得的特定类别ID的列表时非常有用 - 你只需将它们作为数组传递 - 使用示例:
$myCustomCatIDs = array( 5 , 20 , 7);
$myCustomCats = Category::getCategoryInformations( $myCustomCatIDs );
Run Code Online (Sandbox Code Playgroud)
小智 0
看看主页类别模块我用 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');
}
}
Run Code Online (Sandbox Code Playgroud)