【问题标题】:WooCommerce 3.0: can't find hook which corresponds to an admin created backend orderWooCommerce 3.0:找不到对应于管理员创建的后端订单的钩子
【发布时间】:2017-07-26 18:56:34
【问题描述】:

按照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 断点放入这些各自的方法中,但没有一个被命中。

【问题讨论】:

  • 到底是什么问题?你想做什么?
  • 您的问题到底是什么?
  • 当 WooCommerce 管理员代表他们的一位客户创建订单时,我正在尝试触发一个可以连接到支付网关 API 的事件。

标签: php wordpress woocommerce hook-woocommerce orders


【解决方案1】:

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

所以我使用了第一个,因为它对于“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 钩子有 2 个参数:

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

但是我没有得到它的工作,正如我所期望的那样。我在 WC_Ordersave() 方法的源代码中找到了它。 p>

【讨论】:

  • 这段代码看起来都很完美,但我无法让该方法在本地或在我的测试服务器上触发。我想这可能是我的配置。
猜你喜欢
  • 2013-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多