3

I want to skip some products whenever reorder. file path in vendor folder (vendor/magento/module-sales/Controller/AbstractController/Reorder.php:57) and it is an abstract Class.

I want simply add this code condition. if($item->getSku() != 'offer123') Is it possible by plugin ?

foreach ($items as $item) {
   if($item->getSku() != 'offer123'){ // i want to apply this condition 
            try {
                $cart->addOrderItem($item);
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                if ($this->_objectManager->get(\Magento\Checkout\Model\Session::class)->getUseNotice(true)) {
                    $this->messageManager->addNotice($e->getMessage());
                } else {
                    $this->messageManager->addError($e->getMessage());
                }
                return $resultRedirect->setPath('*/*/history');
            } catch (\Exception $e) {
                $this->messageManager->addException($e, __('We can\'t add this item to your shopping cart right now.'));
                return $resultRedirect->setPath('checkout/cart');
            }
}
        }

Thanks in advance.

1

Try following code. It is working for me.

File: etc/di.xml

<type name="Magento\Checkout\Model\Cart">
    <plugin name="custom_skip_product_addtocart" type="[Vendor]\[Module]\Plugin\Cart" sortOrder="100"/>
</type>

Then create following file in your module.

File: Plugin/Cart.php

<?php

namespace [Vendor]\[Module]\Plugin;

class Cart
{
    public function aroundAddOrderItem($subject, callable $proceed, $orderItem, $qtyFlag = null)
    {
        if ($orderItem->getParentItem() === null) {
            $sku = $orderItem->getSku();
            if(in_array($sku, ['WT08-XS-Black']))
            {
                return $this;
            }
            else
            {
                return $proceed($orderItem, $qtyFlag);
            }
        }
        return $this;
    }
}

Let me know if you have any trouble.

  • Hi Yash7690 It's working perfect :-) , Thanks a lot. – Rakesh Chand Feb 13 at 11:40
  • Thats great !!! Cheers !!! – Yash Shah Feb 13 at 11:45

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.