我是magento的新手,我需要你的帮助。 我需要将“缺货”产品移到搜索结果列表的末尾。我怎样才能做到这一点?
我发现模块catalogsearch
用于搜索。我还看到有一个表cataloginventory_stock_item
和is_in_stock
字段。这是检查产品是否缺货的正确标志。我想我应该在数据来自数据库时添加订单条件。但是我无法找到这些数据从哪个(文件)获取catalogsearch_query
和catalogsearch_result
表格以及我应该在哪里(以及如何)添加订单条件。你能举个例子吗?
答案 0 :(得分:1)
我做到了。我想和你分享我的方法。对于那些已经使用过magento的人来说,这可能很容易,但对新手来说却不是。
'CatalogSearch'模块负责搜索产品目录。模块的路径是'DOC_ROOT / app / code / core / Mage / CatalogSearch'。
首先,我们不应该编辑核心文件('Mage'文件夹中的文件),并且必须覆盖核心类和函数。 为了实现我们的目标(我的主题问题),我们应该在不同的类中编辑一些函数,因此我们需要覆盖这些函数。
Magento中有两种搜索思想产品目录:简单和高级(可能更多,但我不知道另一种),我们将对这两种类型产生影响。
第1步: 我们应该告诉Magento引擎,我们想要覆盖它的一些核心类。因此,我们创建自己的模块。转到'/ app / etc / modules /'并使用以下内容创建文件'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个新文件,其中包含以下内容:
'/应用程序/代码/本地/ YourCompany / YourModuleName /型号/ 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;
}
}
/app/code/local/YourCompany/YourModuleName/Model/CatalogSearch/Layer.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 /型号/ CatalogSearch / Mysql4 /全文/ 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。如果有人能提供更好的方法来做到这一点 - 欢迎你。但是我找不到合适的教程来做到这一点。
答案 1 :(得分:0)
此主题可以帮助您: http://www.magentocommerce.com/boards/viewthread/31680/
但是,修改核心文件或在核心目录中添加自己的文件是一个非常糟糕的主意。我强烈建议你制作自己的模块,覆盖现有的核心功能(你应该在网上看到大量的教程)。这是一个很小的延伸,所以并非不可能实现。