Question

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

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top