how get Category name in custom Plugin

  • Posts: 138
  • Thank you received: 4
3 years 4 months ago #344217

-- 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:

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.

  • Posts: 84957
  • Thank you received: 13846
  • MODERATOR
3 years 4 months ago #344220

Hi,

Your code should be:

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.

  • Posts: 138
  • Thank you received: 4
3 years 4 months ago #344225

thanks for reply.
but i get this error:

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.

  • Posts: 84957
  • Thank you received: 13846
  • MODERATOR
3 years 4 months ago #344231

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.

  • Posts: 138
  • Thank you received: 4
3 years 4 months ago #344254

i attached screen of debug.please see


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

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

  • Posts: 84957
  • Thank you received: 13846
  • MODERATOR
3 years 4 months ago #344257

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.

  • Posts: 138
  • Thank you received: 4
3 years 4 months ago #344267

thank you


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

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

  • Posts: 3
  • Thank you received: 0
1 week 5 days ago #369950

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: 1 week 5 days ago by lencse.

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

  • Posts: 84957
  • Thank you received: 13846
  • MODERATOR
1 week 5 days ago #369956

Hi,

In the product listing view, you can get the current category ID from the URL parameter using:

$category_id = hikaInput::get()->getInt('cid');

If you want to get the category object with its name:
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.080 seconds
Powered by Kunena Forum