WooCommerce 3.0:找不到与管理员创建的后端订单相对应的挂钩

WooCommerce 3.0: can't find hook which corresponds to an admin created backend order

根据此 ,当管理员使用我的支付网关通过管理面板创建订单时,我尝试连接到我自己的自定义支付网关。

我添加了以下代码:

    add_action( 'woocommerce_process_shop_order_meta', array( $this, 'process_offline_order' ) );
    add_action( 'woocommerce_admin_order_actions_end', array( $this, 'process_offline_order2' ) );
    add_action( 'woocommerce_save_post_shop_order', array( $this, 'process_offline_order3' ) );

我已经尝试将 xdebug 中断点放入这些相应的方法中,但是 none 其中的方法受到了攻击。

经过一些研究和测试,我认为正确的钩子是这个 WP 钩子之一:

所以我使用了第一个,因为它最方便“shop_order” post 类型:

add_action( 'save_post_shop_order', 'process_offline_order', 10, 3 );
function process_offline_order( $post_id, $post, $update ){

    // Orders in backend only
    if( ! is_admin() ) return;

    // Get an instance of the WC_Order object (in a plugin)
    $order = new WC_Order( $post_id ); 

    // For testing purpose
    $trigger_status = get_post_meta( $post_id, '_hook_is_triggered', true );

    // 1. Fired the first time you hit create a new order (before saving it)
    if( ! $update )
        update_post_meta( $post_id, '_hook_is_triggered', 'Create new order' ); // Testing

    if( $update ){
        // 2. Fired when saving a new order
        if( 'Create new order' == $trigger_status ){
            update_post_meta( $post_id, '_hook_is_triggered', 'Save the new order' ); // Testing
        }
        // 3. Fired when Updating an order
        else{
            update_post_meta( $post_id, '_hook_is_triggered', 'Update  order' ); // Testing
        }
    }
}

您将能够使用此代码轻松进行测试。对我来说它工作正常。


我也尝试过使用 woocommerce_before_order_object_save 钩子,它有两个参数:

  • $order(WC_Order 对象)
  • $data_store(要存储的数据通过WC_Data_Store class)

但是我并没有像我期望的那样工作。我在WC_Order[=的源代码中找到了它49=]save()方法。