How to Change or Replace Add to Cart button using Custom Button for a Spacific Category in Woocommerce.
How to Change or Replace Add to Cart button using Custom Button for a Spacific Category in Woocommerce? This question is most common for every WordPress and WooCommerce website user, developer or designer because sometimes we want change the add to cart button to a custom button like “contact us, Read More, Send Enquiry etc.” and we did not know how to change or replace it. Then we check lots of plugins for this type of functionality but some of plugin does not work and some plugin not supported in our website version.
So this post helps everone How to Change or Replace Add to Cart button using Custom Button for a Spacific Category in Woocommerce. Here’s the code below and you can copy it and paste it on your themes function.php file in the last line of your code and don’t forget to remove Php opening (<?php) and closing tag(?>). Use these tags on required places.
<?php
// Change the loop add to cart button by a custom button for specific product category
add_filter( 'woocommerce_loop_add_to_cart_link', 'filter_loop_add_to_cart_link_callback', 10, 2 );
function filter_loop_add_to_cart_link_callback( $button, $product ) {
$terms = array('category-slug1', 'category-slug2', 'etc'); // Product categories ( Ids, slugs or names )
if( has_term( $terms, 'product_cat', $product->get_id() ) ){
$button = contact_us_replacement_button();
}
return $button;
}
// Change single product add to cart button for a specific product category
add_action( 'woocommerce_single_product_summary', 'action_single_product_summary_callback', 1 );
function action_single_product_summary_callback() {
global $product;
$terms = array('category-slug1', 'category-slug2'); // Product categories ( Ids, slugs or names )
if( has_term( $terms, 'product_cat', $product->get_id() ) ){
// For variable product types
if( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
remove_action('woocommerce_single_variation', 'woocommerce_template_single_price', 30 );
add_action( 'woocommerce_single_variation', 'contact_us_replacement_button', 20 );
}
// For all other product types
else {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action('oocommerce_single_product_summary', 'woocommerce_template_single_price', 30 );
add_action( 'woocommerce_single_product_summary', 'contact_us_replacement_button', 30 );
}
}
}
// The Function for "Custom Button" replacement
function contact_us_replacement_button(){
$text = __( "Please Call Store", "woocommerce" );
// Output
echo '<a class="custom_call_button" href="#">' . $text . '</a>';
}
?>
Hope this article helps you “How to Change or Replace Add to Cart button using Custom Button for a Spacific Category in Woocommerce.” and You can share this article with your friends so that they not stuck in making this type of functionality.