我想触发动作save_postronghop_order通过url,因为我有一个自定义函数,它将在检查时创建产品元。
我试过这样做:
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);
}
但在我的情况下似乎不起作用。如何触发更新所有订单并运行actionsave_postronghop_order所有订单?由于
你可以这样做,把你的代码放在functions.php
或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 );
}
要通过URL更新订单信息,您可以尝试以下步骤:
- 在根目录中创建一个文件
在文件的顶部,添加这段代码:
require(dirname(__FILE__) . '/wp-load.php');
- 获取所有订单
$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
}
- 现在,您将能够通过
URL
更新订单信息。点击url/页面名,例如,http://example.com/test.php