jjzjj

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

coder 2024-05-02 原文

按照这个 post 的思路,当管理员使用我的支付网关通过管理面板创建订单时,我正在尝试连接到我自己的自定义支付网关。

我添加了以下代码:

    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 中断点放入这些相应的方法中,但没有一个被击中。

最佳答案

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

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

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 钩子(Hook),它有两个参数:

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

但我没有像预期的那样让它工作。我在 WC_Order save() 方法的源代码中找到了它.

关于php - WooCommerce 3.0 : can't find hook which corresponds to an admin created backend order,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45335298/

有关php - WooCommerce 3.0 : can't find hook which corresponds to an admin created backend order的更多相关文章

随机推荐