我目前正在与 Woocommerce 合作。以下代码当前将变体价格添加到产品页面的下拉列表中:
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 ) . ')';
但它什么也不返回。有谁知道实现这一目标的正确方法?