代码之家  ›  专栏  ›  技术社区  ›  Daniel Koczuła

Prestashop产品列表不包括售罄

  •  0
  • Daniel Koczuła  · 技术社区  · 6 年前

    我想从产品列表中排除已售罄的产品(数量=0)。我试图修改产品列表中的foreach循环。第三方物流,但它没有按应有的方式运作。我试过这个:

    {if $product.quantity_all_versions != 0}
    

    售罄的产品是隐藏的,但它显示的不是21个产品,例如16个,并且分页中的计数是错误的。

    我认为更好的方法是修改$products变量。我在哪里可以找到它?或者修改sql查询更好?我在哪里可以找到它?

    //编辑

    我已更改查询:

    $sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity, MAX(product_attribute_shop.id_product_attribute) 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`, MAX(image_shop.`id_image`) id_image,
                        il.`legend`, m.`name` AS manufacturer_name, cl.`name` AS category_default,
                        DATEDIFF(product_shop.`date_add`, DATE_SUB(NOW(),
                        INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).'
                            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`
                        AND p.`quantity` > 0
                    '.Shop::addSqlAssociation('product', 'p').'
                    LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa
                    ON (p.`id_product` = pa.`id_product`)
                    '.Shop::addSqlAssociation('product_attribute', 'pa', false, 'product_attribute_shop.`default_on` = 1').'
                    '.Product::sqlStock('p', 'product_attribute_shop', false, $context->shop).'
                    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` i
                        ON (i.`id_product` = p.`id_product`)'.
                    Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1').'
                    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`
                    WHERE product_shop.`id_shop` = '.(int)$context->shop->id.'
                        AND cp.`id_category` IN('.implode(',',$cats_array).')'
                        .($active ? ' AND product_shop.`active` = 1' : '')
                        .($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '')
                        .($id_supplier ? ' AND p.id_supplier = '.(int)$id_supplier : '')
                        .' GROUP BY product_shop.id_product';
    

    我补充道:

    AND p.`quantity` > 0
    

    现在,在我的产品页面上的站点上没有显示任何产品,但当我在PhpMyAdmin中运行此SQL时,它正在工作。为什么?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Roman K.    6 年前

    我认为变量 $products 在中分配 controllers/front/CategoryController.php 使用 getProducts 功能来自 classes/Category.php

    因此,您需要在中编辑SQL查询 获取产品 .我不知道您的Prestashop的确切版本,但在1.6中有两个查询,您应该添加如下内容

     AND IFNULL(stock.quantity, 0) > 0 
    

    对他们来说。

    您也可以尝试 How to automatically disable out of stock products in PrestaShop 使用数据库tirgers的。

    有人甚至写了一个模块 Hide out of stock products in Prestashop automatically – Free Prestashop module 。这可能是最简单的解决方案。