0

i want to redirect from observer to the default magento product url from observer.

public function __construct(
            \Magento\Framework\App\ResponseFactory $responseFactory
        ) {

              $this->_responseFactory = $responseFactory;
        }

        public function execute(Observer $observer) {

                $productUrl = $currentProduct->getProductUrl(); 
                //$RedirectUrl= $this->_url->getUrl('chennaievent\index\index');
                return $this->_responseFactory->create()->setRedirect($productUrl)->sendResponse();

            }


        }

2 Answers 2

0

Try below code.

<?php
namespace Company\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Controller\ResultFactory;

class Test implements ObserverInterface
{
    protected $resultRedirect;

    public function __construct(
        \Magento\Framework\App\ResponseFactory $responseFactory,
        ResultFactory $result
    ) {
        $this->_responseFactory = $responseFactory;
        $this->resultRedirect = $result;
    }

    public function execute(Observer $observer) 
    {
        $resultRedirect = $this->resultRedirect->create(ResultFactory::TYPE_REDIRECT);
        //$resultRedirect->setUrl($this->_redirect->getRefererUrl());
        $productUrl = $currentProduct->getProductUrl(); 
        $resultRedirect->setUrl($productUrl);
        return $resultRedirect;
    }
}
2
0

I am not sure what exactly you are looking for but as per magecomp url you have given, you may apply following solution.

File: etc\frontend\di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Controller\Category\View">
        <plugin name="redirect_single_category_product" type="[Vendor]\[Module]\Plugin\CategoryView" sortOrder="10" />
    </type>
</config>

File: Plugin\CategoryView.php

<?php

namespace [Vendor]\[Module]\Plugin;

class CategoryView
{
    public function __construct(
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\App\ResponseInterface $response,
        \Magento\Framework\App\ActionFactory $actionFactory,
        \Magento\Catalog\Model\ProductRepository $productRepository
    )
    {
        $this->request = $request;
        $this->storeManager = $storeManager;
        $this->_response = $response;
        $this->actionFactory = $actionFactory;
        $this->productRepository = $productRepository;
    }

    public function afterExecute(
        \Magento\Catalog\Controller\Category\View $subject,
        $result
    )
    {
        // replace your condition here
        if(1)
        {
            // for example take first product
            $productId = 1;
            $current_product = $this->productRepository->getById($productId, false, $this->storeManager->getStore()->getId());

            $this->_response->setRedirect($current_product->getProductUrl());
            $this->request->setDispatched(true);
            return $this->actionFactory->create('Magento\Framework\App\Action\Redirect');
        }

        return $result;
    }
}

This should work. Replace [Vendor], [Module] with your vendor and module name. Let me know if you are looking for something else or specific.

3
  • After applying this, if you goto any category page, it will redirect to product page with product id 1.
    – Yash Shah
    Commented Feb 6, 2019 at 10:25
  • ,no its not working means its not redirecting to the product details page Commented Feb 7, 2019 at 13:39
  • Put a die('here'); inside if(1) statement and see if it is executing it or not. If not then probably you need to flush cache. Let me know the output of it.
    – Yash Shah
    Commented Feb 7, 2019 at 13:52

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.