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
CodePudding user response:
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 );
}
CodePudding user response:
To update orders information through the URL, you can try the following steps.
- Create a file in your root directory
On the top of the file, add this piece of code.
require(dirname(__FILE__) . '/wp-load.php');
- 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
}
- Now, you will be able to update orders information through
URL
. Hit your url/pagename, for example,http://example.com/test.php