0

I have to skip some items when the customer does Reorder. For this, I have overridden the controller Using Plugin.

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" type="MyModule\MyReorder\Plugin\Reorder" sortOrder="1" />
    </type>
</config>

Controller File

<?php

namespace MyModule\MyReorder\Plugin;

use Magento\Framework\Registry;

class Reorder
{
    /**
     * @var \Magento\Checkout\Model\Cart
     */
    private $cart;
    /**
     * @var \Magento\Sales\Controller\AbstractController\OrderLoaderInterface
     */
    private $orderLoader;
    /**
     * @var Registry
     */
    protected $_coreRegistry;

    public function __construct(
        \Magento\Sales\Controller\AbstractController\OrderLoaderInterface $orderLoader,
        \Magento\Checkout\Model\Cart $cart,
        Registry $registry
    ) {
        $this->_coreRegistry = $registry;
        $this->orderLoader = $orderLoader;
        $this->cart = $cart;
    }
    public function aroundExecute(
        \Magento\Sales\Controller\AbstractController\Reorder $subject,
        $result
    ){
    }
    public function afterExecute(
        \Magento\Sales\Controller\AbstractController\Reorder $subject,
        $result
    ) {
        $order = $this->_coreRegistry->registry('current_order');
        $cart = $this->cart;
        $reorderid =  $order->getIncrementId();
        $quote = $cart->getQuote();
        $quote->setReOrderId($reorderid);
        $cart->save();

        return $result;
    }
}

Please help me how to skip product with some ID in around Method.

0

Check original execute() and there you will found foreach ($items as $item) {

And inside this foreach loop you can sky the item which you want to skip

Like

$items = $order->getItemsCollection();
        foreach ($items as $item) {
            try {
               $orderItemSku = $item->getSku();
               $orderItemproductId = $item->getProduct();
                 /// SKip Product which has id 150
                 if(orderItemproductId == 150){
                    continue;
                 }
                $cart->addOrderItem($item);

        ......
| improve this answer | |
  • Hi @amit yes I have checked there in controller. Can you please help me to override this in my controller? – m2dev Nov 29 '19 at 7:25
  • I have already overriden one thing in my controller. – m2dev Nov 29 '19 at 7:25
  • can you please help? – m2dev Nov 29 '19 at 9:32
0

This is the actual code in class:


    /**
     * Action for reorder
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        $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();

        /* @var $cart \Magento\Checkout\Model\Cart */
        $cart = $this->_objectManager->get(\Magento\Checkout\Model\Cart::class);
        $items = $order->getItemsCollection();
        foreach ($items as $item) {
            try {
                $cart->addOrderItem($item);
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                if ($this->_objectManager->get(\Magento\Checkout\Model\Session::class)->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');
    }

and you are trying to do something on this action: $cart->addOrderItem($item);

I think you can try adding your logic in the method -

addOrderItem($orderItem, $qtyFlag = null)

If you see the method it has the below code:


    /**
     * Convert order item to quote item
     *
     * @param \Magento\Sales\Model\Order\Item $orderItem
     * @param true|null $qtyFlag if is null set product qty like in order
     * @return $this
     */
    public function addOrderItem($orderItem, $qtyFlag = null)
    {
If you see the method it has the below code:
        /* @var $orderItem \Magento\Sales\Model\Order\Item */
        if ($orderItem->getParentItem() === null) {
            $storeId = $this->_storeManager->getStore()->getId();
            try {
                /**
                 * We need to reload product in this place, because products
                 * with the same id may have different sets of order attributes.
                 */
                $product = $this->productRepository->getById($orderItem->getProductId(), false, $storeId, true);
            } catch (NoSuchEntityException $e) {
                return $this;
            }
            $info = $orderItem->getProductOptionByCode('info_buyRequest');
            $info = new \Magento\Framework\DataObject($info);
            if ($qtyFlag === null) {
                $info->setQty($orderItem->getQtyOrdered());
            } else {
                $info->setQty(1);
            }

            $this->addProduct($product, $info);
        }
        return $this;
    }

you can add before on this public method and throw exception from there. The exception will be caught in the controller class and the loop will go to next item. Hope it helps to add the logic you are trying to add

| improve this answer | |

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.