I'm trying to get an array of items from MySQL database to show specific products depending on manufacturer ID in cart (prestashop). I made this query
$cart_items = $cartObj->getProducts();
if (is_array($cart_items) && count($cart_items) > 0) {
$cart_ids = array();
foreach ($cart_items as $cart_item) {
$cart_ids[] = $cart_item['id_product'];
}
$arrayKeys = array_keys($cart_ids);
$cart_ids[$arrayKeys[0]];
$id_manufacturers = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT id_manufacturer
FROM `'._DB_PREFIX_.'product`
WHERE id_product IN (' . implode(',', array_map('intval', $cart_ids)) . ')
');
$items = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
select id_product
from `'._DB_PREFIX_.'product` p
where p.id_manufacturer IN (' . implode(',', array_map('intval', $id_manufacturers)) . ')
'.(count($excluded_ids) ? ' AND p.id_product NOT IN (' . implode(', ', $excluded_ids) . ')' : '').'
group by p.id_product
limit '.(int)$limit.'
');
}
And nothing shows up. I know, when i use Db::getInstance->getValue in $id_category_default when trying to retrieve products just from 1 manufacturer, everything works fine. And when i'm adding test array for $items, such as (1, 2, 3, 4, 5), products also show up from this manufacturers id. But when i'm trying to obtain an array of id_manufacturer and show products based on that array - nothing shows up. Is there some other way to create array for id_manufacturers?
DB strucutre is something like this
+------+-------+--------------------------------------------+
| id_product | id_manufacturer | content |
+------+-------+--------------------------------------------+
| 1 | 1 | ... |
| 2 | 1 | ... |
| 3 | 2 | ... |
| 4 | 3 | ... |
+------+-------+--------------------------------------------+
And i need an array of id_manufacturer ids, like (1, 2, 3)
Try this code:
$manufacturers = [];
foreach ($cart->getProducts() as $product) {
$manufacturers[] = $product['id_manufacturer'];
}
$items = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT p.`id_product`
FROM `'._DB_PREFIX_.'product` p
WHERE p.`id_manufacturer` IN (' . implode(',', $manufacturers) . ')
'.(count($excluded_ids) ? ' AND p.`id_product` NOT IN (' . implode(', ', $excluded_ids) . ')' : '').'
GROUP BY p.`id_product`
LIMIT '.(int) $limit
);
Make sure to use correct aliases, and sometimes it's worth checking ``
inside your query. Let me know if that works for you.