OhMad
OhMad

Reputation: 7279

WooCommerce hooks – woocommerce_update_order problems

I've registered the following woocommerce hook:

add_action('woocommerce_update_order', 'some_func', 300, 2);
function some_func($order_id, $order){
  // ...
}

However, I have a few problems:

This fires multiple times instead of only at the end when updating an order. It fires two times with the old order, and once with the one one.

I've also tried the following:

add_action('woocommerce_update_order', 'some_func', 300, 2);
function some_func($order_id, $order){
    remove_action('woocommerce_update_order', 'some_func');
    // ...
}

Which doesn't change it, either.

Also, I've tried modifying the remove_action to include the priority and argument count, like:

add_action('woocommerce_update_order', 'some_func', 300, 2);
function some_func($order_id, $order){
    remove_action('woocommerce_update_order', 'some_func', 300, 2);
    // ...
}

Now, it does fire only once, but it gives me the old order instead of the newly updated one.

I'm using WooCommerce 3.7.0.

Any suggestions on how I can get the most up-to-date version of the order after an update while only firing the hook exactly once?

Thanks!

Upvotes: 1

Views: 2159

Answers (2)

mujuonly
mujuonly

Reputation: 11861

add_action( 'save_post', 'my_save_post_function', 10, 3 );

function my_save_post_function( $post_ID, $post, $update ) {

  if("shop_order" == $post->post_type){
  $msg = 'An order updte fireda';

  wp_die( $msg );
  }
}

Do your action inside post condition

Upvotes: 2

mujuonly
mujuonly

Reputation: 11861

function mysite_pending($order_id) {
    error_log("$order_id set to PENDING", 0);
    }
    function mysite_failed($order_id) {
    error_log("$order_id set to FAILED", 0);
    }
    function mysite_hold($order_id) {
    error_log("$order_id set to ON HOLD", 0);
    }
    function mysite_processing($order_id) {
    error_log("$order_id set to PROCESSING", 0);
    }
    function mysite_completed($order_id) {
    error_log("$order_id set to COMPLETED", 0);
    }
    function mysite_refunded($order_id) {
    error_log("$order_id set to REFUNDED", 0);
    }
    function mysite_cancelled($order_id) {
    error_log("$order_id set to CANCELLED", 0);
    }

    add_action( ‘woocommerce_order_status_pending’, ‘mysite_pending’);
    add_action( ‘woocommerce_order_status_failed’, ‘mysite_failed’);
    add_action( ‘woocommerce_order_status_on-hold’, ‘mysite_hold’);
    // Note that it’s woocommerce_order_status_on-hold, not on_hold.
    add_action( ‘woocommerce_order_status_processing’, ‘mysite_processing’);
    add_action( ‘woocommerce_order_status_completed’, ‘mysite_completed’);
    add_action( ‘woocommerce_order_status_refunded’, ‘mysite_refunded’);
    add_action( ‘woocommerce_order_status_cancelled’, ‘mysite_cancelled’);

These much status change hooks are there. You may use the specific one you need. Hope it may be helpful

Upvotes: 1

Related Questions