我尝试将商店添加到opencart 2.0
在标题的控制器中:
<?php
class ControllerCommonHeader extends Controller {
public function index() {
// Analytics
$this->load->model('extension/extension');
$data['analytics'] = array();
$analytics = $this->model_extension_extension->getExtensions('analytics');
foreach ($analytics as $analytic) {
if ($this->config->get($analytic['code'] . '_status')) {
$data['analytics'][] = $this->load->controller('analytics/' . $analytic['code']);
}
}
if ($this->request->server['HTTPS']) {
$server = $this->config->get('config_ssl');
} else {
$server = $this->config->get('config_url');
}
if (is_file(DIR_IMAGE . $this->config->get('config_icon'))) {
$this->document->addLink($server . 'image/' . $this->config->get('config_icon'), 'icon');
}
$data['title'] = $this->document->getTitle();
$data['base'] = $server;
$data['description'] = $this->document->getDescription();
$data['keywords'] = $this->document->getKeywords();
$data['links'] = $this->document->getLinks();
$data['styles'] = $this->document->getStyles();
$data['scripts'] = $this->document->getScripts();
$data['lang'] = $this->language->get('code');
$data['direction'] = $this->language->get('direction');
$data['name'] = $this->config->get('config_name');
if (is_file(DIR_IMAGE . $this->config->get('config_logo'))) {
$data['logo'] = $server . 'image/' . $this->config->get('config_logo');
} else {
$data['logo'] = '';
}
$this->load->language('common/header');
$data['text_home'] = $this->language->get('text_home');
// stores //
$this->load->language('module/store');
$data['store_id'] = $this->config->get('config_store_id');
$data['stores'] = array();
$data['stores'][] = array(
'store_id' => 0,
'name' => $this->language->get('text_default'),
'url' => HTTP_SERVER . 'index.php?route=common/home&session_id=' . $this->session->getId()
);
$this->load->model('setting/store');
$results = $this->model_setting_store->getStores();
foreach ($results as $result) {
$data['stores'][] = array(
'store_id' => $result['store_id'],
'name' => $result['name'],
'url' => $result['url'] . 'index.php?route=common/home&session_id=' . $this->session->getId()
);
}
// stores //
// Wishlist
if ($this->customer->isLogged()) {
$this->load->model('account/wishlist');
$data['text_wishlist'] = sprintf($this->language->get('text_wishlist'), $this->model_account_wishlist->getTotalWishlist());
} else {
$data['text_wishlist'] = sprintf($this->language->get('text_wishlist'), (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0));
}
$data['text_shopping_cart'] = $this->language->get('text_shopping_cart');
$data['text_logged'] = sprintf($this->language->get('text_logged'), $this->url->link('account/account', '', 'SSL'), $this->customer->getFirstName(), $this->url->link('account/logout', '', 'SSL'));
$data['text_account'] = $this->language->get('text_account');
$data['text_klantenservice'] = $this->language->get('text_klantenservice');
$data['text_register'] = $this->language->get('text_register');
$data['text_login'] = $this->language->get('text_login');
$data['text_order'] = $this->language->get('text_order');
$data['text_transaction'] = $this->language->get('text_transaction');
$data['text_download'] = $this->language->get('text_download');
$data['text_logout'] = $this->language->get('text_logout');
$data['text_checkout'] = $this->language->get('text_checkout');
$data['text_category'] = $this->language->get('text_category');
$data['text_all'] = $this->language->get('text_all');
$data['home'] = $this->url->link('common/home');
$data['wishlist'] = $this->url->link('account/wishlist', '', 'SSL');
$data['logged'] = $this->customer->isLogged();
$data['account'] = $this->url->link('account/account', '', 'SSL');
$data['register'] = $this->url->link('account/register', '', 'SSL');
$data['login'] = $this->url->link('account/login', '', 'SSL');
$data['order'] = $this->url->link('account/order', '', 'SSL');
$data['transaction'] = $this->url->link('account/transaction', '', 'SSL');
$data['download'] = $this->url->link('account/download', '', 'SSL');
$data['logout'] = $this->url->link('account/logout', '', 'SSL');
$data['shopping_cart'] = $this->url->link('checkout/cart');
$data['checkout'] = $this->url->link('checkout/checkout', '', 'SSL');
$data['contact'] = $this->url->link('information/contact');
$data['telephone'] = $this->config->get('config_telephone');
$status = true;
if (isset($this->request->server['HTTP_USER_AGENT'])) {
$robots = explode("\n", str_replace(array("\r\n", "\r"), "\n", trim($this->config->get('config_robots'))));
foreach ($robots as $robot) {
if ($robot && strpos($this->request->server['HTTP_USER_AGENT'], trim($robot)) !== false) {
$status = false;
break;
}
}
}
// Menu
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$data['categories'] = array();
$categories = $this->model_catalog_category->getCategories(0);
foreach ($categories as $category) {
if ($category['top']) {
// Level 2
$children_data = array();
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach ($children as $child) {
if ($child['image']) {
$image = $child['image'];
} else {
$image = 'placeholder.png';
}
$filter_data = array(
'filter_category_id' => $child['category_id'],
'filter_sub_category' => true
);
$children_data[] = array(
'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']),
'thumb' => $child['image'],
);
}
// Level 1
$data['categories'][] = array(
'name' => $category['name'],
'children' => $children_data,
'column' => $category['column'] ? $category['column'] : 1,
'href' => $this->url->link('product/category', 'path=' . $category['category_id']),
'thumb' => $category['image'],
);
}
}
$data['language'] = $this->load->controller('common/language');
$data['currency'] = $this->load->controller('common/currency');
$data['search'] = $this->load->controller('common/search');
$data['cart'] = $this->load->controller('common/cart');
// For page specific css
if (isset($this->request->get['route'])) {
if (isset($this->request->get['product_id'])) {
$class = '-' . $this->request->get['product_id'];
} elseif (isset($this->request->get['path'])) {
$class = '-' . $this->request->get['path'];
} elseif (isset($this->request->get['manufacturer_id'])) {
$class = '-' . $this->request->get['manufacturer_id'];
} else {
$class = '';
}
$data['class'] = str_replace('/', '-', $this->request->get['route']) . $class;
} else {
$data['class'] = 'common-home';
}
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/header.tpl')) {
return $this->load->view($this->config->get('config_template') . '/template/common/header.tpl', $data);
} else {
return $this->load->view('default/template/common/header.tpl', $data);
}
}
}
header.tpl
中的:
<!-- stores -->
<div class="fadelink fadelink_links">
<a><?php echo $name; ?></a>
<div class="ul_wrapper">
<ul>
<?php foreach ($stores as $store) {
if ($store['store_id'] == $store_id) { ?>
<li> <a href="<?php echo $store['url']; ?>"><b><?php echo $store['name']; ?></b></a> </li>
<?php } else { ?>
<li> <a href="<?php echo $store['url']; ?>"><?php echo $store['name']; ?></a> </li>
<?php } ?>
<?php } ?>
</ul>
</div>
</div>
<!-- stores -->
在控制器中出现问题,因为aray存储不会加载数据。我用另一个模块和$ stores ='value'测试了它;那些都很好。唯一的区别是商店值是配置值。有人知道我没有看到什么?
答案 0 :(得分:0)
您的tpl文件中存在错误。 它应该是
<?php foreach ($stores as $store) { ?>
<?php if ($store['store_id'] == $store_id) { ?>
<li> <a href="<?php echo $store['url']; ?>"><b><?php echo $store['name']; ?></b></a> </li>
<?php } else { ?>
<li> <a href="<?php echo $store['url']; ?>"><?php echo $store['name']; ?></a> </li>
<?php } ?>
答案 1 :(得分:0)
在tpl文件中调用变量时,从不使用$data
。而是使用$variable
名称。在tpl文件中,只需将$data['stores']
更改为$stores
。
CHANGE
<?php foreach ($data['stores'] as $store) { ?>
要
<?php foreach ($stores as $store) { ?>