0

If a customer placed an order of 3 different items with different quantities like

  1. shirt 2 qty
  2. Watch 1qty
  3. pant 3qty

and cancels the order item at different times I have to set the state to order cancelled

here's my code can anyone please tell me where I am wrong

if (isset($_POST['order'])) {
         $_orderCollectionFactory = $objectManager->create('\Magento\Sales\Model\ResourceModel\Order\CollectionFactory');

     $collection = $_orderCollectionFactory->create()

             ->addFieldToSelect('*')
             ->addFieldToFilter('status', ['neq' => 'cancelled']); 
foreach ($collectionFactory as $order)
        {
            $items = $order->getAllVisibleItems();
            foreach ($items as $items) {

                $totalitem = $totalitem + ($item['qty_canceled']);
                $itemcount = $order->getQtyOrdered();
               if ($itemcount == ($totalitem))
                 {
                    $order->setState("canceled");
                    $order->save();
                }

            }    
    }
}

1 Answer 1

0

Finally, I have Solved the issue for the above code

$_orderCollectionFactory = $objectManager->create('\Magento\Sales\Model\ResourceModel\Order\CollectionFactory');

 $collection = $_orderCollectionFactory->create()
         ->addFieldToSelect('*')
         ->addFieldToFilter('status', ['neq' => 'cancelled']); 
         

foreach ($collection as $order) {

        $items = $order->getAllVisibleItems();
        $totalitem=0;
        foreach ($items as $item) {
             $totalitem = $totalitem + ($item['qty_canceled']);
            $itemcount = $order->getQtyOrdered();

            if ($itemcount == ($totalitem))
             {
                echo $order->getIncrementId();
                $order->setState("canceled");
                $order->setStatus("canceled");
                $order->save();
             }
        }    
}

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.