Many individuals are using WooCommerce to establish their online stores. However, customizing the add to cart functionality can be challenging without the proper knowledge of the right hooks and filters. Despite the large community on StackOverflow and other, there are limited solutions for removing or altering the text of the add-to-cart button.
If you’re one of these individuals and want to disable or remove the add to cart button from archive or single product pages, or if you’re seeking to change the default “Add to Cart” text to something like “Buy Now”, “Add to Bag”, or “Book Now”, this code snippet will assist you in achieving your desired result. This can be applied to update the text for simple, grouped, external, and variable buttons on both the showcase page and the single product page.
Here’s a code snippet to help you get started.
Remove Add to Cart Buttons in WooCommerce
With just a few lines of code, you can remove the “Add to Cart” buttons from your eCommerce website, or on individual products.
To implement this change, add the following code to your theme’s functions.php file:
// Remove "Add to Cart" button on category and shop pages remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart'); // Remove "Add to Cart" button on single product page remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
If you want to disable the Add to Cart button only for non-admin users, you use this instead:
// Remove shoopping cart for non-admin users / normal site visitors if ( ! current_user_can( 'administrator' ) ) { remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart'); remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 ); }
How to change Add to cart button text in WooCommerce
To change the default text of the “Add to Cart” button, use the following code inside theme file:
add_filter( 'woocommerce_product_add_to_cart_text' , 'shoper_woocommerce_product_add_to_cart_text' ); add_filter( 'woocommerce_product_single_add_to_cart_text', 'shoper_woocommerce_product_add_to_cart_text' ); add_filter( 'woocommerce_booking_single_add_to_cart_text', 'shoper_woocommerce_product_add_to_cart_text' ); function shoper_woocommerce_product_add_to_cart_text( $text ) { global $product; if (!isset ($product) || !is_object ($product)) return $text; $product_type = $product->get_type(); if (is_product()) { switch ( $product_type ) { case 'simple': return 'Simple add to cart' ; break; case 'grouped': return 'Wholesale order' ; break; case 'external': return 'Affiliate link'); break; case 'variable': return 'Choose swatch'; break; default: return __( 'Read more', 'shoper' ); } } else { switch ( $product_type ) { case 'simple': return 'Simple add to cart' ; break; case 'grouped': return 'Wholesale order' ; break; case 'external': return 'Affiliate link'); break; case 'variable': return 'Choose swatch'; break; default: return __( 'Read more', 'shoper' ); } }
Check out our code solution for moving WooCommerce variation prices to any location on your website.
Conclusion:
The code provided in the text allows you to modify the text of the “Add to Cart” button in a WooCommerce software. By using the WordPress add_filter function and the shoper_woocommerce_product_add_to_cart_text function, you can change the text displayed on the button based on the type of product being sold. Additionally, the code also includes a conditional statement to distinguish between products viewed on the single product page and products viewed on the shop page.