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条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型