Skip to content

[FEAT] Redirect single search result to product page #4697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions app/code/core/Mage/Catalog/Helper/Search.php

This file was deleted.

19 changes: 0 additions & 19 deletions app/code/core/Mage/Catalog/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -711,22 +711,6 @@
</catalog>
</observers>
</sales_convert_quote_item_to_order_item>
<controller_action_predispatch_catalogsearch_advanced_index>
<observers>
<disable_advanced_search>
<class>catalog/observer_disableAdvancedSearch</class>
<method>execute</method>
</disable_advanced_search>
</observers>
</controller_action_predispatch_catalogsearch_advanced_index>
<controller_action_predispatch_catalogsearch_advanced_result>
<observers>
<disable_advanced_search>
<class>catalog/observer_disableAdvancedSearch</class>
<method>execute</method>
</disable_advanced_search>
</observers>
</controller_action_predispatch_catalogsearch_advanced_result>
</events>
<translate>
<modules>
Expand Down Expand Up @@ -837,9 +821,6 @@
<interval_division_limit>9</interval_division_limit>
<display_product_count>1</display_product_count>
</layered_navigation>
<search>
<enable_advanced_search>1</enable_advanced_search>
</search>
</catalog>
<system>
<media_storage_configuration>
Expand Down
14 changes: 0 additions & 14 deletions app/code/core/Mage/Catalog/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -474,20 +474,6 @@
</fields>
</layer>
-->

<search>
<fields>
<enable_advanced_search translate="label">
<label>Enable Advanced Search</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_enabledisable</source_model>
<sort_order>800</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</enable_advanced_search>
</fields>
</search>
</groups>
</catalog>
<design>
Expand Down
24 changes: 24 additions & 0 deletions app/code/core/Mage/CatalogSearch/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,28 @@ public function getEngine()

return $this->_engine;
}

/**
* @SuppressWarnings("PHPMD.StaticAccess")
*/
public function isNotEnabled(): bool
{
return !Mage::getStoreConfigFlag('catalog/advanced_search/enabled');
}

/**
* @SuppressWarnings("PHPMD.StaticAccess")
*/
public function isRedirectSingleSearchResult(): bool
{
return Mage::getStoreConfigFlag('catalog/search/redirect_to_single_search_result');
}

