4

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!

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Juan
  • 143
  • 2
  • 12
  • Anyone else, is there a way to add the price in the Add to Cart button? – Juan Jul 25 '18 at 20:02
  • So, still have not sorted this out. How to display the price in the add to cart button (wooommerce) – Juan Jul 29 '18 at 02:39

3 Answers3

4

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks! Useful too! I hope this thread helps future developers/designers. I could not find a proper solution before, and now we have two differente ones :) – Juan Jul 29 '18 at 21:21
  • @Juan You asked in this question for a solution based on code to be added in function.php file… My answer here is the only one that does it. The other answers are overriding templates and don't care about product types... Entonces esta es la buena :) – LoicTheAztec Jul 30 '18 at 03:34
  • totally true, I had another thread withut the "functions.php" requirement and thought that was it. Changing it :) – Juan Jul 30 '18 at 17:08
2

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)
    );
}
Riad Citaku
  • 349
  • 1
  • 9
  • Hey! Thanks, but it's not working, actually I can´t override the main add_to_cart.php. I've placed the new file with your code under my_child_theme/woocommerce/loop/add_to_cart.php, and it does not seem to work. – Juan Jul 25 '18 at 16:17
  • That's strange, it should have worked. Maybe it's the cache doing its work? Also, go to WooCommerce > Status and see if the template is being overridden or not. – Riad Citaku Jul 25 '18 at 16:24
  • It is (apparently) Anulaciones twentyseventeen-child/woocommerce/loop/add-to-cart.php – Juan Jul 25 '18 at 16:31
  • Are you sure that you're checking the product page? Because this code is applying only there. – Riad Citaku Aug 06 '18 at 16:43
0

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.

Note: Don't edit the plugin file itself but override the template in your (child) theme so it won't be overwritten when the plugin gets updated.

jrswgtr
  • 2,287
  • 8
  • 23
  • 49
  • Hello and thanks! But it doesn't work. I've edited the add-to-cart.php template. Placed it under mychild-theme/woocommerce/loop/add-to-cart.php The main file is being overriden. I've deleted all the function.php customizations to see if there was something preventing your code from working. Still nothing. I can't believe this! – Juan Jul 29 '18 at 15:06
  • @Juan you want to change the button in the loop right? or do you want to change them at the single product view? – jrswgtr Jul 29 '18 at 15:08
  • OMG! Just realized that your code worked on the archive page. I am so sorry, I just want this on the single product page. Really sorry. – Juan Jul 29 '18 at 15:12
  • @Juan I will add something to my answer. – jrswgtr Jul 29 '18 at 15:13
  • ok! I'll keep your contact also, since I am a Conversion Optimization guy, not a dev and I need help from time to time. If you have a mail, please feel free to share it with me. – Juan Jul 29 '18 at 15:17
  • You should never edit the plugin files directly. When you update the plugin, it will overwrite your changes. This is why hooks and filters exists. – Sillo Jan 24 '21 at 15:29
  • @Sillo Well it doesn't if you overwrite the template file in your (child)theme and don't edit the plugin file itself. Maybe I should have mentioned that explicitly., added it to the answer. – jrswgtr Jan 25 '21 at 12:45
  • @jrswgtr i'd say hooks and filters when possible. Copying templates to child themes may result in outdated template files more often than hooks. But yes child theme files is definately a better choice than editing the plugin itself. – Sillo Jan 26 '21 at 03:03