【问题标题】:Product id return empty in WC_Order - WooComerceWC_Order 中的产品 ID 返回空 - WooCommerce
【发布时间】:2017-07-22 15:29:10
【问题描述】:

我使用的是 WooComerce 插件版本 2.6.11,而 WordPress 版本是 4.7.2。我正在尝试获取所有带有订单产品元数据的产品(order_id=273)。我尝试如下。但$item['product_id'] 返回空。什么都没有返回。

    function custom_get_order_details($order_id){
      global $woocommerce; 


      // for test im using $order_id = 273
      $order = new WC_Order( $order_id );

      foreach ( $order->get_items() as $item_id => $item ){

          print_r($item_id);
          echo $product_id = wc_get_order_item_meta($item_id, '_product_id', true);
          echo "<br>test_30<br>";
          echo $variation_id = wc_get_order_item_meta($item_id, '_variation_id', true);
          echo $quantity = wc_get_order_item_meta($item_id, '_qty', true);

      }
      //$curl = curl_init();
      //curl post here
    }
    add_action( 'woocommerce_payment_complete', 'custom_get_order_details', 10, 1 );

如果我在 for 循环中打印 $item。它打印

array(4) { ["name"]=> string(4) "Test" ["type"]=> string(9) "line_item" ["item_meta"]=> NULL ["item_meta_array"]=> array(9) { [1]=> object(stdClass)#823 (2) { ["key"]=> string(4) "_qty" ["value"]=> string(1) "1" } [2]=> object(stdClass)#822 (2) { ["key"]=> string(10) "_tax_class" ["value"]=> string(0) "" } [3]=> object(stdClass)#821 (2) { ["key"]=> string(11) "_product_id" ["value"]=> string(4) "2856" } [4]=> object(stdClass)#820 (2) { ["key"]=> string(13) "_variation_id" ["value"]=> string(1) "0" } [5]=> object(stdClass)#819 (2) { ["key"]=> string(14) "_line_subtotal" ["value"]=> string(3) "100" } [6]=> object(stdClass)#818 (2) { ["key"]=> string(11) "_line_total" ["value"]=> string(3) "100" } [7]=> object(stdClass)#817 (2) { ["key"]=> string(18) "_line_subtotal_tax" ["value"]=> string(1) "0" } [8]=> object(stdClass)#816 (2) { ["key"]=> string(9) "_line_tax" ["value"]=> string(1) "0" } [9]=> object(stdClass)#815 (2) { ["key"]=> string(14) "_line_tax_data" ["value"]=> string(45) "a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}" } } } 

也返回空 --$order-&gt;get_item_meta($item_id, '_qty', true)

我不知道这有什么问题。

【问题讨论】:

    标签: php wordpress woocommerce wordpress-theming product


    【解决方案1】:

    您可以通过以下方式获取订单的订单商品详情

       // Getting an instance of the order object
    
        $order = new WC_Order( $order_id );
        $items = $order->get_items();
    
       //Loop through them, you can get all the relevant data:
    
        foreach ( $items as $item ) {
            $product_name = $item['name'];
            $product_id = $item['product_id'];
            $product_variation_id = $item['variation_id'];
        }
    

    【讨论】:

    • 我不在模板文件中工作。我正在使用此代码functions.php
    • @Vel 请粘贴您的 sn-p 以便我查看?
    • @vel 如果你得到 print_r($item_id);那么你肯定可以得到价值。您的 sn-p 中没有 return 语句。您只需要以数组或第一个产品 id 的形式返回整体吗?
    • 我已经通过其他方式解决了这些问题。 3个月前我问过这个问题。感谢您的回复。
    【解决方案2】:

    应该是$item_data 而不是$item,有错字。

    echo '$item["product_id"] <i>(product ID)</i>: ' . $item_data['product_id'] . '<br>'; //<--- notice $item_data
    echo '$item["name"] <i>(product Name)</i>: ' . $item_data['name'] . '<br>'; //<--- notice $item_data
    

    更新 v2

    您也可以像这样从item_meta 密钥中获取product_id

    foreach ($items as $item_id => $item) {
        //..
        echo $item['item_meta']['_product_id'][0];
        echo $item['item_meta']['_qty'][0];
        //...
    }
    

    希望这会有所帮助!

    【讨论】:

    • 我更新了我的问题。没有运气。我将 $item_data 更改为 $item。不工作。
    • $item['name'] 是返回产品名称。但产品 ID 返回为空。
    • 我已经更新了我的答案。并且您的var_dump 中缺少product_id 密钥,因此您可以从item_meta 中获取它。
    • 运气不好@raunakgupta。
    • 我在问题部分添加了图片
    猜你喜欢
    • 2012-09-15
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 2015-01-12
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多