2017-08-18 6 views
0

:私は、製品の名前を得た上記のコードでWC_Orderはget_items(でプロダクトIDを返しません)

if ($query->have_posts()) { 
    $order_id = $query->posts[0]->ID; 
    $order = new WC_Order($order_id); 
    $items = $order->get_items(); 
} 

    foreach ($items as $item) { 
     $product_id = $item['product_id']; 
     $product = wc_get_product($item_id); 
     $product_name = $item['name']; 
    } 

が、 $ product_idの場合は0が返されます。これには他の方法がありますか?

解決策が見つかりません。

私の編集されたバージョン:

私はこの試みた:あなたの

WC_Order_Item_Product Object 
(
    [extra_data:protected] => Array 
     (
      [product_id] => 0 
      [variation_id] => 0 
      [quantity] => 1 
      [tax_class] => 
      [subtotal] => 0 
      [subtotal_tax] => 0 
      [total] => 0 
      [total_tax] => 0 
      [taxes] => Array 
       (
        [subtotal] => Array 
         (
         ) 

        [total] => Array 
         (
         ) 

       ) 

     ) 

    [data:protected] => Array 
     (
      [order_id] => 684 
      [name] => Dark Skirt "Erebos" - Large, White 
      [product_id] => 0 
      [variation_id] => 0 
      [quantity] => 1 
      [tax_class] => 
      [subtotal] => 19 
      [subtotal_tax] => 0 
      [total] => 19 
      [total_tax] => 0 
      [taxes] => Array 
       (
        [total] => Array 
         (
         ) 

        [subtotal] => Array 
         (
         ) 

       ) 

     ) 

    [cache_group:protected] => order-items 
    [meta_type:protected] => order_item 
    [object_type:protected] => order_item 
    [id:protected] => 1 
    [changes:protected] => Array 
     (
     ) 

    [object_read:protected] => 1 
    [default_data:protected] => Array 
     (
      [order_id] => 0 
      [name] => 
      [product_id] => 0 
      [variation_id] => 0 
      [quantity] => 1 
      [tax_class] => 
      [subtotal] => 0 
      [subtotal_tax] => 0 
      [total] => 0 
      [total_tax] => 0 
      [taxes] => Array 
       (
        [subtotal] => Array 
         (
         ) 

        [total] => Array 
         (
         ) 

       ) 

     ) 

    [data_store:protected] => WC_Data_Store Object 
     (
      [instance:WC_Data_Store:private] => WC_Order_Item_Product_Data_Store Object 
       (
        [internal_meta_keys:protected] => Array 
         (
          [0] => _order_id 
          [1] => _name 
          [2] => _product_id 
          [3] => _variation_id 
          [4] => _quantity 
          [5] => _tax_class 
          [6] => _subtotal 
          [7] => _subtotal_tax 
          [8] => _total 
          [9] => _total_tax 
          [10] => _taxes 
          [11] => _product_id 
          [12] => _variation_id 
          [13] => _qty 
          [14] => _tax_class 
          [15] => _line_subtotal 
          [16] => _line_subtotal_tax 
          [17] => _line_total 
          [18] => _line_tax 
          [19] => _line_tax_data 
         ) 

        [meta_type:protected] => order_item 
        [object_id_field_for_meta:protected] => order_item_id 
       ) 

      [stores:WC_Data_Store:private] => Array 
       (
        [coupon] => WC_Coupon_Data_Store_CPT 
        [customer] => WC_Customer_Data_Store 
        [customer-download] => WC_Customer_Download_Data_Store 
        [customer-session] => WC_Customer_Data_Store_Session 
        [order] => WC_Order_Data_Store_CPT 
        [order-refund] => WC_Order_Refund_Data_Store_CPT 
        [order-item] => WC_Order_Item_Data_Store 
        [order-item-coupon] => WC_Order_Item_Coupon_Data_Store 
        [order-item-fee] => WC_Order_Item_Fee_Data_Store 
        [order-item-product] => WC_Order_Item_Product_Data_Store 
        [order-item-shipping] => WC_Order_Item_Shipping_Data_Store 
        [order-item-tax] => WC_Order_Item_Tax_Data_Store 
        [payment-token] => WC_Payment_Token_Data_Store 
        [product] => WC_Product_Data_Store_CPT 
        [product-grouped] => WC_Product_Grouped_Data_Store_CPT 
        [product-variable] => WC_Product_Variable_Data_Store_CPT 
        [product-variation] => WC_Product_Variation_Data_Store_CPT 
        [shipping-zone] => WC_Shipping_Zone_Data_Store 
       ) 

      [current_class_name:WC_Data_Store:private] => WC_Order_Item_Product_Data_Store 
      [object_type:WC_Data_Store:private] => order-item-product 
     ) 

    [meta_data:protected] => Array 
     (
     ) 

) 

は、[データ:保護]を参照してください。私はこの配列だ

$order_id = $query->posts[0]->ID; 
$order = new WC_Order($order_id); 
$items = $order->get_items(); 

foreach ($items as $item) { 
    $item_id = $item['product_id']; 
    $product_name = $item['name']; 
    print_r($item); exit; 
} 

を配列し、あなたが見ることができますこれは[product_id]が0であるところです。それは私が推測する問題です。しかし、これのための解決策は何ですか?

+0

私はこれを試してみましたが、これも機能していません。私の編集されたvesionの上を参照してください。 – Ranjit

+0

保護されたデータにアクセスするには '$ item-> get_data();'メソッドを使用してください... [WoCommerce 3のWooCommerceの注文の詳細を取得する方法](https://stackoverflow.com/questions/39401393/how-to) -get-woocommerce-order-details/44708344#44708344)...これはアクセス可能な配列(保護されていない)を返します – LoicTheAztec

答えて

0

それは次のようになります。

foreach ($items as $item) { 
    $product = $item->get_product(); 
    $product_id = $product->get_id(); 
} 
+0

"get_id()メンバ関数への呼び出し"のような致命的なエラーが発生しています。だから、それは動作していないと私は前にこれを試してみました。前にこれを試しましたか? – Ranjit

+0

はい...どのバージョンがあなたのwoocommerce @Ranjitですか? 3.0より下の場合は、代わりに '$ product-> id'を使用してください – Reigel

+0

最新(バージョン3.1.2)です。まだ動かない。 – Ranjit

関連する問題