douzhi4056 2018-07-25 15:01
浏览 121
已采纳

在Woocommerce中的functions.php文件中显示添加到购物车按钮的价格

Been trying to find something about this, but the solutions I found so far here do not work.

I need, in the single product page, to display the add to cart button like this:

ADD TO CART - JUST $PRICE

It needs to be done from the functions.php file in the child theme.

Thanks a lot!

  • 写回答

3条回答 默认 最新

  • duankui3838 2018-07-29 20:02
    关注

    To handle the display of the product price in add-to-cart button for all product prices on shop, other archives pages and single product pages, without any need of overriding templates, using dedicated Woocommerce filter hooks:

    add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 ); // Shop and other archives pages
    add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 ); // Single product pages
    function custom_add_to_cart_price( $button_text, $product ) {
        // Variable products
        if( $product->is_type('variable') ) {
            // shop and archives
            if( ! is_product() ){
                $product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_variation_price() ) ) );
                return $button_text . ' - From ' . strip_tags( $product_price );
            } 
            // Single product pages
            else {
                return $button_text;
            }
        } 
        // All other product types
        else {
            $product_price = wc_price( wc_get_price_to_display( $product ) );
            return $button_text . ' - Just ' . strip_tags( $product_price );
        }
    }
    

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


    On shop page:

    enter image description here

    On single product pages:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 帮我调试idea基于spring boot项目
  • ¥15 es 7.17.20版本生成时间戳
  • ¥15 wpf dategrid表头排序图标自定义
  • ¥15 分析下图所示同步时序逻辑电路的逻辑功能。
  • ¥15 halcon联合c#遇到了问题不能解决
  • ¥15 xshell无法连接提示ssh服务器拒绝密码
  • ¥15 AT89C52单片机C语言关于串口通信的位操作
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 灰狼算法和蚁群算法如何结合