0

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
        ->addFilter(
            'updated_at',
            array('from' => $time),
            'lteq'
        )->addFilter(
            'status',
            'pending',
            'eq'
        )->create();

    $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;
            $order->setStatus($orderState);
            $this->orderRepository->save($order);
        } 
        else {
            $order->setStatus(Order::STATE_CANCELED);
            $this->orderRepository->save($order); // Cancel Order
            
        }
    }
}

}

2 Answers 2

0

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;
    $order->setState($orderState)->setStatus($orderState);
    $order->save();

set order state in your code.

$order->setState($orderState)->setStatus($orderState);

Hope this helps.

Thanks !!

0

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
            $order->setState($orderState);
            $order->setStatus($orderState);
            $this->orderRepository->save($order);
}

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

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.