2011-07-03 19 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

相关问题