Skip to main content

how get Category name in custom Plugin

4 years 2 months ago #344217 by aminweb
-- HikaShop version -- : 4.6.1
-- Joomla version -- : 4.1.5
-- PHP version -- : 7.4
-- Browser(s) name and version -- : chrome

hi,
I developing new plugin(system) for Hikashop.
how can get Category name of Product ?
this is my code:
Code:
function getProductHikashopInfo() { //load HikaShop if not already loaded if(!defined('DS')) define('DS',DIRECTORY_SEPARATOR); if(!include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')) return true; //get product id $product_id = (int)hikashop_getCID('product_id'); if(empty($product_id )) return true; // load product $productClass = hikashop_get('class.product'); $categoryClass = hikashop_get('class.category'); $product_category = $categoryClass->get($product_id )->category_name; echo $product_category; // echo "Root" }
this code output ===> "Root" instead of product category name

Joomla Developer | am.ebrahimzadeh[at]gmail.com

Please Log in or Create an account to join the conversation.

4 years 2 months ago #344220 by nicolas
Hi,

Your code should be:
Code:
function getProductHikashopInfo() { //load HikaShop if not already loaded if(!defined('DS')) define('DS',DIRECTORY_SEPARATOR); if(!include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')) return true; //get product id $product_id = (int)hikashop_getCID('product_id'); if(empty($product_id )) return true; $categoryClass = hikashop_get('class.category'); $categories = $categoryClass->getCategories($product_id); $product_category = reset($categories)->category_name; echo $product_category; }

Please Log in or Create an account to join the conversation.

4 years 2 months ago #344225 by aminweb
thanks for reply.
but i get this error:
Code:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM t50ow_hikashop_category WHERE category_id IN (3)' at line 1

Joomla Developer | am.ebrahimzadeh[at]gmail.com

Please Log in or Create an account to join the conversation.

4 years 2 months ago #344231 by nicolas
Hi,

That error doesn't come from the code I provided.
Normally, if you activate the "debug" setting of the Joomla configuration, you should get the call stack of the error so you should be able to see in which line of which file the issue is.

Please Log in or Create an account to join the conversation.

4 years 2 months ago - 4 years 2 months ago #344254 by aminweb
i attached screen of debug.please see

Joomla Developer | am.ebrahimzadeh[at]gmail.com
Last edit: 4 years 2 months ago by aminweb.

Please Log in or Create an account to join the conversation.

4 years 2 months ago #344257 by nicolas
Hi,

I know, I gave you:
$categoryClass = hikashop_get('class.category');
$categories = $categoryClass->getCategories($product_id);
but it should be:
$productClass = hikashop_get('class.product');
$categories = $productClass->getCategories($product_id);
The following user(s) said Thank You: aminweb

Please Log in or Create an account to join the conversation.

4 years 2 months ago #344267 by aminweb
thank you

Joomla Developer | am.ebrahimzadeh[at]gmail.com

Please Log in or Create an account to join the conversation.

9 months 4 weeks ago - 9 months 4 weeks ago #369950 by lencse
Hello.

How can I get the current (displayed) category id in a custom plugin without knowing the product id or anything else?
(product listing view)

Ty.
Last edit: 9 months 4 weeks ago by lencse.

Please Log in or Create an account to join the conversation.

9 months 3 weeks ago #369956 by nicolas
Hi,

In the product listing view, you can get the current category ID from the URL parameter using:
Code:
$category_id = hikaInput::get()->getInt('cid');

If you want to get the category object with its name:
Code:
if(!defined('DS')) define('DS',DIRECTORY_SEPARATOR); if(!include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')) return; $category_id = hikaInput::get()->getInt('cid'); if(!empty($category_id)) { $categoryClass = hikashop_get('class.category'); $category = $categoryClass->get($category_id); echo $category->category_name; }
Note that if you are in the context of a HikaShop view file (like a listing template), the category ID is often already available in the $this->pageInfo->filter->cid variable.

Please Log in or Create an account to join the conversation.

Time to create page: 0.086 seconds
Powered by Kunena Forum