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

Add custom content to WooCommerce product description

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

或者我需要通过儿童主题来做这件事吗? 一直试图找到描述的钩子,但只能找到一个简短的描述。 示例:

This is a description.

Just some sample text to fill the description out.

我想要的是注入"This is the last line in the description"所以洞的描述应该是这样的。

This is a description.

Just some sample text to fill the description out.

This is the last line in the description

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

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 过滤器挂钩中的自定义函数:

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 文件。已测试并有效。


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

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 文件。已测试并有效。