我需要把所有的类别及其在的Prestashop(我用V 1.6.0.9)的产品页面上的ID列表。
我试图做这样的事情:
$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你有我的店(见“相关产品”)。
这是我的第三个店,我这个问题挣扎了两天。
在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)
=>当你有你想要得到类别的某些特定ID的列表非常有用的 - 你只是将它们作为阵列 - 例如使用的:
$myCustomCatIDs = array( 5 , 20 , 7);
$myCustomCats = Category::getCategoryInformations( $myCustomCatIDs );
必须在home categories module I与PS 1.6测试这个模块一看,它的工作原理。您可以修改模块的钩您的需求。我做了一些修改的personnal有太多显示的子类别。 (对不起,我的英语不好,不是我的母语)
下面是我的模块定制的PHP代码,存储智者的类别和子类别的项目,并链接到第三方物流文件。
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');
}
}
Try-
Add in controller function-
$categoryList = Category::getCategories();
and assign the variable in smarty.
$this->smarty->assign(array(
'displayCategoryList' => $categoryList,
));
Add in tpl file-
{$displayCategoryList|@print_r}