【问题标题】:Append related product categories description to product description in Woocommerce将相关产品类别描述附加到 Woocommerce 中的产品描述
【发布时间】:2018-03-20 18:15:08
【问题描述】:

我无法在每件商品下方的“描述”标签中为每件商品添加商品类别描述的末尾处。到目前为止,我的函数(在底部)放置了所有类别描述,而不是产品实际所在的一个类别。

Here's the example 带有描述页面的类别。

Sample product page with description (开始于:Stories 美国原住民的故事和传说传统上服务... ...)

这是我用来组合两个函数的代码。它可以添加所有类别描述,但我试图让它只显示一个相关描述:

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

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

        // The custom content
        $args  = array( 'taxonomy' => 'product_cat' );
        $terms = get_terms('product_cat', $args);

        $count = count($terms); 
        // if ($count > 0) {

            foreach ($terms as $term) {
                /*  echo $term->description;*/
                $custom_content = '<p class="custom-content">' . __($term->description, "woocommerce").'</p>';

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

https://pastebin.com/GHBxe23i

到目前为止,我正在为 wordpress 实现一个非破坏性 php 插件中的代码。

【问题讨论】:

    标签: php wordpress woocommerce product wordpress-hook


    【解决方案1】:

    您应该将代码中的get_terms() 替换为wp_get_post_terms(),这样:

    add_filter( 'the_content', 'woocommerce_custom_product_description', 20, 1 );
    function woocommerce_custom_product_description( $content ) {
        global $post;
    
        // Only for woocommerce single product pages
        if ( ! is_product() ) 
            return $content; // exit
    
        $terms =  wp_get_post_terms( $post->ID, 'product_cat' );
        if ( count( $terms ) == 0 ) 
            return $content; // exit
    
        foreach ( $terms as $term )
            $content .= '<p class="custom-content">' . $term->description . '</p>';
    
        return $content;
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 2018-07-27
      • 2021-10-09
      • 1970-01-01
      • 2020-06-08
      • 1970-01-01
      • 2016-04-14
      相关资源
      最近更新 更多