Display price on add to cart button from the funct

2019-05-26 13:51发布

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条回答
等我变得足够好
2楼-- · 2019-05-26 14:02

Edit your woocommerce/loop/add-to-cart.php template file like this:

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

global $product;

echo apply_filters( 'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok.
    sprintf( '<a href="%s" data-quantity="%s" class="%s" %s>ADD TO CART - JUST %s</a>',
        esc_url( $product->add_to_cart_url() ),
        esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
        esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
        isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
        $product->get_price()
    ),
$product, $args );

For the single product pages do basically the same in:

woocommerce/single-product/add-to-cart/simple.php

And any other template you use in the add-to-cart directory.

查看更多
做自己的国王
3楼-- · 2019-05-26 14:06

I would suggest you override the WooCommerce template from the child theme, the file name is add-to-cart.php and can be found at woocommerce > loop.

At the bottom add the following code, it will be displayed only in product single.

if (is_single()) {
    echo sprintf(
        '<span>%s %s</span>',
        esc_attr__('JUST', 'woocommerce'),
        esc_attr($product->price)
    );
}
查看更多
太酷不给撩
4楼-- · 2019-05-26 14:21

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

查看更多
登录 后发表回答