Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the product ID in WooCommerce 3+ Order items

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
      }
}
}
like image 786
Ali Avatar asked Jan 31 '23 00:01

Ali


2 Answers

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

like image 147
LoicTheAztec Avatar answered Feb 03 '23 08:02

LoicTheAztec


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.

like image 27
Garet Claborn Avatar answered Feb 03 '23 06:02

Garet Claborn