Display Price on Add to Cart Button from the Functions.PHP File in Woocommerce

Display price on add to cart button from the functions.php file in Woocommerce

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:

Sample Image

On single product pages:

Sample Image

Display price on add to cart button only if it's higher than 0 in WooCommerce

Note the extra if conditions who check the price. Explanation via comment tags added in the code

function custom_add_to_cart_price( $button_text, $product ) {
// Product type = variable
if ( $product->is_type('variable') ) {

// NOT return true when viewing a single product.
if( ! is_product() ) {
// Get price
$price = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_price() ) );

// Greater than
if ( $price > 0 ) {
$product_price = wc_price( $price );
$button_text .= ' - From ' . strip_tags( $product_price );
}
}
} else {
// Get price
$price = wc_get_price_to_display( $product );

// Greater than
if ( $price > 0 ) {
$product_price = wc_price( $price );
$button_text .= ' - Just ' . strip_tags( $product_price );
}
}

return $button_text;
}
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_price', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_price', 10, 2 );

WooCommerce display price on add to cart button

This should work: modify the add_to_cart.php file to

echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="button %s product_type_%s">%s %s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
$product->is_purchasable() ? 'add_to_cart_button' : '',
esc_attr( $product->product_type ),
$product->get_price_html(),
esc_html( $product->add_to_cart_text() )
),
$product );

and the price will display in front of the "Add to cart" text.

EDIT: You should note that updating the woocommerce plugin will undo that and any other modification you have made to any of the files.

Replace add to cart button based on weight in WooCommerce single products

First you can simplify your code a bit like:

add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button', 10, 2 );
function replace_default_button( $button, $product ){

$weight = $product->get_weight();
preg_replace('/\D/', '', $weight);

if ( $weight > 8 ){
$button = '<a href="#" class="button alt">' . __( "Add to Quote", "woocommerce" ) . '</a>';
}
return $button;
}

Then to have something similar for single products pages use the following:

add_action( 'woocommerce_single_product_summary', 'action_single_product_summary_callback', 1 );
function action_single_product_summary_callback() {
global $product;

$weight = $product->get_weight();
preg_replace('/\D/', '', $weight);

if ( $weight > 8 ) {

// For variable product types
if( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_single_variation', 'add_to_quote_replacement_button', 20 );
}
// For all other product types
else {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'add_to_quote_replacement_button', 30 );
}
}
}

function add_to_quote_replacement_button(){
echo '<a href="#" class="button alt">' . __( "Add to Quote", "woocommerce" ) . '</a>';
}

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

Display Contact Button if Woocommerce Product Price is zero else display Add to cart button

Put this in your theme functions.php file. Thanks!

function replace_add_to_cart() {
global $product;

if( $product->get_price() == 0 ) {
$link = 'YOUR CONTACT PAGE URL';
$button_text = 'Contact Us';
} else {
$link = $product->get_permalink();
$button_text = 'Buy Now';
}

echo do_shortcode('[button link="' . esc_attr($link) . '"]'.$button_text.'[/button]');
}
add_action('woocommerce_after_shop_loop_item','replace_add_to_cart');

Display availability in add to cart button for WooCommerce simple products

Updated: Your elseif condition, never matches… Try the following instead:

add_filter( 'woocommerce_product_single_add_to_cart_text', 'add_to_cart_text_availability', 10, 2 );
add_filter( 'woocommerce_product_add_to_cart_text', 'add_to_cart_text_availability', 10, 2 );
function add_to_cart_text_availability( $add_to_cart_text, $product ) {
if ( $product->is_type( 'simple' ) ) {
if ( $product->is_in_stock() ) {
$add_to_cart_text = sprintf(
__("Buy Now (%s Available)", "woocommerce"),
$product->get_stock_quantity()
);
} else {
$add_to_cart_text = __('SOLD OUT', 'woocommerce');
}
}
return $add_to_cart_text;
}

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



Related Topics



Leave a reply



Submit