I am trying to get woocommerce thank you page order_id. Using the code below. But unfortunately I can't get it.
    add_action( 'woocommerce_thankyou', 'bbloomer_check_order_product_id');
function bbloomer_check_order_product_id( $order_id ){
$order = new WC_Order( $order_id );
$items = $order->get_items(); 
foreach ( $items as $item ) {
   $product_id = $item['product_id'];
      if ( $product_id == XYZ ) {
        // do something
      }
}
}
This code is outdated for WooCommerce version 3+. You should use instead:
add_action( 'woocommerce_thankyou', 'check_order_product_id', 10, 1);
function check_order_product_id( $order_id ){
    # Get an instance of WC_Order object
    $order = wc_get_order( $order_id );
    # Iterating through each order items (WC_Order_Item_Product objects in WC 3+)
    foreach ( $order->get_items() as $item_id => $item_values ) {
        // Product_id
        $product_id = $item_values->get_product_id(); 
        // OR the Product id from the item data
        $item_data = $item_values->get_data();
        $product_id = $item_data['product_id'];
        # Targeting a defined product ID
        if ( $product_id == 326 ) {
            // do something
        }
    }
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works for WooCommerce version 3+
Reference: How to get WooCommerce order details
I was not satisfied with current answers, as sometimes you need to check multiple products. If you do the same search for each product, it's really a waste, so I put it into a dispatcher format.
add_action('woocommerce_order_status_completed', 'onItemCheckout',10,1);
function onItemCheckout($order_id){
    $order = wc_get_order($order_id);
    foreach ($order->get_items() as $item_key => $item_values){
        $product_id = $item_values->get_product_id();
        switch($item_values->get_product_id()){
            case 9999 : FreeShipping($order, $product_id); break;
            case 1010 : RequireValidation($order, $product_id); break;
            default: break;
        }
    }
}
Alternatively,...
$ItemCheckoutHandler=[];
$ItemCheckoutHandler[9999]='FreeShipping';
$ItemCheckoutHandler[1010]='RequireValidation';
add_action('woocommerce_order_status_completed', 'onItemCheckout',10,1);
function onItemCheckout($order_id){
    global $ItemCheckoutHandler;
    $order = wc_get_order($order_id);
    foreach ($order->get_items() as $item_key => $item_values){
        $product_id=$item_values->get_product_id();
        $ItemCheckoutHandler[ $product_id ]( $order, $product_id );
    }   //Call the function assigned to that product id in the array
}
In either case, the assigned functions would take the order object, rather than the id, and the product_id as an argument:
function FreeShipping($order, $product_id){ ... }
function RequireValidation($order, $product_id){ ... }
You can of course customize these inputs to your liking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With