how get Category name in custom Plugin

  • Posts: 138
  • Thank you received: 4
2 years 5 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: 83338
  • Thank you received: 13477
  • MODERATOR
2 years 5 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
2 years 5 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: 83338
  • Thank you received: 13477
  • MODERATOR
2 years 5 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
2 years 5 months ago #344254

i attached screen of debug.please see


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

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

  • Posts: 83338
  • Thank you received: 13477
  • MODERATOR
2 years 5 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
2 years 5 months ago #344267

thank you


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

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

Time to create page: 0.061 seconds
Powered by Kunena Forum