2011-07-03 18 views
0

我是一個magento的新手,我需要你的幫助。 我需要將「缺貨」產品移至搜索結果列表的末尾。我怎樣才能做到這一點?如何在搜索後將「缺貨」產品移至列表末尾?

我發現模塊catalogsearch用於搜索。我也看到有一個表cataloginventory_stock_itemis_in_stock字段。這是檢查產品是否缺貨的正確標誌。我想我應該在從數據庫獲取數據時添加一個訂單條件。但我無法找到該數據在哪裏(一個文件)得到的形式catalogsearch_querycatalogsearch_result表以及我應該在哪裏(以及如何)添加訂單條件。你可以給我一個例子嗎?

回答

0

我做到了。我想與你分享我的方法。也許這對於已經與magento合作但不適合新手的人來說很容易。

在「CatalogSearch」模塊是負責通過產品目錄搜索。模塊的路徑是'DOC_ROOT/app/code/core/Mage/CatalogSearch'。

首先,我們不應該修改的核心文件(「法師」文件夾中的文件),並且必須覆蓋核心類和功能。 爲了達到我們的目標(我的話題問題),我們應該在不同的類中編輯一些函數,所以我們需要重寫這個函數。

的是在Magento 2類型的搜索思想產品目錄的:簡單和高級(也許更多,但我不知道其他),我們會在這2種影響。

第1步: 我們應該告訴Magento的引擎,我們想代替它的一些核心類。因此,我們創建自己的模塊。轉到 '/應用程序的/ etc /模塊/' 和創建文件 'YourCompany_YourModuleName.xml' 有如下內容:

<?xml version="1.0"?> 
<config> 
    <modules> 
     <YourCompany_YourModuleName> 
      <active>true</active> 
      <codePool>local</codePool> 
     </YourCompany_YourModuleName> 
    </modules> 
</config> 

第2步: 現在我們應該創建我們的模塊。轉到'/ app/code/local/YourCompany/YourModuleName /'並創建文件夾'etc'。把'config.xml'文件放在這個'etc'文件夾中,我們必須告訴magetno我們想要覆蓋哪些文件/類。

config.xml的內容:

<?xml version="1.0"?> 
<config> 
    <modules> 
     <yourcompany_yourmodulename> 
      <version>0.1.0</version> 
     </yourcompany_yourmodulename> 
    </modules> 
    <global> 
     <models> 
      <catalogsearch> 
       <rewrite> 
        <layer>YourCompany_YourModuleName_Model_CatalogSearch_Layer</layer> 
        <advanced>YourCompany_YourModuleName_CatalogSearch_Advanced</advanced> 
       </rewrite> 
      </catalogsearch> 
      <catalogsearch_mysql4> 
       <rewrite> 
        <fulltext_collection>YourCompany_YourModuleName_Model_CatalogSearch_Mysql4_Fulltext_Collection</fulltext_collection> 
        <advanced_collection>YourCompany_YourModuleName_Model_CatalogSearch_Mysql4_Advanced_Collection</advanced_collection> 
       </rewrite> 
      </catalogsearch_mysql4>  
     </models> 
    </global> 
</config> 

現在你可能已經注意到,我們在4類覆蓋4個功能。

