WooCommerce trigger all orders update

I want to trigger action save_post_shop_order by url, because I have custom function that will create product meta there on checking.

I tried something like:

add_action('wp_head', 'update_orders_by_url');
function update_orders_by_url() {
    if( isset( $_GET['update_woo_orders'] ) ) {
    $query = new WC_Order_Query( array(
        'limit' => -1
    ));
    
    $orders = $query->get_orders();
    
    foreach($orders as $order){
         $arra2[] = $order;
                    
    // Save
    $order->save();

    }
}
    //echo count($arra2);
}

But seems not working in my case.
How I can trigger update all orders and run action save_post_shop_order for all orders?
Thanks

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can do it this way, put your code in functions.php or a plugin

 add_filter( 'handle_bulk_actions-edit-shop_order', 'save_post_shop_order', 10, 3 );
 /**
  * @param $redirect_to
  * @param $action
  * @param $post_ids
  * @return mixed|string
  */
 function save_post_shop_order( $redirect_to, $action, $post_ids ) {

      // do your job




       return $redirect_to = add_query_arg( array(
            'bulk_action_name' => $action,
            'processed_count' => count( $processed_ids ),
            'processed_ids' => implode( ',', $processed_ids ),
        ), $redirect_to );
 }


add_action( 'admin_notices', 'save_post_shop_order_admin_notice' );
 /**
  *  The results notice from bulk action on orders
  */
function save_post_shop_order_admin_notice() {

  $count = intval( $_REQUEST['processed_count'] );

  printf( '<div id="message" class="updated fade"><p>' .
      _n( "",
          "",
          $count,
          ''
      ) . '</p></div>', $count );
}

Method 2

To update orders information through the URL, you can try the following steps.

  1. Create a file in your root directory

On the top of the file, add this piece of code.

require(dirname(__FILE__) . '/wp-load.php');

  1. Get all orders
$orders = wc_get_orders( array('numberposts' => -1) );

// Loop through each WC_Order object
foreach( $orders as $order ){
    echo $order->get_id() . '<br>'; // The order ID
    echo $order->get_status() . '<br>'; // The order status
    .......
    // do your stuff
}
  1. Now, you will be able to update orders information through URL.
    Hit your url/pagename, for example, http://example.com/test.php


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x