【问题标题】:What hook to use once order is created in Woocommerce 3在 Woocommerce 3 中创建订单后使用什么挂钩
【发布时间】:2021-02-02 07:59:01
【问题描述】:

我创建了以下插件,它应该在创建 Woocommerce 订单时向外部服务器发送 POST HTTP 请求。但是,这并没有发生:外部服务器上没有收到请求,wp-content/debug.log 中没有显示任何内容(我在wp-config.php 中有define( 'WP_DEBUG_LOG', true );)。我做错了什么?

<?php
/**
 * Plugin Name: MyPlugin
 */


function my_hook($order_id) {
    $url = "https://example.com/do_something";
    $data = wp_remote_post($url, array(
        'headers'     => array(
            'Authorization' => "Token my_token",
            'Content-Type'  => 'application/json; charset=utf-8',
        ),
        'body'        => json_encode(array('order_id' => $order_id)),
        'method'      => 'POST',
        'data_format' => 'body',
    ));
}
add_action(
    'woocommerce_new_order',
    'my_hook'
);

?>

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce orders


    【解决方案1】:

    从 WooCommerce 4.3.0 开始,要使用的正确挂钩是 woocommerce_checkout_order_created,以便在创建订单时向外部服务器发送 POST HTTP 请求。所以你的代码将是:

    add_action( 'woocommerce_new_order', 'my_hooked_function_callback' );
    
    function my_hooked_function_callback( $order ) {
    
        $url = "https://example.com/do_something";
    
        $data = wp_remote_post( $url, array(
            'headers'     => array(
                'Authorization' => "Token my_token",
                'Content-Type'  => 'application/json; charset=utf-8',
            ),
            'body'        => json_encode( array( 
               'order_id' => $order->get_id() 
            ) ),
            'method'      => 'POST',
            'data_format' => 'body',
        ) );
    }
    

    代码进入活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

    hook is locatedcreate_order() method for WC_Checkout 类中。

    注意:该代码不适用于通过管理员手动创建的订单。


    补充说明:

    • 要更新订单元数据一旦创建订单,您将使用操作挂钩 woocommerce_checkout_update_order_meta带有 2 个可用参数:$order_id$data(发布的数据)
    • 要更新订单数据或元数据在订单创建之前,您将使用操作挂钩woocommerce_checkout_create_order 带有 2 个可用参数:$order 和 @ 987654332@(发布的数据)
    • 要更新订单商品数据或元数据在创建订单之前,您将使用带有 2 个可用参数的操作挂钩 woocommerce_checkout_create_order_line_item $item$cart_item_key$values$order

    相关:How to debug in WooCommerce 3

    【讨论】:

      【解决方案2】:

      如果你进入 class-wc-checkout,你会发现 create_order 函数会在结束前触发这些钩子:

              /**
               * Action hook to adjust order before save.
               *
               * @since 3.0.0
               */
              do_action( 'woocommerce_checkout_create_order', $order, $data );
      
              // Save the order.
              $order_id = $order->save();
      
              /**
               * Action hook fired after an order is created used to add custom meta to the order.
               *
               * @since 3.0.0
               */
              do_action( 'woocommerce_checkout_update_order_meta', $order_id, $data );
      
              /**
               * Action hook fired after an order is created.
               *
               * @since 4.3.0
               */
              do_action( 'woocommerce_checkout_order_created', $order );
      

      也许你只需要使用其中之一?

      【讨论】:

        猜你喜欢
        • 2017-06-10
        • 1970-01-01
        • 2018-05-26
        • 2019-03-14
        • 2021-04-21
        • 1970-01-01
        • 2020-08-17
        • 2013-11-02
        • 1970-01-01
        相关资源
        最近更新 更多