成功结帐钩子后获取订单数据

新手上路,请多包涵

在 WooCommerce 中,我想在客户成功结帐后向 API 发送请求。它基本上是一个客户销售在线课程的网站(如 udemy )。

当客户结帐时,我想发送一个 API 请求并为该特定课程注册用户。我尝试了几个 WooCommerce 钩子,但没有一个对我有用。

这是我正在使用的代码:

 add_action('woocommerce_checkout_order_processed', 'enroll_student', 10, 1);

function enroll_student($order_id)
{
    echo $order_id;
    echo "Hooked";
}

我正在为插件编写此代码并使其更容易,我目前正在使用货到付款方式。

谁能指出我哪里出错了,因为当我结帐时,我看不到我正在打印的“上钩”消息,也看不到 $order_id

它带我进入成功页面,并没有显示我正在打印的这两件事。

原文由 Syed Haris Ali Ghaznavi 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 343
2 个回答

仅适用于 Woocommerce 3+ 的 更新 2 (添加了仅执行一次代码的限制)

 add_action('woocommerce_thankyou', 'enroll_student', 10, 1);
function enroll_student( $order_id ) {
    if ( ! $order_id )
        return;

    // Allow code execution only once
    if( ! get_post_meta( $order_id, '_thankyou_action_done', true ) ) {

        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );

        // Get the order key
        $order_key = $order->get_order_key();

        // Get the order number
        $order_key = $order->get_order_number();

        if($order->is_paid())
            $paid = __('yes');
        else
            $paid = __('no');

        // Loop through order items
        foreach ( $order->get_items() as $item_id => $item ) {

            // Get the product object
            $product = $item->get_product();

            // Get the product Id
            $product_id = $product->get_id();

            // Get the product name
            $product_id = $item->get_name();
        }

        // Output some data
        echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';

        // Flag the action as done (to avoid repetitions on reload for example)
        $order->update_meta_data( '_thankyou_action_done', true );
        $order->save();
    }
}

代码在您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。

相关话题:

代码经过测试并且可以工作。


更新(按照您的评论中的要求从 Orders 项目中获取 产品 ID

可能你可以使用 woocommerce_thankyou 钩子代替,这将在订单接收页面上显示你的回显代码,这样:

 add_action('woocommerce_thankyou', 'enroll_student', 10, 1);
function enroll_student( $order_id ) {

    if ( ! $order_id )
        return;

    // Getting an instance of the order object
    $order = wc_get_order( $order_id );

    if($order->is_paid())
        $paid = 'yes';
    else
        $paid = 'no';

    // iterating through each order items (getting product ID and the product object)
    // (work for simple and variable products)
    foreach ( $order->get_items() as $item_id => $item ) {

        if( $item['variation_id'] > 0 ){
            $product_id = $item['variation_id']; // variable product
        } else {
            $product_id = $item['product_id']; // simple product
        }

        // Get the product object
        $product = wc_get_product( $product_id );

    }

    // Ouptput some data
    echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';
}

代码在您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。

代码经过测试并且可以工作。

然后您可以在 $order 对象上使用所有类 WC_Abstract_Order 方法。

有关的:

原文由 LoicTheAztec 发布,翻译遵循 CC BY-SA 4.0 许可协议

而不是“woocommerce_thankyou”钩子,“woocommerce_checkout_order_processed”钩子是相关的钩子。 ‘woocommerce_checkout_order_processed’ 挂钩只会被调用一次,您无需为每个产品添加元数据并进行额外调用以保持检查代码仅运行一次。因为,“woocommerce_thankyou”可以被调用多次,即每次加载谢谢页面时。将 add_action('woocommerce_thankyou', 'enroll_student', 10, 1); 替换为

add_action('woocommerce_checkout_order_processed', 'enroll_student', 10, 1);

并删除元代码和检查。更新的代码是

add_action('woocommerce_checkout_order_processed', 'enroll_student', 10, 1);
function enroll_student( $order_id ) {
// Getting an instance of the order object
$order = wc_get_order( $order_id );

if($order->is_paid())
   $paid = 'yes';
else
  $paid = 'no';

    // iterating through each order items (getting product ID and the product object)
// (work for simple and variable products)
foreach ( $order->get_items() as $item_id => $item ) {

    if( $item['variation_id'] > 0 ){
        $product_id = $item['variation_id']; // variable product
    } else {
        $product_id = $item['product_id']; // simple product
    }

    // Get the product object
    $product = wc_get_product( $product_id );

}

// Ouptput some data
echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';

}

原文由 Nabeel Perwaiz 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题