/**
* @SuppressWarnings("PHPMD.StaticAccess")
*/
public function getNoRoutePath(): string
{
return $this->_getUrl(Mage::getStoreConfig('web/default/cms_no_route'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @category Mage
* @package Mage_Catalog
*/
class Mage_Catalog_Model_Observer_DisableAdvancedSearch
class Mage_CatalogSearch_Model_Observer_DisableAdvancedSearch implements Mage_Core_Observer_Interface
{
/**
* Disable Advanced Search at storeview scope
Expand All @@ -29,8 +29,7 @@ class Mage_Catalog_Model_Observer_DisableAdvancedSearch
*/
public function execute(Varien_Event_Observer $observer): void
{
/** @var Mage_Catalog_Helper_Search $helper */
$helper = Mage::helper('catalog/search');
$helper = Mage::helper('catalogsearch');
if ($helper->isNotEnabled()) {
$observer->getControllerAction()->getResponse()->setRedirect($helper->getNoRoutePath());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* OpenMage
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available at https://opensource.org/license/osl-3-0-php
*
* @category Mage
* @package Mage_Catalog
* @copyright Copyright (c) 2006-2020 Magento, Inc. (https://www.magento.com)
* @copyright Copyright (c) 2019-2024 The OpenMage Contributors (https://www.openmage.org)
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* Catalog Observer
*
* @category Mage
* @package Mage_Catalog
*/
class Mage_CatalogSearch_Model_Observer_SingleSearchResult implements Mage_Core_Observer_Interface
{
/**
* Product list block name in layout
*/
public const RESULT_BLOCK_NAME = 'search_result_list';

/**
* Redirect single search result to product page
*
* @SuppressWarnings("PHPMD.ExitExpression")
* @SuppressWarnings("PHPMD.StaticAccess")
*/
public function execute(Varien_Event_Observer $observer): void
{
$helper = Mage::helper('catalogsearch');

if (!$helper->isRedirectSingleSearchResult()) {
return;
}

/** @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)->sendResponse();
exit; //stop everything else
}
}
}
}
}
34 changes: 34 additions & 0 deletions app/code/core/Mage/CatalogSearch/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,36 @@
</catalogsearch>
</updates>
</layout>
<events>
<controller_action_predispatch_catalogsearch_advanced_index>
<observers>
<disable_advanced_search>
<class>catalogsearch/observer_disableAdvancedSearch</class>
</disable_advanced_search>
</observers>
</controller_action_predispatch_catalogsearch_advanced_index>
<controller_action_predispatch_catalogsearch_advanced_result>
<observers>
<disable_advanced_search>
<class>catalogsearch/observer_disableAdvancedSearch</class>
</disable_advanced_search>
</observers>
</controller_action_predispatch_catalogsearch_advanced_result>
<controller_action_layout_render_before_catalogsearch_result_index>
<observers>
<single_search_result>
<model>catalogsearch/observer_singleSearchResult</model>
</single_search_result>
</observers>
</controller_action_layout_render_before_catalogsearch_result_index>
<controller_action_layout_render_before_catalogsearch_advanced_result>
<observers>
<single_search_result>
<model>catalogsearch/observer_singleSearchResult</model>
</single_search_result>
</observers>
</controller_action_layout_render_before_catalogsearch_advanced_result>
</events>
</frontend>
<adminhtml>
<translate>
Expand Down Expand Up @@ -129,7 +159,11 @@
<search_separator>OR</search_separator>
<use_layered_navigation_count>2000</use_layered_navigation_count>
<show_autocomplete_results_count>1</show_autocomplete_results_count>
<redirect_to_single_search_result>1</redirect_to_single_search_result>
</search>
<advanced_search>
<enabled>1</enabled>
</advanced_search>
</catalog>
</default>
</config>
20 changes: 20 additions & 0 deletions app/code/core/Mage/CatalogSearch/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,28 @@
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</show_autocomplete_results_count>
<redirect_to_single_search_result translate="label">
<label>Redirect single search result to product</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_enabledisable</source_model>
<sort_order>80</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</redirect_to_single_search_result>
</fields>
</search>
<advanced_search>
<enabled translate="label">
<label>Enable Advanced Search</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_enabledisable</source_model>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</enabled>
</advanced_search>
</groups>
</catalog>
</sections>
Expand Down
12 changes: 10 additions & 2 deletions app/code/core/Mage/Core/Model/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -1413,15 +1413,23 @@ public function dispatchEvent($eventName, $args)
break;
case 'object':
case 'model':
$method = $obs['method'];
$observer->addData($args);
$object = Mage::getModel($obs['model']);
if ($object instanceof Mage_Core_Observer_Interface) {
$method = 'execute';
} else {
$method = $obs['method'];
}
$this->_callObserverMethod($object, $method, $observer, $obsName);
break;
default:
$method = $obs['method'];
$observer->addData($args);
$object = Mage::getSingleton($obs['model']);
if ($object instanceof Mage_Core_Observer_Interface) {
$method = 'execute';
} else {
$method = $obs['method'];
}
$this->_callObserverMethod($object, $method, $observer, $obsName);
break;
}
Expand Down
26 changes: 26 additions & 0 deletions app/code/core/Mage/Core/Observer/Interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* OpenMage
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available at https://opensource.org/license/osl-3-0-php
*
* @category Mage
* @package Mage_Catalog
* @copyright Copyright (c) 2006-2020 Magento, Inc. (https://www.magento.com)
* @copyright Copyright (c) 2019-2024 The OpenMage Contributors (https://www.openmage.org)
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* Observer interface
*
* @category Mage
* @package Mage_Core
*/
interface Mage_Core_Observer_Interface
{
public function execute(Varien_Event_Observer $observer): void;
}
1 change: 0 additions & 1 deletion app/locale/en_US/Mage_Catalog.csv
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@
"Email","Email"
"Email to a Friend","Email to a Friend"
"Empty","Empty"
"Enable Advanced Search","Enable Advanced Search"
"Enable MAP","Enable MAP"
"Enable Qty Increments","Enable Qty Increments"
"Enable WYSIWYG","Enable WYSIWYG"
Expand Down
2 changes: 2 additions & 0 deletions app/locale/en_US/Mage_CatalogSearch.csv
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"Catalog Search","Catalog Search"
"Catalog Search Index","Catalog Search Index"
"Don't see what you're looking for?","Don't see what you're looking for?"
"Enable Advanced Search","Enable Advanced Search"
"Enter ""0"" to enable layered navigation for any number of results.","Enter ""0"" to enable layered navigation for any number of results."
"Go","Go"
"Go to Home Page","Go to Home Page"
Expand All @@ -31,6 +32,7 @@
"Popular Search Terms","Popular Search Terms"
"Quick Search Form","Quick Search Form"
"Rebuild Catalog product fulltext search index","Rebuild Catalog product fulltext search index"
"Redirect single search result to product","Redirect single search result to product"
"Relevance","Relevance"
"Results","Results"
"Search","Search"
Expand Down
Loading