How to get all products from category (PHP)

  • Posts: 88
  • Thank you received: 19
1 year 7 months ago #344078

-- HikaShop version -- : 4.6.1
-- Joomla version -- : 4.x
-- PHP version -- : 8.0

I'm working on an integration to YOOtheme Pro. Currently on the category with products listing.
I wonder if a function exists to get all products of a given category, like how we can get a product with:

$productClass = hikashop_get('class.product');
$product = $productClass->getProduct($id);

Or do I need to get them manually using a mysql query?

Thanks

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

  • Posts: 88
  • Thank you received: 19
1 year 7 months ago #344079

I could do something like this:

$db 	= JFactory::getDbo();
$sql = 'SELECT product_id FROM #__hikashop_product_category WHERE category_id = '.$params->category.'';
$db->setQuery($sql);
$product_ids = $db->loadColumn();

$productClass = hikashop_get('class.product');
foreach ($product_ids as $product_id) {
	$products[] = $productClass->getProduct($product_id);
}

But is this the best way?

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

  • Posts: 81481
  • Thank you received: 13062
  • MODERATOR
1 year 7 months ago #344080

Hi,

There is no function for that.
You have the function getProductsIn in class.category which is quite close to what you need. But you would need to remove the $products parameter and the code using it. I think this function is a good base even if you just want to get the MySQL query.

The following user(s) said Thank You: dyvel

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

Time to create page: 0.058 seconds
Powered by Kunena Forum