Magento 2.3 Advanced search giving 404
-
22-02-2021 - |
Question
I would like to use the advanced search on my home page, it works fine if I click the advanced search link in the footer at the bottom. But if I add
{{block class="Magento\CatalogSearch\Block\Advanced\Form" template="Magento_CatalogSearch::advanced/form.phtml"}}
to my home page, the search results are all 404?
Solution
You cannot achieve your request like this way
You get the issue like 404 because of the form post URL of home page advanced search is : www.example.com/cms/index/result
which is a 404 page at Magento.
And form Post URL defined at getSearchPostUrl()
public function getSearchPostUrl()
{
return $this->getUrl('*/*/result');
}
So you have to change it to full URL
public function getSearchPostUrl()
{
return $this->getUrl('catalogsearch/advanced/result');
}
So, you have to create a plugin at on this method which change url to $this->getUrl('catalogsearch/advanced/result');
Create a custom module and define plugin at di.xml
app/code/{Vendor}/{Modulename}/etc/frontend
.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\CatalogSearch\Block\Advanced\Form">
<plugin name="change_post_url" sortOrder="1"
type="{Vendor}\{Modulename}\Plugin\FormPlugin" />
</type>
</config>
Plugin class located at FormPlugin.php
app/code/{Vendor}/{Modulename}/Plugin/
<?php
namespace {Vendor}\{Modulename}\Plugin;
class FormPlugin
{
public function afterGetSearchPostUrl(\Magento\CatalogSearch\Block\Advanced\Form $subject, $result)
{
return $subject->getUrl('catalogsearch/advanced/result');
}
}
Also, this module should have:
app/code/{Vendor}/{Modulename}/etc/module.xml
.app/code/{Vendor}/{Modulename}/composer.json
- .
app/code/{Vendor}/{Modulename}/registration.php
.
After do module enabled and setup upgrade, Di compile, Static content deploy After adding the event you should flush the cache.
The Module structure will like