我做到了。我想与你分享我的方法。也许这对于已经与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个新的文件:
'/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;
}
}
/应用程序/代码/本地/ 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;
}
}
/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;
}
}
“/应用程序/代码/本地/ 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.如果有人能提供更好的方式来做到这一点 - 不客气。然而,我找不到合适的教程来做到这一点。
上述代码仅影响搜索结果页面,还是导航菜单中的类别页面也受到缺货产品移动到列表末尾的影响。 – SarthakGupta
此解决方案已实施到可用的扩展:https://github.com/r-martins/Magento-OutOfStockLast *但*在Magento 1.9中它打破了搜索。 @lexa:你会有解决的办法吗? –