2017-03-02 7 views
1

私はwoocomerceプラグインバージョン2.6.11を使用していますが、wordpressバージョンは4.7.2です。注文の商品メタデータを持つすべての商品を取得しようとしています(order_id = 273)。私は以下のようにしてみました。 $item['product_id']が空に戻ります。何も帰りません。商品IDはWC_Order- woocommerceで空です

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を印刷するとします。 $order->get_item_meta($item_id, '_qty', true)

私はこれで何イムが間違って知らない - それはまた、空の返却され

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:{}}" } } } 

を印刷します。

enter image description here

enter image description here enter image description here

答えて

0

それは$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 

UPDATE v2ではまた、このことができます。この

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

希望のようitem_metaキーからproduct_idを得ることができます!

+0

順のオーダーアイテムの詳細を取得することができます。運がない。 $ item_dataを$ itemに変更しました。働いていない。 – vel

+0

$ item ['name']は製品名を返します。プロダクトIDは空に戻ります。 – vel

+0

私は私の答えを更新しました。あなたの 'var_dump'に' product_id'キーがありませんので、 'item_meta'からそれを得ることができます。 –

2

あなたは私が私の質問を更新

// 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']; 
    } 
+0

私はそのようにしました。働いていない。 – vel

+0

@Velこのコードを試しているテンプレートファイルはどれですか? –

+0

テンプレートファイルで作業していません。私はこのコードを使用していますfunctions.php – vel

関連する問題