K_C
K_C

Reputation: 435

Woocommerce update_status() Only Works for Admin Orders

Very odd issue. I have built a plugin (for a client, not a public one) that creates a couple of REST endpoints that a shipping service (Shippo) passes data to (all this is fine and working). The plugin takes the data, gets an Order Number, and attempts to set a Completed status.

The code only works perfectly if the Order is from the Admin account. But any other custom account and the status will not save. An order note is generated claiming the status was changed, and I can set meta data on the order. But the status will not change.

In the code, I have attempted to use update_status() and set_status() both with and without .save();

add_action('rest_api_init', function () {
register_rest_route('lab/v1', '/shipment_label_created_shippo', ['methods' => 'POST', 'callback' => 'rest_shipment_label_created_shippo', 'permission_callback' => '__return_true', ]);
register_rest_route('lab/v1', '/shipment_label_updated_shippo', ['methods' => 'POST', 'callback' => 'rest_shipment_label_updated_shippo', 'permission_callback' => '__return_true', ]);
    register_rest_route('lab/v1', '/shipment_tracking_updated_shippo', ['methods' => 'POST', 'callback' => 'rest_shipment_tracking_updated_shippo', 'permission_callback' => '__return_true', ]);});

// Label Created in Shippo (transaction_created)
function rest_shipment_label_created_shippo($data){
    $log = new WC_Logger();

$response = new WP_REST_Response("Failed");
$response->set_status(200);

$meta_order_number = $data['data']['metadata'];
$woo_order_number = explode(" ", $meta_order_number);
$order_number = intval($woo_order_number[1]);

if ($shippo_status == "SUCCESS" && !is_null($shippo_order_id)) {


  if( class_exists('WC_Order') && $order_number > 0 ) {

      $order = wc_get_order($order_number);

      if ($order) {
          $payment_method = $order->get_payment_method();
          $payment_method_title = $order->get_payment_method_title();
          $date_paid = $order->get_date_paid();

          // Update the Order Meta as well for tracking
          $update_order_was_shipped = update_post_meta($order_number, '_order_was_shipped', $date_shipped);

          // Add the Shippo Transaction ID
          $update_transactionid = update_post_meta($order_number, '_shippo_transaction_id', $shippo_transaction_id);


          if ($payment_method == "invoice" && !is_null($date_paid))
          {

              $order->update_status('shipped-invoiced');
              $saved_order_id = $order->save();
          }
          else
          {
              $order->update_status('completed');

              $saved_order_id = $order->save();
          }
  }

Thanks in advance for any thoughts you might have!

Upvotes: 0

Views: 974

Answers (1)

K_C
K_C

Reputation: 435

Just in case someone else stumbles onto this I wanted to answer the question.

The reason it appears as though only Admin orders (orders I placed) are working is because I never had the order tab up. Our client, always had the Order open that they were updating via a 3rd party. When the Rest API was hit by the 3rd party, order details were not saved because the order was locked.

This makes sense of course, but was not top of mind for me.

Upvotes: 1

Related Questions