PHPwoocommercebeginner
Customize WooCommerce Add to Cart Button
Change the "Add to Cart" button text and behavior for different product types
Faisal Yaqoob
January 9, 2025
#woocommerce#add-to-cart#customization#button-text
Code
php
1 // Change "Add to Cart" text on single product page 2 function custom_single_add_to_cart_text() { 3 return __('Buy Now', 'woocommerce'); 4 } 5 add_filter('woocommerce_product_single_add_to_cart_text', 'custom_single_add_to_cart_text'); 6
7 // Change "Add to Cart" text on product archives (shop/category pages) 8 function custom_archive_add_to_cart_text() { 9 return __('Add to Bag', 'woocommerce'); 10 } 11 add_filter('woocommerce_product_add_to_cart_text', 'custom_archive_add_to_cart_text'); 12
13 // Change button text based on product type 14 function custom_add_to_cart_text_by_product_type() { 15 global $product; 16
17 $product_type = $product->get_type(); 18
19 switch ($product_type) { 20 case 'external': 21 return __('Buy on Amazon', 'woocommerce'); 22 case 'grouped': 23 return __('View Products', 'woocommerce'); 24 case 'simple': 25 return __('Purchase', 'woocommerce'); 26 case 'variable': 27 return __('Select Options', 'woocommerce'); 28 default: 29 return __('Add to Cart', 'woocommerce'); 30 } 31 } 32 add_filter('woocommerce_product_add_to_cart_text', 'custom_add_to_cart_text_by_product_type'); 33 add_filter('woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_text_by_product_type'); 34
35 // Change button text based on stock status 36 function custom_add_to_cart_text_by_stock() { 37 global $product; 38
39 if (!$product->is_in_stock()) { 40 return __('Out of Stock', 'woocommerce'); 41 } 42
43 if ($product->get_stock_quantity() <= 5 && $product->get_stock_quantity() > 0) { 44 return __('Only Few Left - Add to Cart', 'woocommerce'); 45 } 46
47 return __('Add to Cart', 'woocommerce'); 48 } 49 add_filter('woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_text_by_stock'); 50
51 // Change button text for specific product categories 52 function custom_add_to_cart_text_by_category() { 53 global $product; 54
55 if (has_term('digital-downloads', 'product_cat', $product->get_id())) { 56 return __('Download Now', 'woocommerce'); 57 } 58
59 if (has_term('pre-order', 'product_cat', $product->get_id())) { 60 return __('Pre-Order Now', 'woocommerce'); 61 } 62
63 return __('Add to Cart', 'woocommerce'); 64 } 65 add_filter('woocommerce_product_add_to_cart_text', 'custom_add_to_cart_text_by_category'); 66 add_filter('woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_text_by_category'); 67
68 // Add icon to Add to Cart button 69 function add_icon_to_cart_button($button_text, $product) { 70 $icon = '<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor"><path d="M0 1.5A.5.5 0 0 1 .5 1H2a.5.5 0 0 1 .485.379L2.89 3H14.5a.5.5 0 0 1 .491.592l-1.5 8A.5.5 0 0 1 13 12H4a.5.5 0 0 1-.491-.408L2.01 3.607 1.61 2H.5a.5.5 0 0 1-.5-.5zM5 12a2 2 0 1 0 0 4 2 2 0 0 0 0-4zm7 0a2 2 0 1 0 0 4 2 2 0 0 0 0-4z"/></svg> '; 71 return $icon . $button_text; 72 } 73 add_filter('woocommerce_product_add_to_cart_text', 'add_icon_to_cart_button', 10, 2);
Customize WooCommerce Add to Cart Button
This snippet allows you to customize the "Add to Cart" button text for different scenarios - shop page, single product page, and different product types.
// Change "Add to Cart" text on single product page
function custom_single_add_to_cart_text() {
return __('Buy Now', 'woocommerce');
}
add_filter('woocommerce_product_single_add_to_cart_text', 'custom_single_add_to_cart_text');
// Change "Add to Cart" text on product archives (shop/category pages)
function custom_archive_add_to_cart_text() {
return __('Add to Bag', 'woocommerce');
}
add_filter('woocommerce_product_add_to_cart_text', 'custom_archive_add_to_cart_text');
// Change button text based on product type
function custom_add_to_cart_text_by_product_type() {
global $product;
$product_type = $product->get_type();
switch ($product_type) {
case 'external':
return __('Buy on Amazon', 'woocommerce');
case 'grouped':
return __('View Products', 'woocommerce');
case 'simple':
return __('Purchase', 'woocommerce');
case 'variable':
return __('Select Options', 'woocommerce');
default:
return __('Add to Cart', 'woocommerce');
}
}
add_filter('woocommerce_product_add_to_cart_text', 'custom_add_to_cart_text_by_product_type');
add_filter('woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_text_by_product_type');
// Change button text based on stock status
function custom_add_to_cart_text_by_stock() {
global $product;
if (!$product->is_in_stock()) {
return __('Out of Stock', 'woocommerce');
}
if ($product->get_stock_quantity() <= 5 && $product->get_stock_quantity() > 0) {
return __('Only Few Left - Add to Cart', 'woocommerce');
}
return __('Add to Cart', 'woocommerce');
}
add_filter('woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_text_by_stock');
// Change button text for specific product categories
function custom_add_to_cart_text_by_category() {
global $product;
if (has_term('digital-downloads', 'product_cat', $product->get_id())) {
return __('Download Now', 'woocommerce');
}
if (has_term('pre-order', 'product_cat', $product->get_id())) {
return __('Pre-Order Now', 'woocommerce');
}
return __('Add to Cart', 'woocommerce');
}
add_filter('woocommerce_product_add_to_cart_text', 'custom_add_to_cart_text_by_category');
add_filter('woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_text_by_category');
// Add icon to Add to Cart button
function add_icon_to_cart_button($button_text, $product) {
$icon = '<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor"><path d="M0 1.5A.5.5 0 0 1 .5 1H2a.5.5 0 0 1 .485.379L2.89 3H14.5a.5.5 0 0 1 .491.592l-1.5 8A.5.5 0 0 1 13 12H4a.5.5 0 0 1-.491-.408L2.01 3.607 1.61 2H.5a.5.5 0 0 1-.5-.5zM5 12a2 2 0 1 0 0 4 2 2 0 0 0 0-4zm7 0a2 2 0 1 0 0 4 2 2 0 0 0 0-4z"/></svg> ';
return $icon . $button_text;
}
add_filter('woocommerce_product_add_to_cart_text', 'add_icon_to_cart_button', 10, 2);
Use Cases
E-commerce Best Practices:
- "Buy Now" for single products
- "Add to Bag" for fashion stores
- "Pre-Order" for upcoming products
- "Download" for digital products
Urgency Indicators:
- "Only 3 Left - Add to Cart"
- "Last Chance - Buy Now"
- "Limited Stock - Order Now"
Product-Specific:
// For a specific product ID
if ($product->get_id() === 123) {
return __('Reserve Your Spot', 'woocommerce');
}
Styling the Button
You can also customize the button appearance with CSS:
.single_add_to_cart_button {
background: #2ea44f !important;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.5px;
}
Dependencies
- WooCommerce
Related Snippets
Add Custom WooCommerce Checkout Field
Add a custom field to WooCommerce checkout page and save it to order meta
PHPwoocommerceintermediate
phpPreview
// Add custom field to checkout
function add_custom_checkout_field($checkout) {
echo '<div id="custom_checkout_field"><h3>' . __('Additional Information') . '</h3>';
...#woocommerce#checkout#custom-field+1
1/9/2025
View
Apply Discount Based on User Role
Automatically apply percentage discounts to WooCommerce products based on user roles
PHPwoocommerceadvanced
phpPreview
// Apply role-based discount to product prices
function apply_role_based_discount($price, $product) {
// Only for logged-in users
if (!is_user_logged_in()) {
...#woocommerce#discount#user-roles+2
1/9/2025
View
Customize WordPress Login Page
Replace WordPress logo on login page with your custom logo
PHPwordpressbeginner
phpPreview
// Replace login logo
function custom_login_logo() {
?>
<style type="text/css">
...#branding#login#wp-login+2
1/9/2025
View