0

I created a new store view.

I have a category called "T-Shirts".

If I load that category e.g. www.new-store-view.example.com/shirts then it loads the search site showing results for "shirts" instead of loading the category site.

I already tried php bin/magento indexer:reindex

What can I do?

1 Answer 1

0

This is 2 way to implement:

  1. Create the observer to the event controller_action_predispatch_catalog_category_view and handle it like

    <?php
    declare(strict_types=1);
    
    namespace Acme\StackExchange\Observer\Frontend\Catalog;
    
    use Magento\Catalog\Api\CategoryRepositoryInterface;
    use Magento\Framework\App\ActionFlag;
    use Magento\Framework\App\ActionInterface;
    use Magento\Framework\App\Request\Http as RequestHttp;
    use Magento\Framework\Event\Observer;
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\Exception\NoSuchEntityException;
    use Magento\Framework\UrlInterface;
    use Magento\Store\Model\StoreManagerInterface;
    
    class CategoryViewPreDispatch implements ObserverInterface
    {
        protected ActionFlag                  $actionFlag;
        protected StoreManagerInterface       $storeManager;
        protected UrlInterface                $urlBuilder;
        protected CategoryRepositoryInterface $categoryRepository;
    
        public function __construct(
            ActionFlag $actionFlag,
            StoreManagerInterface $storeManager,
            UrlInterface $urlBuilder,
            CategoryRepositoryInterface $categoryRepository
        ) {
            $this->actionFlag         = $actionFlag;
            $this->storeManager       = $storeManager;
            $this->urlBuilder         = $urlBuilder;
            $this->categoryRepository = $categoryRepository;
        }
    
        /**
         * @inheritDoc
         */
        public function execute(Observer $observer)
        {
            return;
    
            /** @var RequestHttp $request */
            $request = $observer->getEvent()->getData('request');
            /** @var ActionInterface $action */
            $action = $observer->getEvent()->getData('controller_action');
    
            $categoryId = $request->getParam('id', false);
            if (!$categoryId) {
                return;
            }
    
            try {
                $category = $this->categoryRepository->get($categoryId, $this->storeManager->getStore()->getId());
            } catch (NoSuchEntityException $e) {
                return;
            }
    
            // any checks if needed like
            if (!$category->getIsActive()) {
                return;
            }
    
            $action->getResponse()->setRedirect(
                $this->urlBuilder->getUrl(
                    'catalogsearch/result/',
                    [
                        '_query' => [
                            'q' => $category->getName(),
                        ],
                    ]
                )
            );
            $this->actionFlag->set('', $action::FLAG_NO_DISPATCH, true);
            $this->actionFlag->set('', $action::FLAG_NO_POST_DISPATCH, true);
        }
    }
    
  2. Create the observer to the event catalog_controller_category_init_after and handle it like

    <?php
    declare(strict_types=1);
    
    namespace Acme\StackExchange\Observer\Frontend\Catalog;
    
    use Magento\Catalog\Model\Category;
    use Magento\Framework\App\ActionInterface;
    use Magento\Framework\Event\Observer;
    use Magento\Framework\Exception\LocalizedException;
    use Magento\Framework\UrlInterface;
    
    class CategoryControllerInitAfter implements \Magento\Framework\Event\ObserverInterface
    {
        protected UrlInterface $urlBuilder;
    
        public function __construct(UrlInterface $urlBuilder)
        {
            $this->urlBuilder = $urlBuilder;
        }
    
    
        /**
         * @inheritDoc
         */
        public function execute(Observer $observer)
        {
            /** @var Category $category */
            $category = $observer->getEvent()->getData('category');
            /** @var ActionInterface $action */
            $action = $observer->getEvent()->getData('controller_action');
    
            // any checks if needed like
            if (!$category->getIsActive()) {
                return;
            }
    
            $action->getResponse()->setRedirect(
                $this->urlBuilder->getUrl(
                    'catalogsearch/result/',
                    [
                        '_query' => [
                            'q' => $category->getName(),
                        ],
                    ]
                )
            );
    
            // this message will be logged
            throw new LocalizedException(__('Redirect to search result'));
        }
    }
    

    NOTE: The exception message will be logged in log

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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