Changing the Add to Cart Button Text in Woocommerce for Items with Variations

How to change add to cart button text for specific variable products on single page in WooCommerce

You can use show_variation trigger. try the below code.

function change_add_to_cart_text_based_on_variation(){
global $post;

$specific_ids = array( 6594, 6577 );

if( is_product() && in_array( $post->ID, $specific_ids ) ){
?>
<script type="text/javascript">
(function($){
$( ".single_variation_wrap" ).on( "show_variation", function ( event, variation ) {
if( variation.attributes['attribute_pa_choose-option'] == 'unpainted' ){
jQuery('.single_variation_wrap .single_add_to_cart_button').html( "Made to Order" );
}else{
jQuery('.single_variation_wrap .single_add_to_cart_button').html( "Add To Cart" );
}
} );

$( document ).on('click', '.reset_variations', function(event) {
event.preventDefault();
jQuery('.single_variation_wrap .single_add_to_cart_button').html("Add To Cart");
});

})(jQuery);
</script>
<?php
}
}
add_action( 'wp_footer', 'change_add_to_cart_text_based_on_variation', 10, 1 );

Tested and works.

Sample Image

Change add to cart button and text based on WooCommerce product type

Try the following instead:

add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
// Out of stock products
if( ! $product->is_in_stock() ) {
$button_text = __( "Unavailable", "woocommerce" );
}
// Simple and Variable products
elseif( $product->is_type( 'simple' ) || $product->is_type( 'variable' ) ) {
$button_text = __( "Show product", "woocommerce" );
}
// Other product types
else {
$button_text = add_to_cart_text();
}

return '<a class="view-product button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}

Code goes in functions.php file of the active child theme (or active theme). It should work

WooCommerce: Change add to cart button text for multiple products with a redirection to checkout

You can use the following for 2 different products button texts and a redirection to checkout for both (where you will define your 2 product ids):

add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_addtocart_button_text', 10, 2 ); 
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_addtocart_button_text', 10, 2 );
function custom_addtocart_button_text( $button_text, $product ) {
// 1st product
if ( $product->get_id() == 11359 ) {
$button_text = __( 'Ja ik word kompaan', 'woocommerce' );
}
// 2nd product
elseif ( $product->get_id() == 11362 ) {
$button_text = __( 'Something else', 'woocommerce' );
}
return $button_text;
}

add_filter ( 'woocommerce_add_to_cart_redirect', 'custom_redirect_to_checkout' );
function custom_redirect_to_checkout( $url ) {
if ( isset( $_POST['add-to-cart'] ) && $_POST['add-to-cart'] > 0 ) {
$product_id = intval( $_POST['add-to-cart'] );

if( in_array( $product_id, array( 11359, 11362 ) ) ){
$url = wc_get_checkout_url();
}
}
return $url;
}

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

WooCommerce - Change Add to Cart button text for products £40 or more

no need to place a function inside another. this should be enough.

  add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text', 100, 2 );

function woocommerce_custom_single_add_to_cart_text( $add_to_cart_text, $product ) {
if ( $product->get_regular_price() >= '40' ) {
return __( 'Add to Cart with FREE UK Shipping', 'woocommerce' );
}
return $add_to_cart_text;
}

WooCommerce: change the add to cart text when the product is already in cart

  • $_product->id should be $_product->get_id()
  • Use return outside the foreach loop
  • global $woocommerce is not necessary
  • Second parameter from woocommerce_product_add_to_cart_text filter hook is $product, not $product_id

So you get

function woocommerce_custom_add_to_cart_text( $add_to_cart_text, $product ) {
// Get cart
$cart = WC()->cart;

// If cart is NOT empty
if ( ! $cart->is_empty() ) {

// Iterating though each cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Get product id in cart
$_product_id = $cart_item['product_id'];

// Compare
if ( $product->get_id() == $_product_id ) {
// Change text
$add_to_cart_text = __( 'Already in cart', 'woocommerce' );
break;
}
}
}

return $add_to_cart_text;
}
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_add_to_cart_text', 10, 2 );

Change Buy button for variants in backorder Woocommerce

When a product variation is selected, a hidden input field is set in the variation form with the id of the selected product variation. For example:

<input type="hidden" name="variation_id" class="variation_id" value="44">

If the selected attributes do not match any product variation, the
value will be blank.

All possible product variations are in the data-product_variations attribute of the variation form element.

Then you can use this jQuery script to change the button name. It will be Reservar if the product variation allows backorders while Comprar if it does not.

// change the text of the add to cart button based on product backorders
add_action( 'wp_footer', 'change_the_text_of_the_add_to_cart_button' );
function change_the_text_of_the_add_to_cart_button() {

?>
<script type="text/javascript">
jQuery(function($){
$('input[name=variation_id].variation_id').change(function(){
const variationID = $(this).val();
const variationData = $('form.variations_form').data("product_variations");
$(variationData).each(function(index,variation){
if ( variationID == variation.variation_id ) {
if ( variation.backorders_allowed ) {
$('form.variations_form button[type=submit]').text('Reservar');
} else {
$('form.variations_form button[type=submit]').text('Comprar');
}
}
});
});
});
</script>
<?php

}

The code has been tested and works. Add it to your active theme's functions.php.

Change Add to cart button text for specific products in Woocommerce

The following code will change add to cart button text depending on defined product IDs:

add_filter( 'woocommerce_product_add_to_cart_text', 'custom_loop_add_to_cart_button', 20, 2 ); 
function custom_loop_add_to_cart_button( $button_text, $product ) {
// HERE define your specific product IDs in this array
$specific_ids = array(37, 40, 53);

if( in_array($product->get_id(), $specific_ids) ) {
$button_text = __("add to cart", "woocommerce");
} else {
$button_text = __('+', 'woocommerce');
}
return $button_text;
}

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



Related Topics



Leave a reply



Submit