如何获取当前类别ID-OpenCart 2.0 [英] How to get current category id - OpenCart 2.0
问题描述
我有一个用于产品tpl的代码,如果类别ID = 12回显,我需要一个条件.下面的代码在opencart 1.5.6.4上有效,但在2.0上却不生成任何东西.
I have a code for product tpl witch I need a condition if a category id=12 echo.. THe code below works on opencart 1.5.6.4 but on 2.0 doesn't generate anything.
<?php if(isset($this->request->get['path'])) {
$path = $this->request->get['path'];
$cats = explode('_', $path);
$cat_id = $cats[count($cats) - 1];
}
?>
<?php if (isset($cat_id) && $cat_id == '108') { ?>
<div style="text-align: right;"><a href = "javascript:void(0)" onclick ="document.getElementById('fade').style.display='block'" style="
color: rgb(221, 0, 23);">Mesurments table</a></div>
<div id="fade" class="black_overlay"><a class="close" href = "javascript:void(0)" onclick = "document.getElementById('fade').style.display='none'"></a><img src="/image/data/misc/blugi.jpg"style="
width: 100%;"></div>
?php } ?>
推荐答案
之所以不再起作用,是因为在2.0>中,模板是通过Loader对象而不是Controller对象呈现的.
The reason this no longer works is because in 2.0> templates are rendered via the Loader object, not the Controller object.
您需要在产品控制器中将您的$cat_id
变量声明为数据变量.
You'll need to declare your $cat_id
variable inside the product controller as a data variable.
所以让我们清理一下并使它更有用.
So let's clean this up and make it a bit more usable.
在catalog/controller/product/product.php
中添加:
if (isset ($this->request->get['path'])) {
$path = $this->request->get['path'];
$cats = explode('_', $path);
$data['cat_id'] = $cats[count($cats) - 1];
}
然后在您的模板中,您可以根据需要访问$cat_id
.
Then in your template you can access $cat_id
as you like.
在您的product.tpl
文件中,在底部的javascript区域中添加:
In your product.tpl
file, in the javascript area at the bottom add:
<script type="text/javascript"><!--
$('#fade-link').css({
color: rgb(221, 0, 23)
}).on('click', function() {
$('#fade').show();
});
$('#fade a.close').on('click', function(){
$('#fade').hide();
});
//--></script>
然后将您的现有代码替换为:
Then replace your existing code with:
<?php if ($cat_id && $cat_id == '108') { ?>
<div style="text-align: right;">
<a id="fade-link">Measurements Table</a></div>
<div id="fade" class="black_overlay">
<a class="close"></a>
<img src="/image/data/misc/blugi.jpg" style="width: 100%;">
</div>
<?php } ?>
这应该可以带您到那里,或者至少离我很近,我没有对其进行测试,您可能需要调整JS.
This should get you there, or at least be pretty close, I didn't test it, you may need to adjust the JS.
这篇关于如何获取当前类别ID-OpenCart 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!