![](/img/trans.png)
[英]Display In stock available variations in WooCommerce single product short description
[英]Avoid short description additional text to appear on Woocommerce product variations description
我想在適用於我的大多數產品的描述之后在我的所有產品上顯示一條消息。 但是,問題在於,在可變產品上,該消息將同時顯示在產品的整體描述和選擇變體時。
So I don't want the additional text when the variant is chosen so I amended my function to add an else if statement. 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;
}
}
但是,這仍然不起作用,並且文本出現在兩個地方。 我也嘗試將產品類型更改為變量,但隨后消息都沒有出現在任何地方。
有沒有辦法讓我在產品是變體時不會添加消息?
使用以下內容可避免將附加文本添加到可變產品的每個變體描述中:
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 文件。 測試和工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.