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.