![](/img/trans.png)
[英]Display a custom field under short description in Woocommerce single product pages
[英]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;
}
重要 - 对于可变产品:
我发现在使用过滤器woocommerce_short_description
时存在类似的错误,该错误显然对产品变体描述也有效,但不应该(因为开发人员文档中没有记录) ......解决方案如下:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.