2013-03-25 102 views
3

在產品搜索排序列表中是否有一種方法可以按照最高價到最低價訂購產品,但是最終會顯示零價格的產品。在Magento中按價格列出產品但在最後零價格

正常的分揀工作正常(從低到高或低),但真的想的能力,包括零級價格的產品在年底,它是如何將上市的一個例子是:

產品1:£3

產品2:£18

產品3:£0

產品4:£0

我還沒有找到一種方法,從這裏搜索或谷歌尚未與Magento太熟悉我不知道我會在哪裏尋找改變這一點。如果任何人有答案或可以指向我的查詢或正確的文件,我自己編輯我不介意自己去。

使用從這裏一些幫助和其他問題我已編輯的文件/app/code/core/Mage/Catalog/Block/Product/List.php_getProductCollection()方法(不用擔心,我已經複製到本地),並加入這幾行:

$orderFilterType = $this->getRequest()->getParam('order'); 
$dirFilterType = $this->getRequest()->getParam('dir'); 

if(isset($orderFilterType) && $orderFilterType == 'price' && isset($dirFilterType) && $dirFilterType == 'asc') { 

$this->_productCollection = $layer->getProductCollection()->addAttributeToSort('price', 'DESC'); 


} else { 

$this->_productCollection = $layer->getProductCollection(); 

} 

這意味着,無論我在這段代碼中做什麼,只有當某人通過升序選項通過價格下拉菜單選擇訂單時纔會運行。

現在的問題是我不知道該怎麼做才能影響$this->_productCollection = $layer->getProductCollection();的值才能使此返回正如我所願。

+0

你應該發佈一些代碼也。無論你嘗試過什麼 – 2013-04-05 05:06:13

+0

嗨Deepanshu,我已經更新了我現在的位置。 – 2013-04-08 11:12:52

回答

1
晚,但我知道我剛剛解決了這個在一個好得多的方式比其他的解決方案,我發現,可能幫助別人黨

4年。

在錄/塊/產品/列表/ Toolbar.php替換

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

$zeroPriceLast = new Zend_Db_Expr('`price_index`.`price` = 0 ASC, `price_index`.`price` ASC'); 

$this->_collection->getSelect()->order($zeroPriceLast); 

和在條件包裹,使得由價格排序時僅施加新的邏輯。

0

要在原始的SQL這樣做看起來像:

SELECT main.*, price.value as price FROM catalog_product_entity AS main 
    JOIN catalog_product_entity_decimal AS price ON price.entity_id = main.entity_id 
    JOIN eav_attribute AS attr_price ON attr_price = 'price' AND price.attribute_id = attr_price.attribute_id 
ORDER BY price == 0, price 

您可能必須替換核心塊以便獲得ORDER BY price ==0, price子句的工作。

0

我認爲你需要像這樣

  1. 每個產品列表中創建一個觀察者:

    <events> <catalog_block_product_list_collection> <observers> <rebuild_collection> <type>singleton</type> <class>XXX_YYY_Model_Catalog_Observer</class> <method>rebuildCollection</method> </rebuild_collection> </observers> </catalog_block_product_list_collection></events>

其中XXX - 您的命名空間 YYY - 你的模塊

  1. 創建PHP文件

    <?php 
    class XXX_YYY_Model_Catalog_Observer extends Mage_Catalog_Model_Observer 
    { 
    public function rebuildCollection($observer) 
    { 
    $event = $observer->getEvent(); 
    /** @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */ 
    $collection = $event->getCollection(); 
    
    $toolbar = Mage::getBlockSingleton('catalog/product_list_toolbar'); 
    
         // here you can add some attributes to collection but it's not required 
         // ex. $collection->addAttributeToSelect('brand') 
         // ->addAttributeToSelect('style'); 
         // you can also force visibility filter Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection); 
    
         // and here is main part for your logic 
         // getting current sort order 
         $arr = $collection->getSelect()->getPart(Zend_Db_Select::ORDER); 
         // probably here you will need to add condition like IF ORDERING BY PRICE 
         $r = Mage::app()->getRequest()->getRequestString(); 
    
         $order_field = 'new_price_for_sort'; 
         $collection->getSelect()->joinLeft(array('a' => 'catalog_product_entity_price'), // or here you will need Price Index Table Name if you have Groupped or configurable products. But need to remember that non-simple products always have 0 price and MAX_PRICE/MIN_PRICE should be used then 
             'a.product_id=e.entity_id AND a.attibute_id=PRICE or SPECIAL PRICE ATTRIBUTE ID', // or here you will not require attribute_id condition if you are using INDEX TABLE 
             array(
              'new_price_for_sort' => new Zend_Db_Expr('IF(a.value > 0,a.value, 9999999)'), // which mean that in new field we will have price if it's not 0, and will have big integer is real price is zero. 
             )); 
           } 
    // now we are reseting current order by 
           $collection->getSelect()->reset(Zend_Db_Select::ORDER); 
    
    // and creating new order by new field, but with saving direction of order 
    $dir = $arr['dir']; 
           if (!$collection->isEnabledFlat()) { 
            $collection->setOrder($order_field, $dir); 
           } else { 
            $collection->getSelect()->order($order_field . ' ' . $dir); 
           } 
    
    
          } 
         //check in log if collection real built well 
         Mage::log((string)$collection->getSelect(), null, 'selects.log'); 
         // add collection to list toolbar 
         $toolbar->setCollection($collection); 
        } 
    return $collection; 
    } 
    } 
    

PS對不起,我沒有正確複查此代碼,但複製的大部分來自實際工作的觀察者。 但可以有一些misstypes或遺漏字符)

+0

嗨@TaganPablo,謝謝你,這看起來很有趣。然而,對Magento瞭解不多,我甚至不知道從哪裏開始實施這個或爲產品列表創建觀察者,命名空間或模塊 – 2013-04-02 09:22:17

+0

下面是有用的手冊http://www.magentocommerce.com/wiki/5_ -_modules_and_development/0 _-_ module_development_in_magento/customizing_magento_using_event-observer_method – TaganPablo 2013-04-02 10:49:50