TransWikia.com

How to do use a different theme on search result page instead of default?

Magento Asked by joe young on November 20, 2021

How do I make the search results page use a different theme like you can on category pages?

As, I’m using a different theme for a catalog on my store, but when you use the mini search it pulls the default store theme.

For example,

Design Section of catalog

One Answer

You can assign custom theme using Event/Observer for Search result page

Create events.xml at app/code/{VendorName}/{Modulename}/etc/frontend/ and call an observer on layout_load_before event.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="layout_load_before">
        <observer name="ForcetoChangeTheme" 
            instance="{VendorName}{Modulename}ObserverChangeTheme" />
    </event>
</config>

On the observer ChangeTheme.php at {VendorName}{Modulename}Observer change the theme programmatically.

<?php
namespace DevamitberaChangethemeAtCheckoutObserver;

use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestDataPersistorInterface;
use MagentoFrameworkAppObjectManager;

class ChangeTheme implements ObserverInterface
{
     /**
     * Design package instance
     *
     * @var MagentoFrameworkViewDesignInterface
     */
    private $_design;
    /**
     * Store manager
     *
     * @var MagentoStoreModelStoreManagerInterface
     */
    private $_storeManager;
    private $request;
    private $logger;

    public function __construct(
        MagentoFrameworkViewDesignInterface $design,
        MagentoStoreModelStoreManagerInterface $storeManager,   
        MagentoFrameworkAppRequestHttp $request,
        PsrLogLoggerInterface $logger
    ) {
        $this->_design = $design;
        $this->_storeManager = $storeManager;
        $this->request = $request;
        $this->logger = $logger;
        }

    /**
     *
     * @param MagentoFrameworkEventObserver $observer
     * @return void
     */
    public function execute(MagentoFrameworkEventObserver $observer)
    {
        $controller = $observer->getControllerAction();
        //$this->logger->info($this->request->getFullActionName());
        if($this->request->getFullActionName() =='catalogsearch_result_index'){
            $this->_design->setDesignTheme(1); //  1 is Theme id
        }
    }

}

Answered by Amit Bera on November 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP