I created a module that changes the status of the order from pending to processing based on specific payment status and if the payment status doesn't match, it cancel those order. the module is working fine for canceling the orders but not changing the status to processing. Here's my code

protected $searchCriteriaBuilder;

 * @var \Magento\Sales\Api\OrderManagementInterface
protected $orderManagement;
protected $date;
protected $orderFactory;
 * CancelOrder constructor.
 * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
 * @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
 * @param \Magento\Sales\Api\OrderManagementInterface $orderManagement
public function __construct(
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
    \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
    \Magento\Sales\Api\OrderManagementInterface $orderManagement,
    \Magento\Framework\Stdlib\DateTime\TimezoneInterface $date,
    \Magento\Sales\Model\OrderFactory $orderFactory
) {
    $this->orderFactory = $orderFactory;
    $this->orderRepository = $orderRepository;
    $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    $this->orderManagement = $orderManagement;
    $this->date = $date;

public function execute()
    $today          = date("Y-m-d h:i:s");
    $to             = strtotime('-120 min', strtotime($today));
    $time = date("Y-m-d h:i:s", $to);
    //$agoDate = '2016-11-07'; // For example date, your logic to calculate the date here
    $searchCriteria = $this->searchCriteriaBuilder
            array('from' => $time),

    $orders = $this->orderRepository->getList($searchCriteria);
    foreach ($orders->getItems() as $order) {
        $payment = $order->getPayment();
        $paymentStatus = $payment->getAdditionalInformation('payment_status');
        if($paymentStatus == 'SETTLED_BY_MERCHANT' || $paymentStatus == 'AUTHORISED') {
            $orderState = Order::STATE_PROCESSING;
        else {
            $this->orderRepository->save($order); // Cancel Order


2 Answers 2


You can do it like following way

    use Magento\Sales\Model\Order;

    $orderId = 1;
    //Use constructor injection for Order class. Below $objectManager is just for testing/explanation purpose
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId);
    $orderState = Order::STATE_PROCESSING;

set order state in your code.


Hope this helps.

Thanks !!


try this and let me know if it works,

if($paymentStatus == 'SETTLED_BY_MERCHANT' || $paymentStatus == 'AUTHORISED') {
            $orderState = Order::STATE_PROCESSING;
            //set order state before setting status

i just added this line $order->setState($orderState);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.