woocommerce_order_status_changed hook: 身份变老变新?

woocommerce_order_status_changed hook: getting old and new status?

如何使用 WooCommerce 挂钩获取订单的旧状态和新状态:woocommerce_order_status_changed

这是我的代码,但只有 $order_id 被填充..

add_action('woocommerce_order_status_changed','woo_order_status_change_custom');
function woo_order_status_change_custom($order_id,$old_status,$new_status) {
//order ID is filled
//old_status and new_status never
//tested by logging the parameters
}

现在我可以使用此代码轻松获取新状态:

 $order = new WC_Order( $order_id );
$orderstatus = $order->status;

但是$old_status是空的,我怎样才能得到之前的订单状态呢?

试试这个代码。根据我的说法,它应该根据您的评论工作。

add_action( 'save_post', 'wpse63478_save' );
function wpse63478_save() {

 if(!current_user_can('manage_options'))
   return false;
 if(!is_admin())
    return false;
 if($_REQUEST['post_type'] != 'shop_order')
    return false;
 if($_REQUEST['post_ID']!='')
 {
    $orderId = $_REQUEST['post_ID'];
    $order = new WC_Order( $orderId );
    $currentStatus = $order->status;
    $requestedStautus = $_REQUEST['order_status'];
    if ( $requestedStautus== 'on-hold' and $currentStatus == 'completed') {
        //Do your work here
    }
 }
}

我正在寻找 wc 挂钩并找到了这个 post。未设置参数的原因是您缺少 add_action 函数中的参数。该函数默认只有一个参数。要同时拥有这三个,您应该使用:

add_action('woocommerce_order_status_changed', 'woo_order_status_change_custom', 10, 3);

10 是 Wordpress 中操作的默认顺序,最后一个参数是 Wordpress 应该传递给自定义操作的参数数量。

因为您没有在 add_action 调用结束时添加 NUMBER 个参数。这是正确的行:

add_action('woocommerce_order_status_changed','woo_order_status_change_custom', 10, 3);

“10, 3”表示"I want 3 parameters will be sent to my callback function"。默认情况下只会发送 1 个参数 (order_id)。