0

i need to change the url patter in advance search in magento2 like : catalogsearch/advanced/result/name/simple/ in default is gives the as query string but i need to change this.

how can i do this ?

can any one suggest the better solution for this ?

thanks in advance for your suggestion.

1 Answer 1

0

Hello i have spent a lot of time on this and figure out a solution for this.please see the below changes for this : 1) you need to change the method from get to post in form.phtml by ovverride that phtml file.

2) you need to ovverride the Magento\CatalogSearch\Controller\Advanced\Result.php file as below code :

use Magento\CatalogSearch\Model\Advanced as ModelAdvanced; use Magento\Framework\App\Action\Context; use Magento\Framework\UrlFactory; use Magento\Framework\UrlInterface; use Magento\Framework\Controller\ResultFactory;

class Result extends \Magento\Framework\App\Action\Action { /** * Url factory * * @var UrlFactory */ protected $_urlFactory;

/**
 * Catalog search advanced
 *
 * @var ModelAdvanced
 */
protected $_catalogSearchAdvanced;

/**
 * Construct
 *
 * @param Context $context
 * @param ModelAdvanced $catalogSearchAdvanced
 * @param UrlFactory $urlFactory
 */
public function __construct(
    Context $context,
    ModelAdvanced $catalogSearchAdvanced,
    UrlFactory $urlFactory,
    UrlInterface $urlBuilder
) {
    parent::__construct($context);
    $this->_catalogSearchAdvanced = $catalogSearchAdvanced;
    $this->_urlFactory = $urlFactory;
    $this->_urlBuilder = $urlBuilder;
}

/**
 * @return \Magento\Framework\Controller\Result\Redirect
 */
public function execute()
{
    try {
        $post = $this->getRequest()->getPostValue();
        if(!empty($post)){
            $newurl = $this->_urlBuilder->getUrl('catalogsearch/advanced/result1/',['name' => $post['name'],'sku'=>$post['sku']]);
            $params = ['name' => $post['name'],'sku'=>$post['sku']];
            $resultRedirect = $this->resultRedirectFactory->create();
            header('Location:'.$newurl);
            $resultRedirect->setPath($this->_redirect->redirect($newurl,$params));    

            return $resultRedirect;
        }
    } catch (\Magento\Framework\Exception\LocalizedException $e) {
        $this->messageManager->addError($e->getMessage());
        $defaultUrl = $this->_urlFactory->create()
            ->addQueryParams($this->getRequest()->getQueryValue())
            ->getUrl('*/*/');
        $resultRedirect = $this->resultRedirectFactory->create();
        $resultRedirect->setUrl($this->_redirect->error($defaultUrl));
        return $resultRedirect;
    }
}

}

3) you need to create a new route for as i create result1 in vendor itself and you need to put the code below :

use Magento\CatalogSearch\Model\Advanced as ModelAdvanced; use Magento\Framework\App\Action\Context; use Magento\Framework\UrlFactory; use Magento\Framework\UrlInterface; use Magento\Framework\Controller\ResultFactory;

class Result1 extends \Magento\Framework\App\Action\Action { /** * Url factory * * @var UrlFactory */ protected $_urlFactory;

/**
 * Catalog search advanced
 *
 * @var ModelAdvanced
 */
protected $_catalogSearchAdvanced;

/**
 * Construct
 *
 * @param Context $context
 * @param ModelAdvanced $catalogSearchAdvanced
 * @param UrlFactory $urlFactory
 */
public function __construct(
    Context $context,
    ModelAdvanced $catalogSearchAdvanced,
    UrlFactory $urlFactory,
    UrlInterface $urlBuilder
) {
    parent::__construct($context);
    $this->_catalogSearchAdvanced = $catalogSearchAdvanced;
    $this->_urlFactory = $urlFactory;
    $this->_urlBuilder = $urlBuilder;
}

/**
 * @return \Magento\Framework\Controller\Result\Redirect
 */
public function execute()
{
    try {
        $params = $this->_request->getParams();
        $this->_catalogSearchAdvanced->addFilters($params);//$this->getRequest()->getQueryValue()
        $this->_view->loadLayout();
        $this->_view->renderLayout();
    } catch (\Magento\Framework\Exception\LocalizedException $e) {
        $this->messageManager->addError($e->getMessage());
        $defaultUrl = $this->_urlFactory->create()
            ->addQueryParams($this->getRequest()->getQueryValue())
            ->getUrl('*/*/');
        $resultRedirect = $this->resultRedirectFactory->create();
        $resultRedirect->setUrl($this->_redirect->error($defaultUrl));
        return $resultRedirect;
    }
}

}

4) you need to create a same copy of the catalogsearch_advanced_result.xml to create catalogsearch_advanced_result1.xml

and here you will see the pretty url in browser as what ever you want .

enter image description here

see the screenshot i attach of my result you are able to see the url in that capture image

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.