Wordpress 仅获取特定类别商品的价格

Wordpress 仅获取特定类别商品的价格,wordpress,woocommerce,Wordpress,Woocommerce,我试图在Woocommerce上显示价格变化,我可以做得很好。现在,我不想在所有变体上显示价格,我只想在属于特定类别的变体旁边显示价格 这是我到目前为止提出的,从逻辑上讲是有道理的,但不是针对这个特定的类别。关于如何排除故障有什么建议吗 //get prices of variations function display_price_in_variation_option_name( $term ) { global $wpdb, $product; $result = $wpdb->

我试图在Woocommerce上显示价格变化,我可以做得很好。现在,我不想在所有变体上显示价格,我只想在属于特定类别的变体旁边显示价格

这是我到目前为止提出的,从逻辑上讲是有道理的,但不是针对这个特定的类别。关于如何排除故障有什么建议吗

//get prices of variations
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 '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] );

$itemPrice = strip_tags (woocommerce_price( $_product->get_price() ));
//this is where you can actually customize how the price is displayed
return $term . ' (' . $itemPrice . ')';
}
return $term;

//Add prices to variations

global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $termItem ) $categories[] = $termItem->slug;

if ( in_array( 'custom-pet-portrait-painting', $categories ) ) {

  add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );


} //end function

}
或者我可以使用get_术语来包装下面的代码,其中显示了价格的变化?我几乎只需要显示一个类别的变体的价格

if (has_term ('my-category', 'product_cat')) {
//get prices of variations
    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 '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] );

    $itemPrice = strip_tags (woocommerce_price( $_product->get_price() ));
    //this is where you can actually customize how the price is displayed
    return $term . ' (' . $itemPrice . ')';
    }
    return $term;
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );

} //end has_term