2013-08-02 25 views
1

我目前正在使用Woocommerce。下面的代碼目前添加的變化價格下拉的產品頁面上:woocommerce版本 - 將post_meta添加到下拉菜單

function display_price_in_variation_option_name($term) { 
global $wpdb, $product; 

$result = $wpdb->get_col("SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'"); 

$term_slug = (!empty($result)) ? $result[0] : $term; 


$query = "SELECT postmeta.post_id AS product_id 
      FROM {$wpdb->prefix}postmeta AS postmeta 
       LEFT JOIN {$wpdb->prefix}posts AS products ON (products.ID = postmeta.post_id) 
      WHERE postmeta.meta_key LIKE '_wholesale_price%' 
       AND postmeta.meta_value = '$term_slug' 
       AND products.post_parent = $product->id"; 

$variation_id = $wpdb->get_col($query); 

$parent = wp_get_post_parent_id($variation_id[0]); 

if ($parent > 0) { 
    $_product = new WC_Product_Variation($variation_id[0]); 
    return $term . ' (' . woocommerce_price($_product->get_price()) . ')'; 
} 
return $term; 

} 

我想編輯下面一行:

return $term . ' (' . woocommerce_price($_product->get_price()) . ')'; 

而不是它在價格上拉,我想顯示已保存的custom_meta。

我試着用替換上面:

return $term . ' (' . get_post_meta(get_the_ID(), '_wholesale_price', true) . ')'; 

但它沒有返回。有沒有人知道實現這一目標的正確方法?

回答

0

我manged做來解決這個如下:

return $term . ' (' . $value['data']->variation_id, '_wholesale_price', true) . ')'; 
+0

你知道$ value變量的來源嗎?它沒有在您的代碼中設置,並且不包含在全局範圍內。 – jmotes

+0

此外,我認爲你在這裏錯過了get_post_meta調用。 – jmotes

0

如果數據不在循環中,您需要先獲取數據。試試這個:

global $post; 
return $term . ' (' . get_post_meta($post->ID, '_wholesale_price', true) . ')'; 
+0

喜的變化ID,這並不工作。這將返回主要的帖子ID。 – danyo

相關問題