I am looking for a function to change the Add to Cart text on a woocommerce button but only if the product in question has a specific tag. ie if the product has the tag "preorder" the button text changes to "Pre Order Now"
Changing the text globally can be achieved with this;
http://docs.woothemes.com/document/change-add-to-cart-button-text/
Thanks.
You can check for that particular term with has_term
.
//For single product page
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' ); // 2.1 +
function woo_custom_cart_button_text() {
global $product;
if ( has_term( 'preorder', 'product_cat', $product->ID ) ) :
return __( 'Pre order Now!', 'woocommerce' );
endif;
}
//For Archive page
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' ); // 2.1 +
function woo_archive_custom_cart_button_text() {
if ( has_term( 'preorder', 'product_cat', $product->ID ) ) :
return __( 'Pre order Now!', 'woocommerce' );
else:
return __( 'Add to Cart', 'woocommerce' );
endif;
}
Let me know the output.
It's been a while, but try something along the following lines:
<?php
add_filter( 'woocommerce_product_add_to_cart_text' , 'custom_woocommerce_product_add_to_cart_text' );
/**
* custom_woocommerce_template_loop_add_to_cart
*/
function custom_woocommerce_product_add_to_cart_text() {
global $product;
$product_tag = $product->product_tag;
switch ( $product_tag ) {
case 'preorder':
return __( 'Pre order Now!', 'woocommerce' );
break;
default:
return __( 'Add to cart', 'woocommerce' );
}
}
Basically what you're trying to do is do a conditional where you check if the product tag is "preorder". I am not sure if this exact code would work if there are multiple tags.
Edit:
You might have to use this filter to change it on the product page
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );
You can fix it by adding a few lines of codes in functions.php
:
// Change the add to cart text on single product pages
add_filter( 'woocommerce_product_single_add_to_cart_text’, ‘woo_custom_cart_button_text' ); // 2.1 +
function woo_custom_cart_button_text() {
return __( 'ADD TO CART', 'woocommerce' );
}
//Change the add to cart text on product archives
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' ); // 2.1 +
function woo_archive_custom_cart_button_text() {
return __( 'ADD TO CART', 'woocommerce' );
}
http://visupporti.com/woocommerce-how-to-change-add-to-cart-text/