Shoveling a great many forums and trying to write a bicycle did not find a solution for additional sorting of goods in Opecart 2.1.0.2 The task is to add products that do not have an image (worth the default image) to the end of the list. I tried to do this in the frontend part through the php script, but I came to the conclusion that this does not work correctly, since it sorts the goods on each individual page of the product list in the sections of the category.

I came to the fact that this is done either in the product model as an additional sorting parameter to all existing ones, or in the category controller file, but at the moment my knowledge is not so big in OOP php.

Has anyone encountered a similar task?

How to implement this type of sorting which will be taken into account with all existing ones?

  • anyone has come across for sure. What is your question? please specify by clicking edit . - aleksandr barakin
  • specified: How to implement this type of sorting which will be taken into account with all existing ones? and more specifically - products that have an image are always displayed first + if this is possible another condition for which the price is indicated - Demonmeri

1 answer 1

File catalog/model/catalog/product.php , getProducts() method, line 166

 if (isset($data['sort']) && in_array($data['sort'], $sort_data)) { if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') { $sql .= " ORDER BY LCASE(" . $data['sort'] . ")"; } elseif ($data['sort'] == 'p.price') { $sql .= " ORDER BY (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)"; } else { $sql .= " ORDER BY " . $data['sort']; } } else { $sql .= " ORDER BY p.sort_order"; } 

to replace

 if (isset($data['sort']) && in_array($data['sort'], $sort_data)) { if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') { $sql .= " ORDER BY IF(p.image = '', 1, 0), LCASE(" . $data['sort'] . ")"; } elseif ($data['sort'] == 'p.price') { $sql .= " ORDER BY IF(p.image = '', 1, 0), (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)"; } else { $sql .= " ORDER BY IF(p.image = '', 1, 0), " . $data['sort']; } } else { $sql .= " ORDER BY IF(p.image = '', 1, 0), p.sort_order"; } 

In this example, the last item will be displayed products with an empty image field, that is, goods without the main image. If you need to filter out any specific values, the condition in IF() needs to be changed.

  • And if the load to this condition, I need to do the same with the lack of price? - Demonmeri
  • If the price should always be sorted, then you will have to add another sorting parameter to the long-suffering query. It is necessary to operate with the same piece of code. - tutankhamun