在Woocommerce中的“单一产品简短描述”下添加文本

Den*_*eld 2 php wordpress product woocommerce hook-woocommerce

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

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

还有另一种方法吗?

Loi*_*tec 5

更新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;
}
Run Code Online (Sandbox Code Playgroud)

重要提示-对于可变产品:
我发现存在类似错误,例如使用过滤器时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
}
Run Code Online (Sandbox Code Playgroud)

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>';
}
Run Code Online (Sandbox Code Playgroud)

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。