public function aroundExecute(
        \Magento\CatalogSearch\Controller\Result\Index $subject,
        \Closure $proceed
    ) {
        $result = $proceed();

        if (!$subject->getResponse()->isRedirect() && $this->scopeConfig->isSetFlag(self::REDIRECT_SETTINGS_CONFIG_XML_FLAG)) {
            $layer      = $this->layerResolver->get();
            $layerState = $layer->getState();

            if (count($layerState->getFilters()) === 0) {
                $productCollection = $layer->getProductCollection();
                if ($productCollection->getCurPage() === 1 && $productCollection->getSize() === 1) {
                    /** @var \Magento\Catalog\Api\Data\ProductInterface $product */
                    $product = $productCollection->getFirstItem();
                    if ($product->getId()) {
                        $this->addRedirectMessage($product);
                        $result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
                        $result->setUrl($product->getProductUrl());

                        $this->eventManager->dispatch(
                            'smile_elasticsuite_redirect_if_one_result',
                            [
                                'store_id'           => $product->getStoreId(),
                                'product_collection' => $productCollection,
                            ]
                        );
                    }
                }
            }
        }

        return $result;
    }
public function aroundExecute(
        \Magento\CatalogSearch\Controller\Result\Index $subject,
        \Closure $proceed
    ) {
        $result = $proceed();

        if (!$subject->getResponse()->isRedirect() && $this->scopeConfig->isSetFlag(self::REDIRECT_SETTINGS_CONFIG_XML_FLAG)) {
            $layer      = $this->layerResolver->get();
            $layerState = $layer->getState();

            if (count($layerState->getFilters()) === 0) {
                $productCollection = $layer->getProductCollection();
                if ($productCollection->getCurPage() === 1 && $productCollection->getSize() === 1) {
                    /** @var \Magento\Catalog\Api\Data\ProductInterface $product */
                    $product = $productCollection->getFirstItem();
                    if ($product->getId()) {
                        $this->addRedirectMessage($product);
                        $result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
                        $result->setUrl($product->getProductUrl());

                        $this->eventManager->dispatch(
                            'smile_elasticsuite_redirect_if_one_result',
                            [
                                'store_id'           => $product->getStoreId(),
                                'product_collection' => $productCollection,
                            ]
                        );
                    }
                }
            }
        }

        return $result;
    }
public function aroundExecute ( \ Magento \ CatalogSearch \ Controller \ Result \ Index $ subject , \ Closure $ proceed ) { $ result = $ proceed ( ) ; if ( ! $ subject -> getResponse ( ) -> isRedirect ( ) && $ this -> scopeConfig -> isSetFlag ( self :: REDIRECT_SETTINGS_CONFIG_XML_FLAG ) ) { $ layer = $ this -> layerResolver -> get ( ) ; $ layerState = $ layer -> getState ( ) ; if ( count ( $ layerState -> getFilters ( ) ) === 0 ) { $ productCollection = $ layer -> getProductCollection ( ) ; if ( $ productCollection -> getCurPage ( ) === 1 && $ productCollection -> getSize ( ) === 1 ) { /** @var \Magento\Catalog\Api\Data\ProductInterface $product */ $ product = $ productCollection -> getFirstItem ( ) ; if ( $ product -> getId ( ) ) { $ this -> addRedirectMessage ( $ product ) ; $ result = $ this -> resultFactory -> create ( ResultFactory :: TYPE_REDIRECT ) ; $ result -> setUrl ( $ product -> getProductUrl ( ) ) ; $ this -> eventManager -> dispatch ( 'smile_elasticsuite_redirect_if_one_result' , [ 'store_id' => $ product -> getStoreId ( ) , 'product_collection' => $ productCollection , ] ) ; } } } } return $ result ; }
Process a redirect to the product page if there is only one result for a given search.

@param \Magento\CatalogSearch\Controller\Result\Index $subject The CatalogSearch Result Controller
@param \Closure                                       $proceed The execute method

@return \Magento\Framework\Controller\ResultInterface
Process a redirect to the product page if there is only one result for a given search .