首页 文章

成功结账后获取订单数据

提问于
浏览
9

在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";
}

我在激活的插件中编写此代码,为了方便起见,我目前正在使用Cash on Delivery方法 .

任何人都可以指出我出错的地方,因为当我结账时,我无法看到消息"hooked"我正在打印也不是 $order_id

我带我到成功页面,并没有显示我正在打印的这两件事 .

2 回答

  • 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 );
    
            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文件中,或者放在任何插件文件中 .

    相关主题:

    代码经过测试和运行 .


    更新(从评论中询问的订单项目中获取产品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方法 .

  • 20

    您可以通过获取订单的订单商品

    // Getting an instance of the order object
    
        $order = new WC_Order( $order_id );
        $items = $order->get_items();
    
       //Loop through them, you can get all the relevant data:
    
        foreach ( $items as $item ) {
            $product_name = $item['name'];
            $product_id = $item['product_id'];
            $product_variation_id = $item['variation_id'];
        }
    

相关问题