gpt4 book ai didi

php - 在 Woocommerce 中的单个产品简短描述下添加文本

转载 作者:行者123 更新时间:2023-12-03 23:05:13 28 4
gpt4 key购买 nike

我想在 Woo Commerce 的产品简短描述下,在产品选项之前添加一些全局文本。

我可以直接更改文件,但当然,一旦它更新它就会被覆盖。

还有另一种方法吗?

最佳答案

更新 2:有 3 种不同的方式,使用钩子(Hook):

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_descriptionthat 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 文件中。测试和工作。

关于php - 在 Woocommerce 中的单个产品简短描述下添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49577834/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com