我想在 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;
}



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/

10-12 12:40
查看更多