我目前正在使用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) . ')';
但它没有返回。有没有人知道实现这一目标的正确方法?
你知道$ value变量的来源吗?它没有在您的代码中设置,并且不包含在全局范围内。 – jmotes
此外,我认为你在这里错过了get_post_meta调用。 – jmotes