【问题标题】:Get the product ID in WooCommerce 3+ Order items在 WooCommerce 3+ 订单项目中获取产品 ID
【发布时间】:2017-07-15 18:21:00
【问题描述】:

我正在尝试获取 woocommerce 感谢页面 order_id。使用下面的代码。 但不幸的是我无法得到它。

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

【问题讨论】:

    标签: php wordpress woocommerce product orders


    【解决方案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

    【讨论】:

    • 感谢它的工作,我如何使用 order_id 获取产品账单详细信息,请告诉我这个
    • $item_values->get_id() 不检索产品 ID,即$item_values->get_product_id()
    • @helgatheviking 是的,你是对的……我更新了。
    • @ali 我已经更新了我的代码……要使用 product_id 获取账单详情,请参阅:WOOCOMMERCE ORDERS DETAIS IN VERSION 3.0+
    【解决方案2】:

    我对当前的答案不满意,因为有时您需要检查多个产品。如果对每个产品都做同样的搜索,那真的很浪费,所以我把它变成了dispatcher格式。

    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 object,而不是 id,并将 product_id 作为参数:

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

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

    【讨论】:

      【解决方案3】:

      试试这个,希望它会工作

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

      谢谢。

      【讨论】:

        猜你喜欢
        • 2017-09-25
        • 2021-04-23
        • 1970-01-01
        • 2018-12-09
        • 2020-12-09
        • 2019-09-08
        • 2018-03-25
        • 2019-08-29
        • 2021-12-06
        相关资源
        最近更新 更多