Anda perlu membuat ekstensi baru yang memeriksa, sebelum merender halaman pencarian cepat (atau pencarian lanjutan) jika koleksi produk mengandung tepat satu produk.
Untuk ini, mari kita buat ekstensi baru bernama StackExchange_CatalogSearch
.
Anda perlu file-file berikut:
app/etc/modules/StackExchange_CatalogSearch.xml
- file deklarasi
<?xml version="1.0"?>
<config>
<modules>
<StackExchange_CatalogSearch>
<codePool>local</codePool>
<active>true</active>
<depends>
<Mage_CatalogSearch />
</depends>
</StackExchange_CatalogSearch>
</modules>
</config>
app/code/local/StackExchange/CatalogSearch/etc/config.xml
- file konfigurasi:
<?xml version="1.0"?>
<config>
<modules>
<StackExchange_CatalogSearch>
<version>1.0.0</version>
</StackExchange_CatalogSearch>
</modules>
<global>
<models>
<stackexchange_catalogsearch>
<class>StackExchange_CatalogSearch_Model</class>
</stackexchange_catalogsearch>
</models>
</global>
<frontend>
<events>
<controller_action_layout_render_before_catalogsearch_result_index><!-- for the quick search-->
<observers>
<stackexchange_catalogsearch>
<model>stackexchange_catalogsearch/observer</model>
<method>redirectToProduct</method>
</stackexchange_catalogsearch>
</observers>
</controller_action_layout_render_before_catalogsearch_result_index>
<controller_action_layout_render_before_catalogsearch_advanced_result><!-- for the advanced search-->
<observers>
<stackexchange_catalogsearch>
<model>stackexchange_catalogsearch/observer</model>
<method>redirectToProduct</method>
</stackexchange_catalogsearch>
</observers>
</controller_action_layout_render_before_catalogsearch_advanced_result>
</events>
</frontend>
</config>
app/code/local/StackExchange/CatalogSearch/Model/Observer.php
- pengamat yang melakukan semua pekerjaan.
<?php
class StackExchange_CatalogSearch_Model_Observer
{
//the product list block name in layout
const RESULT_BLOCK_NAME = 'search_result_list';
public function redirectToProduct($observer)
{
/** @var Mage_Catalog_Block_Product_List $block */
$block = Mage::app()->getLayout()->getBlock(self::RESULT_BLOCK_NAME);
if ($block) {
$collection = $block->getLoadedProductCollection();
if ($collection && $collection->getSize() == 1) {
/** @var Mage_Catalog_Model_Product $product */
$product = $collection->getFirstItem();
$url = $product->getProductUrl();
if ($url){
Mage::app()->getResponse()->setRedirect($url);
Mage::app()->getResponse()->sendResponse();
exit; //stop everything else
}
}
}
}
}
Bersihkan cache, nonaktifkan kompilasi jika diaktifkan dan cobalah.
Catatan: Ekstensi ini mengalihkan ke halaman produk ketika halaman pencarian (dan pencarian lanjutan) hanya akan kembali pada produk, bahkan jika ini terjadi setelah pencarian atau setelah menerapkan filter navigasi berlapis.