微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PrestaShop 交叉销售产品按销售额排序 [PHP MOD]

如何解决PrestaShop 交叉销售产品按销售额排序 [PHP MOD]

我正在尝试修改 PrestaShop 模块 Crosselling,我需要添加 2 个模块:

  • 隐藏列表中的缺货商品。
  • 按产品与页面产品共享订单的次数对产品列表进行排序。

我找到了产品列表功能

if (count($orders) > 0) {
        $list = '';
        foreach ($orders as $order) {
            $list .= (int)$order['id_order'].',';
        }
        $list = rtrim($list,',');

        $list_product_ids = join(',$products_id);

        if (Group::isFeatureActive()) {
            $sql_groups_join = '
            LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = product_shop.id_category_default
                AND cp.id_product = product_shop.id_product)
            LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON (cp.`id_category` = cg.`id_category`)';
            $groups = FrontController::getCurrentCustomerGroups();
            $sql_groups_where = 'AND cg.`id_group` '.(count($groups) ? 'IN ('.implode(',$groups).')' : '='.(int)Group::getCurrent()->id);
        }

        $order_products = Db::getInstance(_PS_USE_sql_SLAVE_)->executeS('
            SELECT disTINCT od.product_id,pl.name,pl.description_short,pl.link_rewrite,p.reference,i.id_image,product_shop.show_price,cl.link_rewrite category,p.ean13,stock.out_of_stock,IFNULL(stock.quantity,0) as quantity
            FROM '._DB_PREFIX_.'order_detail od
            LEFT JOIN '._DB_PREFIX_.'product p ON (p.id_product = od.product_id)
            '.Shop::addsqlAssociation('product','p').
            (Combination::isFeatureActive() ? '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',$this->context->shop) :  Product::sqlStock('p','product',$this->context->shop)).'
            LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (pl.id_product = od.product_id'.Shop::addsqlRestrictionOnLang('pl').')
            LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = product_shop.id_category_default'
                .Shop::addsqlRestrictionOnLang('cl').')
            LEFT JOIN '._DB_PREFIX_.'image i ON (i.id_product = od.product_id)
            '.(Group::isFeatureActive() ? $sql_groups_join : '').'
            WHERE od.id_order IN ('.$list.')
            AND pl.id_lang = '.(int)$this->context->language->id.'
            AND cl.id_lang = '.(int)$this->context->language->id.'
            AND od.product_id NOT IN ('.$list_product_ids.')
            AND i.cover = 1
            AND stock.quantity > 1
            AND product_shop.active = 1
            '.(Group::isFeatureActive() ? $sql_groups_where : '').'
            ORDER BY RAND()
            LIMIT '.(int)Configuration::get('CROSSSELLING_NBR'));

        $tax_calc = Product::getTaxCalculationMethod();

        foreach ($order_products as &$order_product) {
            $order_product['id_product'] = (int)$order_product['product_id'];
            $order_product['image'] = $this->context->link->getimageLink($order_product['link_rewrite'],(int)$order_product['product_id'].'-'.(int)$order_product['id_image'],ImageType::getFormatedname('home'));
            $order_product['link'] = $this->context->link->getProductLink((int)$order_product['product_id'],$order_product['link_rewrite'],$order_product['category'],$order_product['ean13']);
            if (Configuration::get('CROSSSELLING_disPLAY_PRICE') && ($tax_calc == 0 || $tax_calc == 2)) {
                $order_product['displayed_price'] = Product::getPriceStatic((int)$order_product['product_id'],true,null);
            } elseif (Configuration::get('CROSSSELLING_disPLAY_PRICE') && $tax_calc == 1) {
                $order_product['displayed_price'] = Product::getPriceStatic((int)$order_product['product_id'],null);
            }
            $order_product['allow_oosp'] = Product::isAvailableWhenOutOfStock((int)$order_product['out_of_stock']);

            if (!isset($final_products_list[$order_product['product_id'].'-'.$order_product['id_image']])) {
                $final_products_list[$order_product['product_id'].'-'.$order_product['id_image']] = $order_product;
            }
        }
    }

    return $final_products_list;
}

所以,为了隐藏我添加的缺货商品:" AND stock.quantity > 1 " 看起来效果很好,但我不知道如何修改“ORDER BY RAND( )" 按 (count($orders)

的值对产品进行排序

可能是我对函数工作有误解,我的PHP技能有限。

感谢您的帮助

第一次尝试:

 $order_products = Db::getInstance(_PS_USE_sql_SLAVE_)->executeS('
            SELECT od.product_id,$this->context->shop)).'
            LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (pl.id_product = od.product_id'.Shop::addsqlRestrictionOnLang('pl').')
            LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = product_shop.id_category_default'
                .Shop::addsqlRestrictionOnLang('cl').')
            LEFT JOIN '._DB_PREFIX_.'image i ON (i.id_product = od.product_id)
            '.(Group::isFeatureActive() ? $sql_groups_join : '').'
            WHERE od.id_order IN ('.$list.')
            GROUP BY od.product_id
            AND pl.id_lang = '.(int)$this->context->language->id.'
            AND cl.id_lang = '.(int)$this->context->language->id.'
            AND od.product_id NOT IN ('.$list_product_ids.')
            AND i.cover = 1
            AND product_shop.active = 1
            AND stock.quantity > 0
            '.(Group::isFeatureActive() ? $sql_groups_where : '').'
            ORDER BY sum(od.quantity) DESC
            LIMIT '.(int)Configuration::get('CROSSSELLING_NBR'));

解决方法

原始查询已经加入ps_order_detail 表, 因此,您可以更轻松地按“订购产品数量”进行订购。

您需要从 select 中删除 DISTINCT,按 od.product_id 分组并按 sum(od.quantity) desc 排序 - 这可以使查询变得有点不过更重。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。