向WooCommerce产品说明添加自定义内容 [英] Add custom content to WooCommerce product description

查看:330
本文介绍了向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:

这是一个描述.

This is a description.

只需一些示例文本即可填写说明.

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.

这是一个描述.

This is a description.

只需一些示例文本即可填写说明.

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.

添加-在空的(如果要显示此自定义文本)时强制产品说明:

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.

这篇关于向WooCommerce产品说明添加自定义内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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