How to skip some products to addtocart when reorder in magento 2?
Pergunta
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.
Solução
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.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange