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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?