简体   繁体   中英

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. The function is now as follows:

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). Tested and works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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