如果我已经在功能列表中对产品列表进行了分类,那么如何对缺货的产品进行分类?

时间:2017-02-15 07:32:19

标签: php magento-1.9 mergesort

如果我在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;
    }
}

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

但是我还需要对产品进行排序,只有大多数人看过的产品才能将它放在产品列表的底部我有Magento 1.9和1.9.3 - 这对你来说更简单。请有人帮帮我吗

1 个答案:

答案 0 :(得分:0)

您必须在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');