3

How to Order status change Processing to complete in magento 2.3

3

You can Change status this way..

    $orderId = 1; // here order id

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $order = $objectManager->create('\Magento\Sales\Model\Order')
                               ->load($orderId); 

    $order->setState("complete")->setStatus("complete");

    $order->save();
  • can u please help me solve this issue @rakesh – divya sekar Feb 18 at 10:07
4

You can Change the order status to complete using repository Class of order entity \Magento\Sales\Api\OrderRepositoryInterface

    $order = $this->orderRepository->get($orderiId);
    $order->setStatus($completeStatus)->setState($completeStatus);
    $this->orderRepository->save($order); 

Code Looks Like:

<?php
namespace {ClassNameSpace};

use Magento\Framework\Exception\NoSuchEntityException;

class Test {

    /**
     * @var \Magento\Sales\Api\OrderRepositoryInterface
     */
    private $orderRepository;

    public function __construct(
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository      
    ) {

        $this->orderRepository = $orderRepository;
    }
    public function ChangeOrderStatusToComplete()
    {
        $completeStatus = \Magento\Sales\Model\Order::STATE_COMPLETE;
        $id = 2;
        try{
            $order = $this->orderRepository->get($id);
            $order->setStatus($completeStatus)->setState($completeStatus);
            $this->orderRepository->save($order);           
        } catch (NoSuchEntityException $ex) {
            // error Happen
        }


    }
}
  • 1
    Sir If I want to change it Hold to Processing how can we set it on Processing or any other status??? like in your code it is only for Complete. – Asad Khan Sep 18 at 13:29
  • @Amit How does this work? i have a user and the order id = 2 how does this update it? how do i run it – jibin george Nov 21 at 16:30
2

This is duplicated from this Link

use Magento\Sales\Model\Order;

$orderId = 1;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('\Magento\Sales\Model\Order') ->load($orderId);
$orderState = Order::STATE_COMPLETE;
$order->setState($orderState)->setStatus(Order::STATE_COMPLETE);
$order->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy