Oshrib
Oshrib

Reputation: 1880

How to get current category id - OpenCart

I want to add to in header - if statement:

<?php if (category = 17) { ?>
<meta name="description" content="category 17 description" />
<?php } ?>

<?php if (category = 18) { ?>
<meta name="description" content="category 18 description" />
<?php } ?>

How can i get current category id

Upvotes: 1

Views: 15275

Answers (5)

Chandresh
Chandresh

Reputation: 371

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

Source: http://chandreshrana.blogspot.in/2014/07/get-current-category-in-opencart.html

Upvotes: 0

bIgBoY
bIgBoY

Reputation: 417

If you only require the category id , it is a url parameter "path". So, the best way to access category id in the header.tpl will be

<?php if(isset($_GET['category_id'])){
  if ($_GET['path'] = 17) { ?>
<meta name="description" content="category 17 description" />
<?php } ?>

<?php if ($_GET['path'] = 18) { ?>
 <meta name="description" content="category 18 description" />
<?php } 
}  

Upvotes: 2

shadyyx
shadyyx

Reputation: 16055

I guess best approach to do this is to add meta description from within the category controller, like this way:

$this->document->addMeta($category_info['description']);

Just edit Your /catalog/controller/product/category.php controller file.

Upvotes: 0

Jay Gilford
Jay Gilford

Reputation: 15151

Put this before the code in your header

$category = empty($this->request->get['path']) ? 0 : (int) array_pop(explode('_', $this->request->get['path']));

Then use $category instead of just category as you have in your question

Upvotes: 3

Maximii77
Maximii77

Reputation: 93

Where do you want to get the Category ID from?

Is it in the url?

Then you have to use the global $_GET variable.

if the url is

example.com/index.php?category=2

You can get the parameter with

$category= $_GET["category"]

Upvotes: 0

Related Questions