0

I want to override Catalog Product Mass Delete file in Magento2

vendor/magento/module-catalog/Controller/Adminhtml/Product/MassDelete.php

My Code:

di.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
            <preference for="Magento\Catalog\Controller\Adminhtml\Product\MassDelete" type="Amy\AdminProductDeletion\Controller\Adminhtml\Catalog\Product\MassDelete" />
</config>

Controller file:

app/code/Amy/AdminProductDeletion/Controller/Adminhtml/Catalog/Product/MassDelete.php

<?php
/**
 *
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Amy\AdminProductDeletion\Controller\Adminhtml\Catalog\Product;

use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Mail\Template\TransportBuilder;


class MassDelete extends \Magento\Catalog\Controller\Adminhtml\Product
{
    /**
     * Massactions filter
     *
     * @var Filter
     */
    protected $filter;

    /**
     * @var CollectionFactory
     */
    protected $collectionFactory;

    /**
     * @var ProductRepositoryInterface
     */
    private $productRepository;


    protected $transportBuilder;


    /**
     * @param Context $context
     * @param Builder $productBuilder
     * @param Filter $filter
     * @param CollectionFactory $collectionFactory
     * @param ProductRepositoryInterface $productRepository
     */
    public function __construct(
        Context $context,
        Builder $productBuilder,
        Filter $filter,
        TransportBuilder $transportBuilder,
        CollectionFactory $collectionFactory,
        ProductRepositoryInterface $productRepository = null
    ) {
        $this->filter = $filter;
        $this->collectionFactory = $collectionFactory;
        $this->transportBuilder = $transportBuilder;
        $this->productRepository = $productRepository
            ?: \Magento\Framework\App\ObjectManager::getInstance()->create(ProductRepositoryInterface::class);
        parent::__construct($context, $productBuilder);
    }

    /**
     * @return \Magento\Backend\Model\View\Result\Redirect
     */
    public function execute()
    {
        $collection = $this->filter->getCollection($this->collectionFactory->create());
        // echo "Amy";
        // die;
        $productDeleted = 0;
        /** @var \Magento\Catalog\Model\Product $product */
        foreach ($collection->getItems() as $product) {

            $productid = $product->getId();
            $productsku = $product->getSku();
          //  $productname = $product->getName();
            print_r($productsku);
            print_r($productid);
            //print_r($productname);
            //die;


            $objectManager      = \Magento\Framework\App\ObjectManager::getInstance();  
            $sellerId = $objectManager->create(
                'Webkul\Marketplace\Model\Product'
            )->getCollection()
            ->addFieldToFilter(
                  'mageproduct_id',
                ['eq' => $productid]
            )
            ->addFieldToFilter(
                'status',
                ['eq' => 1]
            )
            ->addFieldToSelect('seller_id');
            $sellerIdFilter =  $sellerId->getdata();

            echo "<pre>";
            print_r($sellerIdFilter);
            echo "</pre>";
            //die;



            $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
            $customerModel = $objectManager->create('Magento\Customer\Model\Customer')->load($sellerIdFilter);
            $customerEmail = $customerModel->getEmail();
            $customerfirstName = $customerModel->getFirstname();
            $customerlastName = $customerModel->getLastname();
            print_r($customerEmail);
            print_r($customerfirstName);
            print_r($customerlastName);
            //die;



            $consumer_email = "sample@gmail.com";
            $requestData['mail_subj'] = "Product deleted by Admin";
            $requestData['hello_txt'] = "Hello " . $customerfirstName;
            $requestData['prodid'] = $productid;
            $requestData['prodsku'] = $productsku;
            //$requestData['prodname'] = $prodname;
            $requestData['message'] = "This is to inform you that your Product has been Deleted by Admin.";
            $postObj = new \Magento\Framework\DataObject();
            $postObj->setData($requestData);

            $transport = $this->transportBuilder
                ->setTemplateIdentifier('delete_product')
                ->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID])
                ->setTemplateVars(['data' => $postObj])
                ->setFrom(['name' => "Product Deletion", 'email' => $consumer_email])
                ->addTo($customerEmail)
                ->getTransport();

            $transport->sendMessage();


            $this->productRepository->delete($product);
            $productDeleted++;
        }

        if ($productDeleted) {
            $this->messageManager->addSuccessMessage(
                __('A total of %1 record(s) have been deleted.', $productDeleted)
            );
        }

        return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('catalog/*/index');
    }
}

Any help would be appreciated.

  • try to move di.xml file in to vendor/module/etc/adminhtml – Rk Rathod Mar 4 at 12:38
  • okay will move it. – Amy Mar 4 at 12:44
  • I am getting error while compiling – Amy Mar 4 at 12:48
  • which error ??? – Rk Rathod Mar 4 at 12:53
  • Class Amy\AdminProductDeletion\Controller\Adminhtml\Catalog\Product\Builder does not exist Class Amy\AdminProductDeletion\Controller\Adminhtml\Catalog\Product\MassDelete\Interceptor generation error: – Amy Mar 4 at 12:53
0

Using Preference :-

Vendor/Module/etc/adminhtml/di.xml

<preference for="Magento\Catalog\Controller\Adminhtml\Product\MassDelete" type="Amy\AdminProductDeletion\Controller\Adminhtml\Product\MassDelete" />

After This Follow this Steps :-

rm -rf pub/static/frontend/ pub/static/adminhtml/ pub/static/_requirejs pub/static/deployed_version.txt var/cache var/page_cache var/generation var/view_preprocessed var/session generated/code

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy en_US -f

php bin/magento cache:flush

chmod -R 0777 var/ pub/static generated/
| improve this answer | |
  • I have tried this. It is not working. – Amy Mar 4 at 12:28
  • attached your code – Rk Rathod Mar 4 at 12:29
  • Attached my code, could you please check it. – Amy Mar 4 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.