Hmm, no that doesn't work either Nicolas, hopefully you can take a minute to look at your code, because I think you may have it mixed up. Apologies if I'm wrong, but the query
$product_matches = array(
'discount_product_id IN (\'\',\'0\',\''.implode('\',\'', $all_ids) . '\')',
);
is never going to return any results, right.
- If you create a discount on some products, in the database the discount_product_id's are stored as for example:
,9,13,7,
- In a product listing the $product_matches can look something like:
discount_product_id IN ('','0','6','10','14','9','8','7','13','5')
This means it will try to match the
entire string ",9,13,7," against '6' or '10' or '14', etc. which is never true, isn't it?
It will compare discount_product_id to each value and look for an exact match, which even with a discount applied to only one product will never happen because discount_product_id will have commas around it and e.g. '
,9,' is not equal to '
9'.
The LIKE queries created in the foreach loop on the other hand will return true since
,9,13,7, will match
%,13,%. So instead of
$product_matches = array(
'discount_product_id IN (\'\',\'0\',\''.implode('\',\'', $all_ids) . '\')',
);
foreach($ids as $id) {
$product_matches[] = 'discount_product_id LIKE \'%,'.(int)$all_ids.',%\'';
}
the IN doesn't do anything and what seems to work correctly so far as we've tested is
$product_matches = array();
foreach($all_ids as $id) {
$product_matches[] = 'discount_product_id LIKE \'%,'.(int)$id.',%\'';
}
In any case, hopefully you can take a look at it, thanks!