第3步:用 後續內容創建我們的模塊中的4個新的文件:

  1. '/app/code/local/YourCompany/YourModuleName/Model/CatalogSearch/Advanced.php'

    class YourCompany_YourModuleName_Model_CatalogSearch_Advanced extends Mage_CatalogSearch_Model_Advanced 
    { 
        /** 
        * Retrieve advanced search product collection 
        * 
        * @return Mage_CatalogSearch_Model_Mysql4_Advanced_Collection 
        */ 
        public function getProductCollection(){ 
         if (is_null($this->_productCollection)) { 
          $this->_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection') 
           ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes()) 
           ->addMinimalPrice() 
           ->addTaxPercents() 
           ->addStoreFilter(); 
    
          $this->_productCollection->getSelect()->joinLeft(
           array('_inventory_table'=>'cataloginventory_stock_item'), 
           "_inventory_table.product_id = e.entity_id", 
           array('is_in_stock', 'manage_stock') 
          ); 
    
          $this->_productCollection->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()); 
    
          Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection); 
          Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($this->_productCollection); 
         } 
         return $this->_productCollection; 
        } 
    } 
    
  2. /應用程序/代碼/本地/ YourCompany/YourModuleName /型號/ CatalogSearch /層。PHP '

    class YourCompany_YourModuleName_Model_CatalogSearch_Layer extends Mage_CatalogSearch_Model_Layer 
    { 
        public function prepareProductCollection($collection) 
        { 
         $collection->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes()) 
          ->addSearchFilter(Mage::helper('catalogsearch')->getQuery()->getQueryText()) 
          ->setStore(Mage::app()->getStore()) 
          ->addMinimalPrice() 
          ->addFinalPrice() 
          ->addTaxPercents() 
          ->addStoreFilter() 
          ->addUrlRewrite(); 
         $collection->getSelect()->joinLeft(
           array('_inventory_table'=>'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()); 
         Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection); 
         Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection); 
         return $this; 
        } 
    } 
    
  3. /app/code/local/YourCompany/YourModuleName/Model/CatalogSearch/Mysql4/Advanced/Collection.php'

    class YourCompany_YourModuleName_Model_CatalogSearch_Mysql4_Advanced_Collection extends 
        Mage_CatalogSearch_Model_Mysql4_Advanced_Collection 
    { 
        public function setOrder($attribute, $dir='desc') 
        { 
        $this->addAttributeToSort('on_top', 'desc'); 
         parent::setOrder($attribute, $dir); 
         return $this; 
        } 
    } 
    
  4. 「/應用程序/代碼/本地/ YourCompany /YourModuleName/Model/CatalogSearch/Mysql4/Fulltext/Collection.php」

    class YourCompany_YourModuleName_Model_CatalogSearch_Mysql4_Fulltext_Collection 
        extends Mage_CatalogSearch_Model_Mysql4_Fulltext_Collection 
    { 
        public function setOrder($attribute, $dir = 'desc') 
        { 
         if ($attribute == 'relevance') { 
          $this->getSelect()->order("on_top DESC")->order("relevance {$dir}"); 
         } 
         else { 
          parent::setOrder('on_top', 'DESC'); 
          parent::setOrder($attribute, $dir); 
         } 
         return $this; 
        } 
    } 
    

就是這樣。您如何看待我們應該連接到'cataloginventory_stock_item'表,同時獲取搜索數據以檢測哪些產品缺貨併爲產品添加附加訂單。
沒什麼特別的,但我遇到了問題,我應該怎麼做才能實現我的目標。

p.s.如果有人能提供更好的方式來做到這一點 - 不客氣。然而,我找不到合適的教程來做到這一點。

+0

上述代碼僅影響搜索結果頁面,還是導航菜單中的類別頁面也受到缺貨產品移動到列表末尾的影響。 – SarthakGupta

+0

此解決方案已實施到可用的擴展:https://github.com/r-martins/Magento-OutOfStockLast *但*在Magento 1.9中它打破了搜索。 @lexa:你會有解決的辦法嗎? –

0

本主題可以幫助你: http://www.magentocommerce.com/boards/viewthread/31680/

然而,這是一個非常糟糕的主意修改核心文件,或在覈心目錄中添加自己的文件。我強烈建議你製作你自己的模塊,覆蓋現有的核心功能(你應該看到大量的在線教程)。這是一個很小的擴展,所以不是無法實現的。

+0

該解決方案建議修改模板,而不是核心類。如果從基本軟件包複製到您自己的定製軟件包/主題中,那實際上很好。 –

+0

是的,我知道我不應該修改核心數據並且必須覆蓋它。但是現在我不明白我應該重寫哪個類/函數,並試圖在覈心文件中找到它。但是,您的和其他鏈接在目錄中移除缺貨產品,但我需要在搜索結果列表中執行此操作。我想我應該在從數據庫獲取數據時進行排序,但是它會發生什麼?我使用「loadByQueryText」函數發現數據在Mage_CatalogSearch_Model_Mysql4_Query中獲得了表單數據庫。但它只是從catalogsearch_query獲取數據。但是,產品列表中的數據在哪裏獲取? – lexa

相關問題