Getting order data after successful checkout hook

后端 未结 2 358
北海茫月
北海茫月 2020-12-02 19:08

In WooCommerce, I would like to send a request to an API once the customer has successfully checked out. Its basically a website where the client is selling online courses (

相关标签:
2条回答
  • 2020-12-02 19:54

    Update 2 Only For Woocommerce 3+ (added restriction to execute the code only once)

    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();
        }
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Related thread:

    • Get Order items and WC_Order_Item_Product in WooCommerce 3
    • How to get WooCommerce order details

    The code is tested and works.


    Updated (to get the product Id from Orders items as asked in your comment)

    May be you could use woocommerce_thankyou hook instead, that will display on order-received page your echoed code, this way:

    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>';
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    The code is tested and works.

    Then you can use all class WC_Abstract_Order methods on the $order object.

    Related:

    • How to get WooCommerce order details
    • Get Order items and WC_Order_Item_Product in WooCommerce 3
      • How to get Customer details from Order in WooCommerce?
    0 讨论(0)
  • 2020-12-02 19:56

    you can get the order items of an order by

       // 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'];
        }
    
    0 讨论(0)
提交回复
热议问题