2016-05-26 21 views
1

こんにちは私は、各注文から製品ID、製品バリエーションID、カスタムフィールドテキストを取得する必要があります。私が使用して、すべてのカスタムフィールドのテキスト入力データを除いて取得した:他のすべてのパラメータがうまくいくのに対しWordpress WooCommerceがカスタムデータを取得

$product_description = get_post_meta($item['product_id'])->post_content 

は実際に完全にサイトを墜落しました。下記のスニペットをご覧ください:

add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1); 
function custom_process_order($order_id) 
{ 
    $order = new WC_Order($order_id); 
    $myuser_id = (int)$order->user_id; 
    $user_info = get_userdata($myuser_id); 
    $items = $order->get_items(); 
    foreach ($items as $item) 
    { 
     $product_name = $item['name']; 
     $product_id = $item['product_id']; 
     $product_variation_id = $item['variation_id']; 
     $product_description = get_post_meta($item['product_id'])->post_content 
    } 
    return $order_id; 
} 

どうしたらよいですか?

答えて

0
/** 
* 
* @param int $post_id 
* @return string 
*/ 
function get_the_content_by_id ($post_id) { 
    $content_post = get_post ($post_id); 
    $content = $content_post->post_content; 
    $content = apply_filters ('the_content', $content); 
    $content = str_replace (']]>', ']]>', $content); 
    return $content; 
} 

function.phpにこのコードを追加して、この

$product_description = get_the_content_by_id($item['product_id']);

のような使用は、だからあなたのコードは、あなたが誤ってget_post_metaを使用している

add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1); 
    function custom_process_order($order_id) { 
     $order = new WC_Order ($order_id); 
     $myuser_id = (int) $order->user_id; 
     $user_info = get_userdata ($myuser_id); 
     $items = $order->get_items(); 
     foreach ($items as $item) { 

      $product_name = $item ['name']; 
      $product_id = $item ['product_id']; 
      $product_variation_id = $item ['variation_id']; 
      $product_description = get_the_content_by_id($item['product_id']); 
     } 
     return $order_id; 
    } 
0

のようになります。あなたはPOST_CONTENTを持ちたいなら、あなたはあなたのために働くかもしれない以下のようにコードを変更する必要があります。

add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1); 
function custom_process_order($order_id) { 
    $order = new WC_Order($order_id); 
    $myuser_id = (int)$order->user_id; 
    $user_info = get_userdata($myuser_id); 
    $items = $order->get_items(); 

    foreach ($items as $item) { 
     $product_name = $item['name']; 
     $product_id = $item['product_id']; 
     $product_variation_id = $item['variation_id']; 
     $product_description = get_post_field('post_content', $product_id); 
    } 
    return $order_id; 
} 

あなたは何の疑いを持っているなら、私に教えてください。

+0

お返事ありがとうございました。 – BKCapri

+0

'print_r($ product_description)'を使ってチェックしてみましたか?あなたは 'get_post_field'関数に渡される' $ product_id'を取得していますか? –

+0

はい、私はすべての値を電子メールで送信して、product_idが問題ないようにするメール機能を追加しました – BKCapri

関連する問題