在Woocommerce中的“单一产品简短描述"下添加文本 [英] Add Text under Single Product Short Description in Woocommerce

查看:328
本文介绍了在Woocommerce中的“单一产品简短描述"下添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to add some Global Text immediately under the Product Short Description in Woo Commerce, before the product options.

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

I can change the file directly, but of course as soon as it updates it gets over written.

还有另一种方法吗?

推荐答案

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

Update 2: There is 3 different ways, using hooks:

1)在简短描述内容的底部添加您的自定义文本,(不适用于可变产品):

1) Adding your custom text at the end of the short description content, (not for variable products):

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时,显然也对产品版本描述有效,并且不应 (因为在开发人员文档中未对此进行记录) 解决方案如下:

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)在简短说明内容的末尾添加您的自定义文本,针对所有产品类型:

2) Adding your custom text at the end of the short description content, for all product types:

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)在简短说明后添加您的自定义文本:

3) Adding your custom text after the short description:

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文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于在Woocommerce中的“单一产品简短描述"下添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