Getting all Products in a Category

  • Posts: 10
  • Thank you received: 0
9 years 10 months ago #185615

-- HikaShop version -- : latest
-- Joomla version -- : latest
-- PHP version -- : latest
-- Browser(s) name and version -- : ff latest

Hello Everybody,

Given a category ID, how can I get all the products in it?

I have tried this:

$category = hikashop_get('class.category');

//1.
$category->get(<cid>);


//2.
$category->getProductsIn(<cid>, array(<product>));

//3.
$category->getChildren(…)

None of the methods returns the list of products or their ids…

How can I do that?

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

  • Posts: 82865
  • Thank you received: 13372
  • MODERATOR
9 years 10 months ago #185634

Hi,

There is no function to do that.
The best is to write a MySQL query and load the data yourself from the tables hikashop_product_category and hikashop_product.
Otherwise, you can use that code:

$productClass = hikashop_get('class.product');
$productClass->getProducts();
$categoryClass = hikashop_get('class.category');
$products_in_the_category = $category->getProductsIn($category_id, $productClass->products);
But I don't recommend that as it first loads all the data of all the products in the getProducts() function (which is used for products exports) before loading the products based on the category id and these products list.

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

Time to create page: 0.057 seconds
Powered by Kunena Forum