2017-07-15 159 views
2

我试图获得woocommerce谢谢page order_id。使用下面的代码。 但不幸的是我无法得到它。在WooCommerce中获取产品ID 3+订购商品

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

此代码已过时WooCommerce版本3+。您应该改用:

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

代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。

此代码测试,适用于WooCommerce版本3+

参考:How to get WooCommerce order details

+0

感谢它的工作,我如何使用order_id获得产品计费详细信息,可以请您告诉我这个 – ali

+0

'$ item_values-> get_id()'不检索产品ID,即$ item_values-> get_product_id ()'。 – helgatheviking

+0

@helgatheviking是的,你是对的...我做了更新。 – LoicTheAztec

0

试试这个,希望它会工作

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

谢谢。

0

我对目前的答案并不满意,因为有时您需要检查多个产品。如果你对每个产品进行相同的搜索,那真是太浪费了,所以我把它变成了调度程序格式。

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

或者,...

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

在任一情况下,所分配的功能将采取order对象,而不是ID,而product_id作为参数:

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

您当然可以根据自己的喜好自定义这些输入。