[Drupal 6] [Ubercart] How to change the order status programmatically ?

| | 2 min read

One of our Drupal clients who had been using the Drupal Ubercart module had requested us to change the status of the order after a user completes the Ubercart checkout completion process. If you are facing the same situation with Ubercart in your Drupal site and would like to know how to change the order status programmatically then read on to find out the solution.

Our client's Drupal website could change the status of order after completing checkout . After successfully submitting a recurring product order payment, the status of the order is set to 'completed'. However in the same site, a user can purchase alacart items too. The client wanted us to implement a feature where when the user purchased alacart items with the recurring product then the order status should be set to 'payment_received' which otherwise was set to display 'completed'. Here is how we changed the order status programmatically.

This code has to be added in the checkout complete function

/**
* ubercart checkout complete function
*/
function hook_uc_checkout_complete($order, $account) {

  //get recurring product from cart contents
  $recurring_products = uc_recurring_product_get_recurring_products_in_order($order);

  //get all product from cart
  $cart_contents = uc_cart_get_contents();

  //get alacart items from cart contents
  // filter alacart items from cart contents.
  if (!empty($recurring_products)) {
    foreach ($cart_contents as $cart_key => $cart_value) {
      foreach ($recurring_products as $recur_key => $recur_value) {
        if ($cart_value->model !=$recur_value['model'] ) {
          // this array contain alacart items  
          $purchased_alacart[$cart_key] = $cart_value;  
        }
      }
    }
  }

  //assign status value.
  if (empty($purchased_alacart)) {
    $status = "completed";
  }
  else {
    $status = "payment_received";
  }

  // update the status to the order 
  uc_order_update_status($order_id , $status);

  // if you want anything add to order comments
  uc_order_comment_save($order->order_id, $user->uid,$message, 'order', $status);
}