4

Is there a way to link/redirect a product page to a category?

Scenario: I have 70-80 new products that all go into the same category. I can either chose to put one of the products in the "News" category, which will - of course - only show the customer that one product. Or, I can add all the products to the "News"-category, which will most likely make the customer bored before looking thru the entire list.

So, I'd like to create a product that says "Lots of new items from this-and-that manufacturer" and then have that product redirect/link to the category instead of a product page.

Have tried doing it with URL rewrites, but can't really get it working like I'd like it to. At all.

Am I missing something obvious, or is there no straight forward solution to this?

Edit: Magento 2.3.2

3
  • Please specify Magento version?
    – Amit Bera
    Commented Sep 23, 2019 at 7:06
  • 2.3.2 - sorry for leaving that out.
    – PepsiMax
    Commented Sep 23, 2019 at 8:05
  • @PepsiMax please check my answer, I hope it will help you. Commented Sep 23, 2019 at 8:28

1 Answer 1

4

You can do it in the following way.

Create the new product attribute set enable for redirect products. Now Create event using your custom module (NOTE: If you don't have any module then create new module and then create event in that)

So in your events.xml add following code

/app/code/Vendor/Module/etc/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="catalog_controller_product_init_after">
        <observer instance="Vendor\Module\Observer\Catalog\ProductLoadInit" name="vendor_observer_catalog_controller_product_init"/>
    </event>
</config>

Now add following code in your observer

/app/code/Vendor/Module/Observer/Catalog/ProductLoadInit.php

<?php

namespace Vendor\Module\Observer\Catalog;

class ProductLoadInit implements \Magento\Framework\Event\ObserverInterface
{

    protected $_productCollectionFactory;
    protected $resultPageFactory;
    private $url;

    public function __construct(
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
    ) {
        $this->_productCollectionFactory = $productCollectionFactory;
        $this->responseFactory = $responseFactory;
        $this->url = $url;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();

        $collection = $this->_productCollectionFactory->create();
        $collection = $collection->addFieldToFilter('product_id', $product->getId());
        $collection->addFieldToFilter('your_custom_attribute', 1);

        if(count($collection)>0){
            $pItem = $collection->getFirstItem();
            if($pItem->getCategoryPath()){
                $redirectionUrl = $this->url->getUrl($pItem->getCategoryPath());
                $this->responseFactory->create()->setRedirect($redirectionUrl)->sendResponse();
                exit();
            } else {
                return $this;
            }

        }
        return $this;
    }
}

Now using this code your product will redirect to their assigned category.

Please check and let me know in case of any issue.

4
  • 1
    Wonderful - will try that, thank you kindly!
    – PepsiMax
    Commented Sep 23, 2019 at 8:27
  • @PepsiMax did you get the solution? Commented Sep 24, 2019 at 8:06
  • Yes, thank you very much :-)
    – PepsiMax
    Commented Sep 25, 2019 at 15:16
  • cool, so please accept my ans and upvote it so it will help others. Thanks Commented Sep 25, 2019 at 16:23

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.