To achieve this, create a basic module and below code in InstallSchema.php file.
Change the vendor namespace and module name as per your requirement.
<?php
namespace MagemonkeysDiscontinueSetup;
use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
public function install(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
)
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
/**
* Add attributes to the eav/attribute
*/
$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
'discontinue',
[
'group' => 'Discontinue Product Information',
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Discontinue',
'input' => 'boolean',
'class' => '',
'source' => 'MagentoEavModelEntityAttributeSourceBoolean',
'global' => MagentoCatalogModelResourceModelEavAttribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false
]
);
/**
* Add attributes to the eav/attribute
*/
$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
'alternate_product_url',
[
'group' => 'Discontinue Product Information',
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'Alternate Product URL',
'input' => 'text',
'class' => '',
'source' => '',
'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => false,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);
$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
'alternate_product_sku',
[
'group' => 'Discontinue Product Information',
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'Alternate Product SKU',
'input' => 'text',
'class' => '',
'source' => '',
'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);
}
}
Create a Observer with catalog_product_save _before in adminhtml/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_product_save_before">
<observer name="discontinue_product_save_after" instance="MagemonkeysDiscontinueObserverProductsavebefore" />
</event>
</config>
Add Observer file and place the below code in it.
<?php
namespace MagemonkeysDiscontinueObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoCatalogApiProductRepositoryInterface;
use MagentoFrameworkUrl;
use MagentoFrameworkExceptionNoSuchEntityException;
class Productsavebefore implements ObserverInterface
{
/**
* @var ProductRepositoryInterface
*/
protected $productRepository;
protected $_url;
/**
* @param ProductRepositoryInterface $productRepository
*/
public function __construct(
ProductRepositoryInterface $productRepository,
Url $url
) {
$this->productRepository = $productRepository;
$this->_url = $url;
}
public function execute(MagentoFrameworkEventObserver $observer)
{
$_product = $observer->getProduct(); // you will get product object
$sku = $_product->getAlternateProductSku();
if($sku != '')
{
try
{
$alternateProduct = $this->productRepository->get($sku);
}
catch (NoSuchEntityException $noSuchEntityException) {
$productUrl = null;
}
if($alternateProduct)
{
$product = $this->productRepository->getById($alternateProduct->getId(), false);
//$productUrl = $product->setStoreId(1)->getUrlModel()->getUrlInStore($product, ['_escape' => true]);
$productUrl = $product->getUrlKey();
$_product->setData('alternate_product_url', $productUrl);
}
}
else
{
$productUrl = null;
$_product->setData('alternate_product_url', $productUrl);
}
}
}
Create a plugin with aroundGetProduyctCollection to remove the discontinue product from whole site. Please add the below code in that plugin.
<?php
namespace MagemonkeysDiscontinuePluginMagentoCatalogModel;
class Layer
{
public function aroundGetProductCollection(
MagentoCatalogModelLayer $subject,
Closure $proceed
) {
$collection = $proceed();
$collection->addAttributeToSelect('*')->addAttributeToFilter(array(
array(
'attribute' => 'discontinue',
'null' => true),
array(
'attribute' => 'discontinue',
'eq' => '0')
));
return $collection;
}
}

