Let’s initiate a discussion!!
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
<?php namespace Magemonkeys\Discontinue\Setup; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; 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( \Magento\Catalog\Model\Product::ENTITY, 'discontinue', [ 'group' => 'Discontinue Product Information', 'type' => 'int', 'backend' => '', 'frontend' => '', 'label' => 'Discontinue', 'input' => 'boolean', 'class' => '', 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean', 'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::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( \Magento\Catalog\Model\Product::ENTITY, 'alternate_product_url', [ 'group' => 'Discontinue Product Information', 'type' => 'text', 'backend' => '', 'frontend' => '', 'label' => 'Alternate Product URL', 'input' => 'text', 'class' => '', 'source' => '', 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::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( \Magento\Catalog\Model\Product::ENTITY, 'alternate_product_sku', [ 'group' => 'Discontinue Product Information', 'type' => 'text', 'backend' => '', 'frontend' => '', 'label' => 'Alternate Product SKU', 'input' => 'text', 'class' => '', 'source' => '', 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::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
1 2 3 4 5 6 |
<?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="Magemonkeys\Discontinue\Observer\Productsavebefore" /> </event> </config> |
Add Observer file and place the below code in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
<?php namespace Magemonkeys\Discontinue\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Framework\Url; use Magento\Framework\Exception\NoSuchEntityException; 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(\Magento\Framework\Event\Observer $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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php namespace Magemonkeys\Discontinue\Plugin\Magento\Catalog\Model; class Layer { public function aroundGetProductCollection( \Magento\Catalog\Model\Layer $subject, \Closure $proceed ) { $collection = $proceed(); $collection->addAttributeToSelect('*')->addAttributeToFilter(array( array( 'attribute' => 'discontinue', 'null' => true), array( 'attribute' => 'discontinue', 'eq' => '0') )); return $collection; } } |
The search box is the most underrated functionality that is...
Managing and customizing Magento site can be a daunting task,...
Having an in-house eCommerce developer is very costly, and it...
Have you wonder why your conversions are less compare to...
According to a research, around 68% of eCommerce stores are...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.