0

I need to remove a product while reorder button clicked certain condition. For example if the product sku is 'ABCD' means, i need to remove it from adding cart and all other ordered product need to added in cart.

2 Answers 2

0

You have to work on Magento\Sales\Controller\AbstractController\Reorder:: execute() controller Action.

  <?php namespace 
 StackExchange\Magento\Plugin\Magento\Sales\Controller\AbstractController;

  use Magento\Framework\Registry;

  class Reorder {

/**
 * @var \Magento\Framework\Message\ManagerInterface
 */
private $messageManager;

/**
 * @var \Magento\Checkout\Model\Cart
 */
private $cart;

/**
 * @var \Magento\Framework\Controller\Result\RedirectFactory
 */
private $resultRedirectFactory;

/**
 * @var \Magento\Sales\Controller\AbstractController\OrderLoaderInterface
 */
private $orderLoader;

/**
 * @var Registry
 */
private $registry;

public function __construct(
  \Magento\Sales\Controller\AbstractController\OrderLoaderInterface  $orderLoader,
  \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
  \Magento\Checkout\Model\Cart $cart, 
   \Magento\Framework\Message\ManagerInterface $messageManager,  
   Registry $registry     
) {

    $this->registry = $registry;
    $this->orderLoader = $orderLoader;
    $this->resultRedirectFactory = $resultRedirectFactory;
    $this->cart = $cart;
    $this->messageManager = $messageManager;
}
public function aroundExecute(
 \Magento\Sales\Controller\AbstractController\Reorder $subject 
){
  $result = $this->orderLoader->load($subject->getRequest());
    if ($result instanceof \Magento\Framework\Controller\ResultInterface) {
        return $result;
    }
    $order = $this->registry->registry('current_order');
    $resultRedirect = $this->resultRedirectFactory->create();
    $cart = $this->cart;
    $items = $order->getItemsCollection();
    foreach ($items as $item) {

        try{
           $cart->addOrderItem($item) ;
        }catch (\Magento\Framework\Exception\LocalizedException $e) {  
            if ($this->cart->getUseNotice(true)) {
                $this->messageManager->addNoticeMessage($e->getMessage());
            } else {
                $this->messageManager->addErrorMessage($e->getMessage());
            }
            return $resultRedirect->setPath('*/*/history');
        } catch (\Exception $e) {
            $this->messageManager->addExceptionMessage(
                $e,
                __('We can\'t add this item to your shopping cart right now.')
            );
            return $resultRedirect->setPath('checkout/cart');
        }
    }

    $cart->save();
    return $resultRedirect->setPath('checkout/cart');
}
}
0

Generally magento doesn't provided specific event's for that actions, so you need to create around plugin like this.

  1. app\code\Vendor\Extension\etc\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">
      <type name="Magento\Sales\Controller\AbstractController\Reorder">
          <plugin name="reorder_execute_plugin" type="Vendor\Extension\Plugin\Reorderpluginfile" sortOrder="10" />
      </type>
    </config>
    
  2. Now create Reorderpluginfile.php on following locations.

    app\code\Vendor\Extension\Plugin\Reorderpluginfile.php
    

add code like this :

<?php
namespace Vendor\Extension\Plugin\;

use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\App\Action;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
use Magento\Sales\Helper\Reorder as ReorderHelper;

class Reorderpluginfile {
     protected $orderLoader;
     protected $_coreRegistry;
     private $reorder;
     private $checkoutSession;

     public function __construct(
    Action\Context $context,
    OrderLoaderInterface $orderLoader,
    Registry $registry,
    ReorderHelper $reorderHelper = null,
    \Magento\Sales\Model\Reorder\Reorder $reorder = null,
    CheckoutSession $checkoutSession = null) 
    {
        $this->orderLoader = $orderLoader;
        $this->_coreRegistry = $registry;
        parent::__construct($context);
        $this->reorder = $reorder ?: ObjectManager::getInstance()->get(\Magento\Sales\Model\Reorder\Reorder::class);
        $this->checkoutSession = $checkoutSession ?: ObjectManager::getInstance()->get(CheckoutSession::class);
    }

    public function aroundExecute(
 \Magento\Sales\Controller\AbstractController\Reorder $subject)
   {

      **HERE YOU NEED TO CUSTOMIZATION ACCORDING TO REQUIREMENTS**

      $result = $this->orderLoader->load($this->_request);
    if ($result instanceof \Magento\Framework\Controller\ResultInterface) {
        return $result;
    }
    $order = $this->_coreRegistry->registry('current_order');

    /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
    $resultRedirect = $this->resultRedirectFactory->create();

    try {
        $reorderOutput = $this->reorder->execute($order->getIncrementId(), $order->getStoreId());
    } catch (LocalizedException $localizedException) {
        $this->messageManager->addErrorMessage($localizedException->getMessage());
        return $resultRedirect->setPath('checkout/cart');
    }

    // Set quote id for guest session: \Magento\Quote\Api\CartRepositoryInterface::save doesn't set quote id
    // to session for guest customer, as it does \Magento\Checkout\Model\Cart::save which is deprecated.
    $this->checkoutSession->setQuoteId($reorderOutput->getCart()->getId());

    $errors = $reorderOutput->getErrors();
    if (!empty($errors)) {
        $useNotice = $this->_objectManager->get(\Magento\Checkout\Model\Session::class)->getUseNotice(true);
        foreach ($errors as $error) {
            $useNotice
                ? $this->messageManager->addNoticeMessage($error->getMessage())
                : $this->messageManager->addErrorMessage($error->getMessage());
        }
    }

    return $resultRedirect->setPath('checkout/cart');
   }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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