我刚开始使用Opencart。我需要有品牌列表,比如llist of catgories。我这样试试:
<div class="box">
<div class="box-heading"><?php echo "По брендам" ?></div>
<div class="box-content">
<ul class="box-category">
<?php foreach ($manufacturers as $manufacturer) { ?>
<li>
<?php if ($manufacturer['manufacturer_id'] == $manufacturer_id) { ?>
<a href="<?php echo $manufacturer['href']; ?>" class="active"><?php echo $manufacturer['name']; ?></a>
<?php } else { ?>
<a href="<?php echo $manufacturer['href']; ?>"><?php echo $manufacturer['name']; ?></a>
<?php } ?>
<?php if ($manufacturer['children']) { ?>
<ul>
<?php foreach ($manufacturer['children'] as $child) { ?>
<li>
<?php if ($child['manufacturer_id'] == $child_id) { ?>
<a href="<?php echo $child['href']; ?>" class="active"> - <?php echo $child['name']; ?></a>
<?php } else { ?>
<a href="<?php echo $child['href']; ?>"> - <?php echo $child['name']; ?></a>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>
</div>
我在default / template / module / category.tpl文件中添加此代码。 我收到此错误:注意:未定义的变量:制造商....
我该怎么做? 感谢。
答案 0 :(得分:4)
category.tpl
是类别控制器的模板。其目的是显示类别。尝试将浏览器定向到http://your.site/index.php?route=product/manufacturer
,您将获得制造商列表。模板为/catalog/view/theme/default/template/product/manufacturer_list.tpl
。
您的错误是一个简单的结果:$manufacturers
变量未在ControllerModuleCategory控制器(又名category.php
)中定义。如果要在模板中使用变量,则必须在相关控制器中定义它。
为了准确地在/catalog/view/theme/default/template/module/category.tpl
中获取制造商列表(即使这不是本模块的目的),您需要做的事情很少:
1)在/catalog/model/catalog/manufacturer.php
文件(型号ModelCatalogManufacturer)添加功能(此功能将帮助您从数据库中获取制造商列表):
public function getManufacturerByCategory($category_id) {
$query = $this->db->query("SELECT m.*
FROM " . DB_PREFIX . "product p
RIGHT JOIN " . DB_PREFIX . "product_to_category p2c ON
p.product_id = p2c.product_id
LEFT JOIN " . DB_PREFIX . "manufacturer m ON
p.manufacturer_id = m.manufacturer_id
WHERE
p2c.category_id = " . (int)$category_id . " AND
m.manufacturer_id IS NOT NULL
GROUP BY m.manufacturer_id");
return $query->rows;
}
2)在/catalog/controller/module/category.php
文件(又名ControllerModuleCategory控制器)中,在if (file_exists(DIR_TEMPLATE . $this->config->get('confi...
之前插入代码:
if (isset($this->request->get['path'])) {
$category_id = array_pop($parts);
$this->load->model('catalog/manufacturer');
$this->data['manufacturers'] = $this->model_catalog_manufacturer->getManufacturerByCategory($category_id);
} else {
$this->data['manufacturers'] = array();
}
(请注意,控制器中的$this->data['manufacturers']
将在模板中显示为$manufacturers
。$this->data['categories']
将为$categories
,$this->data['another_var']
将是$another_var
等等。)
此时您可以在类别模块模板中使用foreach ($manufacturers as $manufacturer)
(您尝试foreach
的地方,当您收到“未定义变量”错误时)以便输出适用于当前类别产品的制造商名单。
最好的方法是创建另一个模块,“制造商”模块,而不是非法使用类别模块。但我现在还不确定你是否准备好应对这一挑战。
顺便说一下,我是杰伊吉尔福德指南的shadyyx's link。这对初学者来说非常棒。答案 1 :(得分:2)
我有轻松的解决方案: 只需在Controller中添加代码:category.php:
$this->load->model('catalog/manufacturer');
$this->data['mans'] = $this->data['manufacturers'] =
$this->model_catalog_manufacturer->getManufacturers();
并查看:category.tpl:
<?php
foreach($mans as $mans_value)
{
var_dump($mans_value);
}
?>