我做到了。我想與你分享我的方法。也許這對於已經與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:你會有解決的辦法嗎? –