在 Woocommerce 中的单个产品简短描述下添加文本
Add Text under Single Product Short Description in Woocommerce
我想在 Woo Commerce 的产品简短描述下添加一些全局文本,在产品选项之前。
我可以直接更改文件,当然,一旦更新,它就会被覆盖。
还有其他方法吗?
更新 2: 有 3 种不同的方式,使用钩子:
1) 在简短描述内容的末尾添加您的自定义文本,(不适用于可变产品):
add_filter( 'woocommerce_short_description', 'add_text_after_excerpt_single_product', 20, 1 );
function add_text_after_excerpt_single_product( $post_excerpt ){
if ( ! $short_description )
return;
// Your custom text
$post_excerpt .= '<ul class="fancy-bullet-points red">
<li>Current Delivery Times: Pink Equine - 4 - 6 Weeks, all other products 4 Weeks</li>
</ul>';
return $post_excerpt;
}
Important - For variable products:
I found that there is bug like in When using the filter woocommerce_short_description
that is apparently also active for the product variation description, and it should not (as this is not documented in developers documentation)… The solution is below:
2) 在简短描述内容的末尾添加您的自定义文本,对于所有产品类型:
add_action( 'woocommerce_single_product_summary', 'custom_single_product_summary', 2 );
function custom_single_product_summary(){
global $product;
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_single_product_summary', 'custom_single_excerpt', 20 );
}
function custom_single_excerpt(){
global $post, $product;
$short_description = apply_filters( 'woocommerce_short_description', $post->post_excerpt );
if ( ! $short_description )
return;
// The custom text
$custom_text = '<ul class="fancy-bullet-points red">
<li>Current Delivery Times: Pink Equine - 4 - 6 Weeks, all other products 4 Weeks</li>
</ul>';
?>
<div class="woocommerce-product-details__short-description">
<?php echo $short_description . $custom_text; // WPCS: XSS ok. ?>
</div>
<?php
}
3) 在简短描述后添加您的自定义文本:
add_action( 'woocommerce_before_single_product', 'add_text_after_excerpt_single_product', 25 );
function add_text_after_excerpt_single_product(){
global $product;
// Output your custom text
echo '<ul class="fancy-bullet-points red">
<li>Current Delivery Times: Pink Equine - 4 - 6 Weeks, all other products 4 Weeks</li>
</ul>';
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
这是对我有用的解决方案:
add_filter('woocommerce_short_description','ts_add_text_short_descr');
function ts_add_text_short_descr($description){
$text="Your text here";
return $description.$text;
}
我想在 Woo Commerce 的产品简短描述下添加一些全局文本,在产品选项之前。
我可以直接更改文件,当然,一旦更新,它就会被覆盖。
还有其他方法吗?
更新 2: 有 3 种不同的方式,使用钩子:
1) 在简短描述内容的末尾添加您的自定义文本,(不适用于可变产品):
add_filter( 'woocommerce_short_description', 'add_text_after_excerpt_single_product', 20, 1 );
function add_text_after_excerpt_single_product( $post_excerpt ){
if ( ! $short_description )
return;
// Your custom text
$post_excerpt .= '<ul class="fancy-bullet-points red">
<li>Current Delivery Times: Pink Equine - 4 - 6 Weeks, all other products 4 Weeks</li>
</ul>';
return $post_excerpt;
}
Important - For variable products:
I found that there is bug like in When using the filterwoocommerce_short_description
that is apparently also active for the product variation description, and it should not (as this is not documented in developers documentation)… The solution is below:
2) 在简短描述内容的末尾添加您的自定义文本,对于所有产品类型:
add_action( 'woocommerce_single_product_summary', 'custom_single_product_summary', 2 );
function custom_single_product_summary(){
global $product;
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_single_product_summary', 'custom_single_excerpt', 20 );
}
function custom_single_excerpt(){
global $post, $product;
$short_description = apply_filters( 'woocommerce_short_description', $post->post_excerpt );
if ( ! $short_description )
return;
// The custom text
$custom_text = '<ul class="fancy-bullet-points red">
<li>Current Delivery Times: Pink Equine - 4 - 6 Weeks, all other products 4 Weeks</li>
</ul>';
?>
<div class="woocommerce-product-details__short-description">
<?php echo $short_description . $custom_text; // WPCS: XSS ok. ?>
</div>
<?php
}
3) 在简短描述后添加您的自定义文本:
add_action( 'woocommerce_before_single_product', 'add_text_after_excerpt_single_product', 25 );
function add_text_after_excerpt_single_product(){
global $product;
// Output your custom text
echo '<ul class="fancy-bullet-points red">
<li>Current Delivery Times: Pink Equine - 4 - 6 Weeks, all other products 4 Weeks</li>
</ul>';
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
这是对我有用的解决方案:
add_filter('woocommerce_short_description','ts_add_text_short_descr');
function ts_add_text_short_descr($description){
$text="Your text here";
return $description.$text;
}