【发布时间】:2017-05-16 03:13:34
【问题描述】:
我正在使用最新的 WC,我真的希望价格显示在每个变体旁边,以便更好地了解。
我无法为我的 functions.php 找到合适的代码来在每个变体旁边显示价格。我看到了几个较旧的帖子,但没有一个真正有效。
我尝试了以下方法:
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
if ( empty( $term ) ) return $term;
if ( empty( $product->id ) ) return $term;
$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 'attribute_%'
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 . ' (' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ')';
}
return $term;
}
【问题讨论】:
-
您能否链接到其他帖子并解释为什么它们不起作用?
-
stackoverflow.com/questions/32144111/… 这个没用。在实施一种或另一种方式后,我看不到任何变化。但它适用于旧版本的 WC,尽管我开始相信我的主题以某种方式阻止了它。我正在使用 Shopera。
标签: php wordpress variations