简体   繁体   English

仅当我已经对功能正常的产品列表进行排序时,如何对缺货的产品进行排序?

[英]How to sort the products which are out of stock only if I already sorted the list of products in function?

if i have this in Layer.php 如果我在Layer.php中有这个

class WebPierCom_OutOfStockLastAndMostViewed_Catalog_Model_Layer extends Mage_Catalog_Model_Layer
{
    public function prepareProductCollection($collection)
    {
        parent::prepareProductCollection($collection);
        if (!Mage::helper('webpiercom_outofstockmastmndmostviewed_catalog')->isSortOutOfStockProductsAtBottomEnabled()) {
            return $this;
        }

        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');
        } 
        catch (Exception $e) {}
        return $this;
    }
}

And i have in Helper.php this 我在Helper.php中有这个

class WebPierCom_OutOfStockLastAndMostViewed_Catalog_Helper_Data extends Mage_CatalogInventory_Helper_Data
{
    const XML_PATH_SORT_OUT_OF_STOCK    = 'cataloginventory/options/sort_out_of_stock_at_bottom';
    const XML_PATH_SORT_OUT_OF_STOCK_BY_MOST_VIEWED    = 'cataloginventory/options/sort_out_of_stock_at_bottom_by_most_viewed';
    const XML_PATH_SORT_OUT_OF_STOCK_SEARCH_RESULT = 'cataloginventory/options/sort_out_of_stock_at_bottom_for_search';


    public function isSortOutOfStockProductsAtBottomEnabled()
    {
        return $this->isShowOutOfStock() && Mage::getStoreConfigFlag(self::XML_PATH_SORT_OUT_OF_STOCK);
    }

    public function isSortOutOfStockProductsAtBottomByMostViewedEnabled()
    {
    return $this->isShowOutOfStock() && Mage::getStoreConfigFlag(self::XML_PATH_SORT_OUT_OF_STOCK_BY_MOST_VIEWED);
    }

    public function isEnabledForSearchResults()
    {
        return $this->isShowOutOfStock() && Mage::getStoreConfigFlag(self::XML_PATH_SORT_OUT_OF_STOCK_SEARCH_RESULT);
    }

    public function checkFieldExisted($select, $field)
    {
        $result = false;
        if($field) {
            $columns = $select->getPart(Zend_Db_Select::COLUMNS);
            foreach ($columns as $column) {
                if (in_array($field , $column)) {
                    $result = true;
                    break;
                }
            }           
        }
        return $result;
    }
}

But i need also to sort products which are out of stock only by most viewed before place it at bottom of product list I have Magento 1.9 and 1.9.3 - which is simplier for you. 但是,我还需要对仅按大多数浏览量来看缺货的产品进行排序,然后再将其放在产品列表的底部,我有Magento 1.9和1.9.3-对您来说更简单。 Can anybody help me please 有人可以帮我吗

You must use this code in your layer.php file to resolve your query. 您必须在layer.php文件中使用此代码来解决您的查询。

 $collection->getSelect()->joinLeft(
        array('_inventory_table'=>$this->getTable('cataloginventory/stock_item')),
        "_inventory_table.product_id = e.entity_id",
        array('is_in_stock', 'manage_stock')
    );
    $collection->addExpressionAttributeToSelect('on_top',
    '(CASE WHEN (((_inventory_table.use_config_manage_stock = 1) AND (_inventory_table.is_in_stock = 1)) OR  ((_inventory_table.use_config_manage_stock = 0) AND (1 - _inventory_table.manage_stock + _inventory_table.is_in_stock >= 1))) THEN 1 ELSE 0 END)',
     array());
    $collection->getSelect()->order('on_top DESC');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM