douwan4993 2018-03-30 16:23
浏览 218
已采纳

在Woocommerce中的单个产品简短描述下添加文本

I want to add some Global Text immediately under the Product Short Description in Woo Commerce, before the product options.

I can change the file directly, but of course as soon as it updates it gets over written.

Is there another way of doing this?

  • 写回答

1条回答 默认 最新

  • dpy83214 2018-03-30 16:44
    关注

    Update 2: There is 3 different ways, using hooks:

    1) Adding your custom text at the end of the short description content, (not for variable products):

    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;
    }
    

    Important - For variable products:
    I found that there is bug like in When using the filter woocommerce_short_descriptionthat is apparently also active for the product variation description, and it should not (as this is not documented in developers documentation)The solution is below:

    2) Adding your custom text at the end of the short description content, for all product types:

    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) Adding your custom text after the short description:

    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>';
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52
  • ¥100 只改动本课件的 cal_portfolio_weight_series(decision_date), 跑完本课件。设计一个信息比率尽量高的策略。
  • ¥20 如何在visual studio 2022中添加ImageMagick库
  • ¥50 如何实现uniapp编译的微信小程序做可回溯视频
  • ¥15 求Houdini使用行家,付费。价格面议。
  • ¥15 前端高拍仪调用问题报错
  • ¥15 想用octave解决这个数学问题