2016-12-05 5 views
1

カテゴリ内の製品を、デフォルトでフィルタ(位置、価格など)および数量で並べ替える必要があります。数量= 0の製品は、リストの最後になければなりません。PrestaShopカテゴリ内の複数のフィールドを並べ替える

Category.phpにおけるデフォルトのクエリ(https://github.com/PrestaShop/PrestaShop/blob/1.6.1.x/classes/Category.php#L688

私はBY注文する数量を追加しようとしているが、結果は唯一の位置

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; 
} 

によって並べ替えられて、私はこのクエリを変更しようとしていますavaliable_stockのためのサブクエリを追加することが、結果が同じであることで、位置のみ

$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; 
} 

によってソート誰もがこの問題を私に助言/助けることができますか? SQLで

答えて

1

ORDER BY文は次のように機能します。

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

これは、最初の列の値(位置)は常に異なっているので、あなたのMySQLの文では2番目の列が考慮されないことを意味します。あなたはいつも量によって順序はあなたが(real_quantityが既に正しいquantyti値を持っていることをasumming)このような何かを行う必要がありますしたい場合は、

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; 
} 

量0の製品はの最後に表示されなければならない場合あなたが以前に説明したように、デフォルトの注文はあなたのために働かないでしょう。デフォルト注文を維持するが、最後に在庫のない商品を表示する場合は、在庫が0より大きい商品(WHEREの条件を使用)の場合は1、デフォルト注文の場合は1、在庫なしの商品の場合は2つのSQLステートメントを作成する必要があります。その後、結果をマージします。

幸運。

+0

デフォルトオーダーを保持する必要があり、数量ゼロの商品は、ページ番号(LIMIT) – esik

+1

を持つリストの最後に表示されます。数量0の商品がリストの最後に表示される必要がある場合、デフォルト注文は機能しません前に説明したように。デフォルト注文を維持するが、最後に在庫がない商品を表示する場合は、在庫が0より大きい商品(WHERE条件を使用する)の場合は1、デフォルト注文の場合は1、在庫のない商品の場合は2つのSQLステートメントを作成する必要があります。その後、結果をマージします。 – PixelWeb

+0

私はより良い方法を見つけました。例えば、 'ORDER BY stock.quantity> 0 DESC、pl.name ASC'は最初に数量でソートし、次に名前でソートします – esik

関連する問題