Luca Reghellin
Luca Reghellin

Reputation: 8101

link to products belonging to more than one category in prestashop?

Ive got a category structure like this:

Category 1

Category 4

Category 7

Category 8

And wanna show all the products that belong to Category 8 AND Category 3. Category 8 is not a child of 3.

How can I do it? How (first of all) can I build the relative link?

Upvotes: 0

Views: 337

Answers (1)

Bruno Leveque
Bruno Leveque

Reputation: 2811

You have several ways to achieve this, however in terms of performance it mostly depends on the size of your catalog.

Here is an example written in SQL that would be very fast and also make sure the selected products are active:

$products_categories = Db::getInstance()->ExecuteS('
SELECT cp.id_product, pl.name product_name
FROM '._DB_PREFIX_.'category_product cp
LEFT JOIN '._DB_PREFIX_.'category c ON (c.id_category = cp.id_category)
LEFT JOIN '._DB_PREFIX_.'product p ON (p.id_product = cp.id_product)
LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (pl.id_product = p.id_product)
WHERE cp.id_category = 3 AND p.active = 1 AND c.active = 1 and pl.id_lang = 1 AND cp.id_product IN (SELECT id_product FROM '._DB_PREFIX_.'category_product WHERE id_category = 8)');

Upvotes: 1

Related Questions