简体   繁体   English

在WooCommerce 3+订购商品中获取商品ID

[英]Get the product ID in WooCommerce 3+ Order items

I am trying to get woocommerce thank you page order_id. 我想让woocommerce感谢你的页面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+. 此代码已过时,适用于WooCommerce版本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. 代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

This code is tested and works for WooCommerce version 3+ 此代码经过测试,适用于WooCommerce版本3+

Reference: How to get WooCommerce order details 参考: 如何获取WooCommerce订单详细信息

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: 在任何一种情况下,分配的函数都将使用order 对象而不是id,并将product_id作为参数:

function FreeShipping($order, $product_id){ ... }
function RequireValidation($order, $product_id){ ... }

You can of course customize these inputs to your liking. 您当然可以根据自己的喜好自定义这些输入。

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. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM