BUG: Customer Also Bought doesn't work correctly

Latest 2.0.2RC.



Customer Also Bought doesn’t work correctly.



Did a test by buying 4 different products in one order.



Went back to one of the products. The “Customer Also Bought” list shows the current product name selected, 4 times. Although the link is actually correct.



Say the related products:



Product A, Link A

Product B, Link B

Product C, Link C

Product D, Link D



Say I select Product A.



Customer Also Bought shows:



Product A, Link A

Product A, Link B

Product A, Link C

Product A, Link D





If I select Product B.



Customer Also Bought shows:



Product B, Link A

Product B, Link B

Product B, Link C

Product B, Link D





The product name in the “Customer Also Bought” box is whatever product I selected.

Hi hykit,



Please provide a link to your store and I will do my best to help you.



Thank you.

Thanks for the offer to help.



But it’s definitely a bug with the “customers_also_bought” addon.



The file “func.php” contains the function



```php function fn_customers_also_bought_get_products(&$params, &$fields, &$sortings, &$condition, &$join, &$sorting, &$group_by)

{

if (!empty($params[‘also_bought’]) && !empty($params[‘product_id’])) {

$fields[0] = ‘?:also_bought_products.related_id AS product_id’; // FIXME, hard-coded key

$fields[] = ‘SUM(?:also_bought_products.amount) amnt’;

$join .= ’ LEFT JOIN ?:also_bought_products ON ?:also_bought_products.product_id = products.product_id ‘;

$condition .= db_quote(’ AND ?:also_bought_products.product_id = ?i’, $params[‘product_id’]);

$group_by = ‘?:also_bought_products.related_id’;

}



return true;

} ```



The query is wrong.



For the product_id=414, it generates the query:



```php

SELECT ?:also_bought_products.related_id AS product_id, descr1.product as product, products.tracking, products.feature_comparison, products.zero_price_action, products.product_type, products.tax_ids, GROUP_CONCAT(IF(products_categories.link_type = ‘M’, CONCAT(products_categories.category_id, ‘M’), products_categories.category_id)) as category_ids, min_qty, max_qty, products.qty_step, products.list_qty_count, avail_since, buy_in_advance, products.product_code, products.amount, MIN(prices.price) as price, products.status, products.list_price, descr1.short_description, IF(descr1.short_description = ‘’, descr1.full_description, ‘’) as full_description, products.is_edp, SUM(?:also_bought_products.amount) amnt, products.is_oper, products.is_op, products.is_pbp FROM ?:products as products LEFT JOIN ?:product_descriptions as descr1 ON descr1.product_id = products.product_id AND descr1.lang_code = ‘EN’ LEFT JOIN ?:product_prices as prices ON prices.product_id = products.product_id AND prices.lower_limit = 1 INNER JOIN ?:products_categories as products_categories ON products_categories.product_id = products.product_id INNER JOIN ?:categories ON ?:categories.category_id = products_categories.category_id AND ?:categories.membership_id IN (0, 0) AND ?:categories.status IN (‘A’, ‘H’) AND products.status IN (‘A’) LEFT JOIN ?:also_bought_products ON ?:also_bought_products.product_id = products.product_id WHERE 1 AND prices.membership_id IN (0, 0) AND ?:also_bought_products.product_id = 414 GROUP BY ?:also_bought_products.related_id ORDER BY descr1.product asc LIMIT 0, 6 ```

Actually, here’s the fix:



Change the function:



```php function fn_customers_also_bought_get_products(&$params, &$fields, &$sortings, &$condition, &$join, &$sorting, &$group_by)

{

if (!empty($params[‘also_bought’]) && !empty($params[‘product_id’])) {

$fields[0] = ‘?:also_bought_products.related_id AS product_id’; // FIXME, hard-coded key

$fields[] = ‘SUM(?:also_bought_products.amount) amnt’;

$join .= ’ LEFT JOIN ?:also_bought_products ON ?:also_bought_products.product_id = products.product_id ‘;

$condition .= db_quote(’ AND ?:also_bought_products.product_id = ?i’, $params[‘product_id’]);

$group_by = ‘?:also_bought_products.related_id’;

}



return true;

}

```



to



```php function fn_customers_also_bought_get_products(&$params, &$fields, &$sortings, &$condition, &$join, &$sorting, &$group_by)

{

if (!empty($params[‘also_bought’]) && !empty($params[‘product_id’])) {

$fields[0] = ‘?:also_bought_products.related_id AS product_id’; // FIXME, hard-coded key

$fields[] = ‘SUM(?:also_bought_products.amount) amnt’;

$join .= ’ LEFT JOIN ?:also_bought_products ON ?:also_bought_products.related_id = products.product_id ‘;

$condition .= db_quote(’ AND ?:also_bought_products.product_id = ?i’, $params[‘product_id’]);

$group_by = ‘?:also_bought_products.related_id’;

}



return true;

} ```

Thanks a lot for your help in resolving the issue. It is greatly appreciated.

In fact, we have already come across this bug before and fixed it.

Nevertheless, thanks once again for the provided information.

Dear,

Is it possible to order by amount (on 3.0.5 version)?



function fn_customers_also_bought_get_products(&$params, &$fields, &$sortings, &$condition, &$join, &$sorting, &$group_by)
{
if (!empty($params['also_bought_for_product_id'])) {
$fields[] = 'SUM(?:also_bought_products.amount) amnt';
$join .= ' LEFT JOIN ?:also_bought_products ON ?:also_bought_products.related_id = products.product_id ';
$condition .= db_quote(' AND ?:also_bought_products.product_id = ?i', $params['also_bought_for_product_id']);
$group_by = '?:also_bought_products.related_id';
}
return true;
}


Regards

[color=#282828][font=arial, verdana, tahoma, sans-serif]Dear,[/font][/color]

[color=#282828][font=arial, verdana, tahoma, sans-serif]I ve not found the solution yet, anybody has an idea?[/font][/color]

[color=#282828][font=arial, verdana, tahoma, sans-serif]Regards[/font][/color]

Dear,

Always nobody?

Regards

Sort the “customer also bought” list by the amount (number) of items sold? For which CS-Cart version?

Dear,

Yes, sort by the number of items bought with the product.

For V4.1.2, and seems to be the same code on V3.

Regards