Get the product ID in WooCommerce 3+ Order items

限于喜欢 提交于 2021-02-07 08:01:49

问题


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

回答1:


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




回答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.




回答3:


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.



来源:https://stackoverflow.com/questions/45121404/get-the-product-id-in-woocommerce-3-order-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!