douzhengnao8265 2017-02-16 01:01
浏览 34

Magento 1.9 - 如何在双重条件下对产品进行两次分类?

I have already good and working code which sorted my category list by stock_status. Other words i have out of stock products at bottom. But i also need to insert some code before my for sorting that part of products which out of stock only.

For example:

Product1 have Qty=0 and product_views attribute=97
Product2 have Qty=0 and product_views attribute=902
Product3 have Qty=0 and product_views attribute=75
Product4 have Qty=35 and product_views attribute=1500
Product5 have Qty=398 and product_views attribute=5298
Product6 have Qty=275 and product_views attribute=17

Right now i have this results if i sort by name:

Product4
Product5
Product6
Product1
Product2
Product3

This is amazing for me because i need to sort in stock products by name (or any other choice in frontend as usually) but in same time i need to sort out of stock products by product_views attribute.

And I need to have the list sorted like this (if i choose in the frontend sort by name for example):

Product4
Product5
Product6 (this is in stock products that because it must be sorted as usually based on choice of selectbox in frontend)
Product2
Product1
Product3 (this is out of stock products that because it must be sorted by product_views attrubute - most viewed in top)

Right now i use this code for second level sorting (and i ASK YOU TO hELP ME find the 1 level sorting by product_views. But must be sorted the out of stock products only):

try {
            $websiteId = Mage::app()->getStore()->getWebsiteId();
            if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {


                $stockStatusFieldExisted = Mage::helper('webpiercom_outofstockmastmndmostviewed_catalog')->checkFieldExisted($collection->getSelect(), 'stock_status');

                if(!$stockStatusFieldExisted) {
                    $collection->joinTable(
                        array('wprdc' => 'cataloginventory/stock_status'),
                        'product_id=entity_id',
                        array('stock_status'),
                        array('website_id' => $websiteId),
                        'left'
                    );

                }
            }
            $collection->getSelect()->order('stock_status desc');
        } 
  • 写回答

1条回答 默认 最新

  • duanjitong7226 2017-02-18 23:06
    关注

    I'd be looking at adjusting my app/local/Mage/Catalog/Product/list/toolbar.php. Seek out the setCollection function. Where the call is

    $this->_collection->setOrder(
        $this->getCurrentOrder(), 
        $this->getCurrentDirection())
    

    You can stack up sort orders. Since you want stock first, I'd suggest doing something like

    $this->_collection
        ->setOrder('stock_status', 'desc')
        ->setOrder(
            $this->getCurrentOrder(),
            $this->getCurrentDirection());
    

    This is untested as I haven't a stock filter on any of my sites, but I use this technique with other field names to force default 2nd and 3rd level sorts.

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?