将自定义内容添加到 WooCommerce 产品描述

我正在尝试在我的描述结尾插入一些文本.可以用过滤器吗?

I'm trying to inject some text in my description ending. Is it possible with filter?

或者我需要通过子主题来做到这一点吗?一直试图找到用于描述的钩子,但只能找到一个用于简短描述的钩子.示例:

Or do i need to do this via child theme? Been trying to find the hook for description but can only find one for short description. Example:

这是说明.

只是一些示例文本来填写说明.

Just some sample text to fill the description out.

我想要的是注入这是描述中的最后一行"所以孔描述看起来像这样.

What i want is to inject "This is the last line in the description" So the hole description would look like this.

这是说明.

只是一些示例文本来填写说明.

Just some sample text to fill the description out.

这是描述的最后一行

我在简短描述之前注入文本的代码是这样的:

The code i have for injecting text before short description is this:

add_filter( 'woocommerce_short_description', 'single_product_short_descriptions', 10, 1 );
function single_product_short_descriptions( $post_excerpt ){
    global $product;

    if ( is_single( $product->id ) )
        $post_excerpt = '<div class="product-message"><p>' . __( "Article only available in the store.", "woocommerce" ) . '</p></div>' . $post_excerpt;

    return $post_excerpt;
}

推荐答案

你可以使用这个钩在 the_content 过滤器钩子中的自定义函数:

You can use this custom function hooked in the_content filter hook this way:

add_filter( 'the_content', 'customizing_woocommerce_description' );
function customizing_woocommerce_description( $content ) {

    // Only for single product pages (woocommerce)
    if ( is_product() ) {

        // The custom content
        $custom_content = '<p class="custom-content">' . __("This is the last line in the description", "woocommerce").'</p>';

        // Inserting the custom content at the end
        $content .= $custom_content;
    }
    return $content;
}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

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

添加 - 为空时强制产品描述(如果您希望显示此自定义文本):

Addition - Force product description when is empty (if you want this custom text to be displayed):

add_filter( 'woocommerce_product_tabs', 'force_description_product_tabs' );
function force_description_product_tabs( $tabs ) {

    $tabs['description'] = array(
        'title'    => __( 'Description', 'woocommerce' ),
        'priority' => 10,
        'callback' => 'woocommerce_product_description_tab',
    );

    return $tabs;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

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

相关文章