5

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
      }
}
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Ali
  • 91
  • 2
  • 2
  • 9

3 Answers3

7

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks its working,How can i get product billing detail using order_id, can you please tell me about this – Ali Jul 17 '17 at 17:51
  • `$item_values->get_id()` does not retrieve the product ID, that would be `$item_values->get_product_id()`. – helgatheviking Aug 04 '17 at 17:08
  • @helgatheviking Yes you are right… I make the update. – LoicTheAztec Aug 04 '17 at 17:43
  • @ali I have updated my code … To get billing details using product_id refer to: [WOOCOMMERCE ORDERS DETAIS IN VERSION 3.0+](https://stackoverflow.com/questions/39401393/how-to-get-woocommerce-order-details/44708344#44708344) – LoicTheAztec Aug 04 '17 at 17:49
2

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.

0

Try this, hope it will work

add_action( 'woocommerce_thankyou', 'your_function_name', 10);

function your_function_name($order_id)
{
        $order = wc_get_order($order_id);

        foreach($order->get_items() as $order_key => $order_value)
        {

            $product_id = $order_value->get_data()['product_id'];
            if($product_id == '123')
            {
                //do what you wnat and 123 is random product id, you can match product id with other as you want
            }
        }    
}

Thank you.

MakeWebBetter
  • 463
  • 3
  • 5