diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php
index 800fa6e59d1..fc6b8e67395 100644
--- a/htdocs/core/class/html.form.class.php
+++ b/htdocs/core/class/html.form.class.php
@@ -2037,9 +2037,20 @@ class Form
$selectFields = " p.rowid, p.label, p.ref, p.description, p.barcode, p.fk_product_type, p.price, p.price_ttc, p.price_base_type, p.tva_tx, p.duration, p.fk_price_expression";
(count($warehouseStatusArray)) ? $selectFieldsGrouped = ", sum(ps.reel) as stock" : $selectFieldsGrouped = ", p.stock";
-
+
$sql = "SELECT ";
$sql.= $selectFields . $selectFieldsGrouped;
+
+ if (! empty($conf->global->PRODUCT_SORT_BY_CATEGORY))
+ {
+ //Product category
+ $sql.= ", (SELECT ".MAIN_DB_PREFIX."categorie_product.fk_categorie
+ FROM ".MAIN_DB_PREFIX."categorie_product
+ WHERE ".MAIN_DB_PREFIX."categorie_product.fk_product=p.rowid
+ LIMIT 1
+ ) AS categorie_product_id ";
+ }
+
//Price by customer
if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES) && !empty($socid))
{
@@ -2144,9 +2155,21 @@ class Form
{
$sql.= ' GROUP BY'.$selectFields;
}
- $sql.= $db->order("p.ref");
- $sql.= $db->plimit($limit, 0);
+
+ //Sort by category
+ if(! empty($conf->global->PRODUCT_SORT_BY_CATEGORY))
+ {
+ $sql .= " ORDER BY categorie_product_id ";
+ //ASC OR DESC order
+ ($conf->global->PRODUCT_SORT_BY_CATEGORY == 1) ? $sql .="ASC" : $sql .="DESC";
+ }
+ else
+ {
+ $sql.= $db->order("p.ref");
+ }
+ $sql.= $db->plimit($limit, 0);
+
// Build output string
dol_syslog(get_class($this)."::select_produits_list search product", LOG_DEBUG);
$result=$this->db->query($sql);