Liviu Daniel
Liviu Daniel

Reputation: 63

How to get current category id - OpenCart 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 } ?>

Upvotes: 1

Views: 3776

Answers (1)

secondman
secondman

Reputation: 3277

The reason this no longer works is because in 2.0> templates are rendered via the Loader object, not the Controller object.

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.

In catalog/controller/product/product.php add:

if (isset ($this->request->get['path'])) {
    $path = $this->request->get['path'];
    $cats = explode('_', $path);
    $data['cat_id'] = $cats[count($cats) - 1];
}

Then in your template you can access $cat_id as you like.

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 } ?>

This should get you there, or at least be pretty close, I didn't test it, you may need to adjust the JS.

Upvotes: 2

Related Questions