简体   繁体   English

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

[英]Avoid short description additional text to appear on Woocommerce product variations description

I want to show a message on all my products after the description which works on most of my products.我想在适用于我的大多数产品的描述之后在我的所有产品上显示一条消息。 However, the problem is that on variable products, the message would show both on the overall description of the product AND when a variant was selected.但是,问题在于,在可变产品上,该消息将同时显示在产品的整体描述和选择变体时。

So I don't want the additional text when the variant is chosen so I amended my function to add an else if statement. So I don't want the additional text when the variant is chosen so I amended my function to add an else if statement. The function is now as follows: function现在如下:

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;
   }    
}

However, this still doesn't work and the text appears in both places.但是,这仍然不起作用,并且文本出现在两个地方。 I tried also changing the product type to be variable but then the message doesn't appear in either place.我也尝试将产品类型更改为变量,但随后消息都没有出现在任何地方。

Is there a way I can get it so the message doesn't get added when the product is a variation?有没有办法让我在产品是变体时不会添加消息?

Use the following to avoid your additional text to be added to each variations description of a variable product:使用以下内容可避免将附加文本添加到可变产品的每个变体描述中:

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;
}

Code goes in functions.php file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的functions.php 文件。 Tested and works.测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM