create a plugin to check the product status "afterGetStatus" method for class \Magento\Catalog\Model\Product, if the product status is disabled, then create a custom 404 page and forward the controller to that. Find below the code to do that, I have checked it and it is working.
di.xmldi.xml
<type name="Magento\Catalog\Model\Product">
<plugin name="change_product" type="Rahul\Disabledproductredirect\Plugin\Product" sortOrder="1" />
</type>
Plugin/Product.phpPlugin/Product.php
namespace Rahul\Disabledproductredirect\Plugin;
use Magento\Framework\Controller\Result\Forward; use Magento\Framework\App\Request\Http;
class Product {
private $forward; private $request;
public function __construct(Forward $forward,Http $request) {
use Magento\Framework\Controller\Result\Forward; use Magento\Framework\App\Request\Http;
class Product {
private $forward;
private $request;
public function __construct(Forward $forward,Http $request)
{
$this->forward = $forward;
$this->request = $request;
}
public function afterGetStatus(\Magento\Catalog\Model\Product $subject, $result) { //return $result; if($result == \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED && $this->request->getControllerName()=='product' && $this->request->getActionName() == 'view') {
}
public function afterGetStatus(\Magento\Catalog\Model\Product $subject, $result)
{
//return $result;
if($result == \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED && $this->request->getControllerName()=='product' && $this->request->getActionName() == 'view')
{
$this->forward->forward('noroute');
}
return $result;
}
}
} }