避免简短描述附加文本出现在 Woocommerce 产品变体描述中

Avoid short description additional text to appear on Woocommerce product variations description

我想在适用于我的大多数产品的描述之后显示关于我所有产品的消息。但是,问题在于,对于可变产品,该消息会同时显示在产品的整体描述中以及选择变体时。

所以我不希望在选择变体时出现额外的文本,所以我修改了我的函数以添加一个 else if 语句。现在的功能如下:

add_filter('woocommerce_short_description','ts_add_text_short_descr');
function ts_add_text_short_descr($description){
    global $post;
    global $product;
    

    // Don't want the message if the product is in these specific categories
    if ( has_term( "training-courses-v2", "product_cat", $post->ID )  ||  has_term( "online-training-courses", "product_cat", $post->ID ) ) {
         return $description;
   }
    else if ( $product->is_type( 'variation' ) ) {
        return $description;
    }
    else {
         $text="<strong>Please note that as this is a hygiene product, only unopened products in their original, unopened condition and in their original packaging are eligible for a refund.</strong>";
    return $description.$text;
   }    
}

但是,这仍然不起作用,文本出现在两个地方。我也尝试将产品类型更改为可变的,但是消息没有出现在任何地方。

有什么方法可以让我在产品变体时不添加消息?

使用以下方法避免将额外的文本添加到可变产品的每个变体描述中:

add_filter( 'woocommerce_short_description', 'ts_add_text_short_descr' );
function ts_add_text_short_descr( $description ){
    global $post, $product;

    $product_id = is_a($product, 'WC_Product') ? $product->get_id() : get_the_id();

    // Don't want the message if the product is in these specific categories
    if ( ! has_term( array("training-courses-v2", "online-training-courses"), "product_cat", $product_id ) ) {
        $description .= "<strong>Please note that as this is a hygiene product, only unopened products in their original, unopened condition and in their original packaging are eligible for a refund.</strong>";
    }
    return $description;
}

add_filter( 'woocommerce_available_variation', 'filter_wc_available_variation_desscription', 10, 3);
function filter_wc_available_variation_desscription( $data, $product, $variation ) {
    if ( ! has_term( array("training-courses-v2", "online-training-courses"), "product_cat", $product->get_id() ) ) {
        $data['variation_description'] = get_post_meta($variation->get_id(), '_variation_description', true);
    }

    return $data;
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。