2016-12-05 3 views
1

Ich möchte Produkte in der Kategorie standardmäßig Filter (wie: Position, Preis ...) und nach Menge sortieren. Produkte mit der Menge = 0 müssen am Ende der Liste stehen.PrestaShop Sortierung mehrerer Felder in der Kategorie

Standardabfrage in category.php (https://github.com/PrestaShop/PrestaShop/blob/1.6.1.x/classes/Category.php#L688)

Ich versuche Menge hinzuzufügen, indem zu bestellen, aber Ergebnisse nur von Position sortiert werden

if ($random === true) { 
    $sql .= ' ORDER BY RAND(), stock.`quantity` DESC LIMIT '.(int)$random_number_products; 
} else { 
    $sql .= ' ORDER BY '.(!empty($order_by_prefix) ? $order_by_prefix.'.' : '').'`'.bqSQL($order_by).'` '.pSQL($order_way).', stock.`quantity` DESC 
    LIMIT '.(((int)$p - 1) * (int)$n).','.(int)$n; 
} 

Ich versuche, diese Abfrage zu ändern durch Unterabfrage für avaliable_stock Zugabe, aber die Ergebnisse sind die gleichen, sortiert nur von Position

$sql = 'SELECT p.*, product_shop.*, real_quantity.*'.(Combination::isFeatureActive() ? ', IFNULL(product_attribute_shop.id_product_attribute, 0) AS id_product_attribute, 
      product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity' : '').', pl.`description`, pl.`description_short`, pl.`available_now`, 
      pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, image_shop.`id_image` id_image, 
      il.`legend` as legend, m.`name` AS manufacturer_name, cl.`name` AS category_default, 
      DATEDIFF(product_shop.`date_add`, DATE_SUB("'.date('Y-m-d').' 00:00:00", 
      INTERVAL '.(int)$nb_days_new_product.' DAY)) > 0 AS new, product_shop.price AS orderprice 
     FROM `'._DB_PREFIX_.'category_product` cp 
     LEFT JOIN `'._DB_PREFIX_.'product` p 
      ON p.`id_product` = cp.`id_product` 
     '.Shop::addSqlAssociation('product', 'p'). 
     (Combination::isFeatureActive() ? ' LEFT JOIN `'._DB_PREFIX_.'product_attribute_shop` product_attribute_shop 
     ON (p.`id_product` = product_attribute_shop.`id_product` AND product_attribute_shop.`default_on` = 1 AND product_attribute_shop.id_shop='.(int)$context->shop->id.')':'').' 
     LEFT JOIN `'._DB_PREFIX_.'category_lang` cl 
      ON (product_shop.`id_category_default` = cl.`id_category` 
      AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').') 
     LEFT JOIN `'._DB_PREFIX_.'product_lang` pl 
      ON (p.`id_product` = pl.`id_product` 
      AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').') 
     LEFT JOIN `'._DB_PREFIX_.'image_shop` image_shop 
      ON (image_shop.`id_product` = p.`id_product` AND image_shop.cover=1 AND image_shop.id_shop='.(int)$context->shop->id.') 
     LEFT JOIN `'._DB_PREFIX_.'image_lang` il 
      ON (image_shop.`id_image` = il.`id_image` 
      AND il.`id_lang` = '.(int)$id_lang.') 
     LEFT JOIN `'._DB_PREFIX_.'manufacturer` m 
      ON m.`id_manufacturer` = p.`id_manufacturer` 
     LEFT JOIN (SELECT * FROM `'._DB_PREFIX_.'stock_available` ORDER BY `quantity` DESC) AS real_quantity 
      ON (real_quantity.id_product = p.id_product AND real_quantity.id_product_attribute = 0 AND real_quantity.id_shop = '.(int)$context->shop->id.') 
     WHERE product_shop.`id_shop` = '.(int)$context->shop->id.' 
      AND cp.`id_category` = '.(int)$this->id 
      .($active ? ' AND product_shop.`active` = 1' : '') 
      .($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '') 
      .($id_supplier ? ' AND p.id_supplier = '.(int)$id_supplier : ''); 

if ($random === true) { 
    $sql .= ' ORDER BY RAND(), real_quantity.`quantity` DESC LIMIT '.(int)$random_number_products; 
} else { 
    $sql .= ' ORDER BY '.(!empty($order_by_prefix) ? $order_by_prefix.'.' : '').'`'.bqSQL($order_by).'` '.pSQL($order_way).', real_quantity.`quantity` DESC 
    LIMIT '.(((int)$p - 1) * (int)$n).','.(int)$n; 
} 

Kann mir jemand helfen/beraten Sie mich mit diesem Problem?

Antwort

1

ORDER BY Satz in SQL funktioniert wie folgt:

ORDER BY column1, column2 /*order results by column1, if 2 rows has the same column1 value then compare by column2*/ 

Dies bedeutet, dass in Ihrem MySQL-Satz zweite Spalte wird nie berücksichtigt, da erster Spaltenwert (Positionen) ist immer wieder anders.

Dann, wenn Sie nach der Menge immer bestellen möchten, sollten Sie etwas tun (asumming dass real_quantity bereits die richtige quantyti Wert hat):

if ($random === true) { 
    $sql .= ' ORDER stock.`quantity` DESC LIMIT '.(int)$random_number_products; 
} else { 
    $sql .= ' ORDER BY stock.`quantity` DESC 
    LIMIT '.(((int)$p - 1) * (int)$n).','.(int)$n; 
} 

Wenn das Produkt mit der Menge 0 muss am Ende der erscheinen Liste, dann wird die Standardreihenfolge für Sie nicht funktionieren, wie ich Ihnen vorher erklärt habe. Wenn Sie die Standardbestellung beibehalten möchten, aber am Ende das Produkt ohne Lagerbestand anzeigen möchten, sollten Sie zwei separate SQL-Anweisungen eingeben: 1 für Produkte mit Warenbestand> 0 (unter Verwendung einer WHERE-Bedingung) für die Standardbestellung und die andere für Produkte ohne Lagerbestand. Dann füge Ergebnisse zusammen.

Viel Glück.

+0

Der Standardauftrag muss beibehalten werden, und Produkte mit der Menge Null erscheinen am Ende der Liste mit Paginierung (LIMIT) – esik

+1

Wenn am Ende der Liste ein Produkt mit der Menge 0 erscheinen muss, funktioniert die Standardreihenfolge nicht wie ich es dir vorher erkläre. Wenn Sie die Standardreihenfolge beibehalten möchten, aber am Ende das Produkt ohne Lagerbestand anzeigen möchten, sollten Sie zwei separate SQL-Anweisungen eingeben: 1 für Produkte mit Warenbestand> 0 (mit WHERE-Bedingung), die Standardbestellung und die andere mit Produkten ohne Lagerbestand. Dann füge Ergebnisse zusammen. – PixelWeb

+0

Ich habe einen besseren Weg gefunden, zum Beispiel 'ORDER BY stock.quantity> 0 DESC, pl.name ASC' sortiert zuerst nach Menge und dann nach Namen – esik

Verwandte Themen