0

I have 5 categories with single products each. I also have 5 stores.

I want to redirect the user directly to the product page when he clicks on the category link in the main menu for each store.

Can this be done in magento 2.1 CE?

0

you can write an observer for the event controller_action_predispatch_catalog_category_view that checks if the current category has a single product.

Create this file in one of your extensions.

[Namespace]/[Module]/etc/frontend/events.xml

<?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="controller_action_predispatch_catalog_category_view">
        <observer name="[namespace]-[module]-category-view" instance="[Namespace]\[Module]\Observer\CategoryView" />
    </event>
</config>

then create the observer class:
[Namespace]/[Module]/Observer/CategoryView.php

<?php
namespace [Namespace]\[Module]\Observer;

class CategoryView implements \Magento\Framework\Event\ObserverInterface
{
    /**
     * @var \Magento\Catalog\Model\Layer\Resolver
     */
    protected $layerResolver;
    /**
     * @var \Magento\Framework\App\RequestInterface
     */
    protected $request;

    /**
     * @var \Magento\Catalog\Api\CategoryRepositoryInterface
     */
    protected $categoryRepository;

    /**
     * @var \Magento\Framework\App\ResponseInterface
     */
    protected $response;

    /**
     * @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
     * @param \Magento\Framework\App\RequestInterface $request
     * @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
     * @param \Magento\Framework\App\ResponseInterface $reponse
     */
    public function __construct(
        \Magento\Catalog\Model\Layer\Resolver $layerResolver,
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
        \Magento\Framework\App\ResponseInterface $reponse
    ) {
        $this->layerResolver = $layerResolver;
        $this->request = $request;
        $this->categoryRepository = $categoryRepository;
        $this->response = $reponse;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $categoryId = $this->request->getParam('id', false);
        if (!$categoryId) {
            return $this;
        }
        $layer = $this->layerResolver->get();
        $category = $this->categoryRepository->get($categoryId);
        $layer->setCurrentCategory($category);
        $collection = $layer->getProductCollection();
        if ($collection->count() == 1) {
            /** @var \Magento\Catalog\Model\Product $product */
            $product = $collection->getFirstItem();
            $this->response->setRedirect($product->getProductUrl());
            $this->response->sendResponse();
            exit;
        }
        return $this;
    }

}

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.