skip to Main Content

I have made a function for when the WooCommerce order payment is successful.

add_action( 'woocommerce_payment_complete', 'do_it' ); 
function do_it( $order_id ){
    // Here will run the function
    $order_id="//I want the order id here of woocommerce";
    $order = wc_get_order( $order_id ); //Then I can get order details from here
}

How Can I get $order_id here with this hook?

2

Answers


  1. You already have the order ID in your function as the parameter.

    add_action('woocommerce_payment_complete', 'do_it', 10, 1);
    function do_it($order_id) {
        $order = new WC_Order( $order_id );
        $myuser_id = (int)$order->user_id;
        $user_info = get_userdata($myuser_id);
        $items = $order->get_items();
        foreach ($items as $item) {
            if ($item['product_id']==24) {
              // Do something clever
            }
        }
        return $order_id;
    }
    
    Login or Signup to reply.
  2. Try with this.

    add_action( 'woocommerce_payment_complete', 'do_it' ); 
    function do_it( $orderId ){
        // Get an instance of the WC_Order object (same as before)
        $order = wc_get_order( $order_id );
        $orderId = $order->get_id(); // Get the order ID
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search