This comprehensive code snippet allows you to completely customize the “Add to Cart” button text throughout your WooCommerce store. You can set different button labels based on product type, category, price, stock level, user role, season, and more.
Instead of the generic “Add to Cart” text, this snippet lets you display contextual, engaging button text that better describes the action or creates urgency. It works on both shop/archive pages and single product pages.
[tpsc_custom_add_to_cart id=”123″ text=”Custom Text” class=”my-button-class”]
/**
* Rename/Customize the "Add to Cart" button text throughout WooCommerce
* Change button labels based on product type, category, or custom conditions
* Prefix: tpsc_ (TP Snippet Collection)
*/
// ============================================
// CONFIGURATION - Set your custom text here
// ============================================
// Default button texts for different product types
define('TPSC_SIMPLE_PRODUCT_TEXT', 'Buy Now'); // Simple products
define('TPSC_VARIABLE_PRODUCT_TEXT', 'Choose Options'); // Variable products
define('TPSC_GROUPED_PRODUCT_TEXT', 'View Products'); // Grouped products
define('TPSC_EXTERNAL_PRODUCT_TEXT', 'Buy at Store'); // External/Affiliate products
define('TPSC_OUT_OF_STOCK_TEXT', 'Out of Stock'); // Out of stock products
define('TPSC_DEFAULT_TEXT', 'Add to Cart'); // Fallback text
// ============================================
// SHOP/ARCHIVE PAGES - Button text
// ============================================
// Change button text on shop/archive pages
add_filter('woocommerce_product_add_to_cart_text', 'tpsc_custom_shop_button_text', 10, 2);
function tpsc_custom_shop_button_text($text, $product) {
// Check if product is out of stock first
if (!$product->is_in_stock()) {
return __(TPSC_OUT_OF_STOCK_TEXT, 'woocommerce');
}
// Check product type and return appropriate text
switch ($product->get_type()) {
case 'simple':
return __(TPSC_SIMPLE_PRODUCT_TEXT, 'woocommerce');
case 'variable':
return __(TPSC_VARIABLE_PRODUCT_TEXT, 'woocommerce');
case 'grouped':
return __(TPSC_GROUPED_PRODUCT_TEXT, 'woocommerce');
case 'external':
return __(TPSC_EXTERNAL_PRODUCT_TEXT, 'woocommerce');
default:
return __(TPSC_DEFAULT_TEXT, 'woocommerce');
}
}
// ============================================
// SINGLE PRODUCT PAGE - Button text
// ============================================
// Change button text on single product pages - Simple & External products
add_filter('woocommerce_product_single_add_to_cart_text', 'tpsc_custom_single_button_text', 10, 2);
function tpsc_custom_single_button_text($text, $product) {
// Check if product is out of stock
if (!$product->is_in_stock()) {
return __(TPSC_OUT_OF_STOCK_TEXT, 'woocommerce');
}
// Return text based on product type
if ($product->is_type('simple')) {
return __(TPSC_SIMPLE_PRODUCT_TEXT, 'woocommerce');
} elseif ($product->is_type('external')) {
return __(TPSC_EXTERNAL_PRODUCT_TEXT, 'woocommerce');
}
return $text;
}
// ============================================
// ADVANCED: Category-specific button text
// ============================================
add_filter('woocommerce_product_add_to_cart_text', 'tpsc_category_specific_button_text', 20, 2);
add_filter('woocommerce_product_single_add_to_cart_text', 'tpsc_category_specific_button_text', 20, 2);
function tpsc_category_specific_button_text($text, $product) {
// CUSTOMIZE THESE RULES AS NEEDED
// Example: Different text for specific categories
// Digital products category
if (has_term('digital-downloads', 'product_cat', $product->get_id())) {
return __('Download Now', 'woocommerce');
}
// Courses category
if (has_term('courses', 'product_cat', $product->get_id())) {
return __('Enroll Now', 'woocommerce');
}
// Services category
if (has_term('services', 'product_cat', $product->get_id())) {
return __('Book Service', 'woocommerce');
}
// Membership category
if (has_term('membership', 'product_cat', $product->get_id())) {
return __('Join Now', 'woocommerce');
}
// Gift cards category
if (has_term('gift-cards', 'product_cat', $product->get_id())) {
return __('Purchase Gift Card', 'woocommerce');
}
return $text;
}
// ============================================
// PRICE-BASED: Different text based on price
// ============================================
add_filter('woocommerce_product_add_to_cart_text', 'tpsc_price_based_button_text', 30, 2);
add_filter('woocommerce_product_single_add_to_cart_text', 'tpsc_price_based_button_text', 30, 2);
function tpsc_price_based_button_text($text, $product) {
// Skip if product doesn't have a price
if (!$product->get_price()) {
return $text;
}
$price = floatval($product->get_price());
// Free products (price = 0)
if ($price == 0) {
return __('Get for Free', 'woocommerce');
}
// High-value products (over $500)
if ($price > 500) {
return __('Purchase Premium', 'woocommerce');
}
// Low-value products (under $10)
if ($price < 10) {
return __('Quick Buy', 'woocommerce');
}
return $text;
}
// ============================================
// SALE PRODUCTS: Special text for items on sale
// ============================================
add_filter('woocommerce_product_add_to_cart_text', 'tpsc_sale_button_text', 40, 2);
add_filter('woocommerce_product_single_add_to_cart_text', 'tpsc_sale_button_text', 40, 2);
function tpsc_sale_button_text($text, $product) {
// Check if product is on sale
if ($product->is_on_sale() && $product->is_in_stock()) {
// Calculate discount percentage for more dynamic text
if ($product->get_regular_price() && $product->get_sale_price()) {
$percentage = round(100 - ($product->get_sale_price() / $product->get_regular_price() * 100));
// High discount (over 50%)
if ($percentage >= 50) {
return sprintf(__('Save %d%% - Buy Now!', 'woocommerce'), $percentage);
}
// Regular sale
else {
return __('Buy on Sale!', 'woocommerce');
}
}
return __('Special Offer - Buy Now', 'woocommerce');
}
return $text;
}
// ============================================
// STOCK-BASED: Different text based on stock levels
// ============================================
add_filter('woocommerce_product_add_to_cart_text', 'tpsc_stock_based_button_text', 50, 2);
add_filter('woocommerce_product_single_add_to_cart_text', 'tpsc_stock_based_button_text', 50, 2);
function tpsc_stock_based_button_text($text, $product) {
// Only for products with stock management
if ($product->managing_stock() && $product->is_in_stock()) {
$stock_quantity = $product->get_stock_quantity();
// Low stock (less than 5)
if ($stock_quantity > 0 && $stock_quantity <= 5) {
return sprintf(__('Only %d Left - Buy Now!', 'woocommerce'), $stock_quantity);
}
// Very low stock (only 1 left)
if ($stock_quantity == 1) {
return __('Last One - Grab It!', 'woocommerce');
}
}
return $text;
}
// ============================================
// SEASONAL/TIME-BASED: Different text based on date/time
// ============================================
add_filter('woocommerce_product_add_to_cart_text', 'tpsc_seasonal_button_text', 60, 2);
add_filter('woocommerce_product_single_add_to_cart_text', 'tpsc_seasonal_button_text', 60, 2);
function tpsc_seasonal_button_text($text, $product) {
// Get current date
$current_month = date('n');
$current_day = date('j');
// Black Friday (November)
if ($current_month == 11 && $current_day >= 24 && $current_day <= 30) {
return __('Black Friday Deal!', 'woocommerce');
}
// Christmas season (December)
if ($current_month == 12 && $current_day <= 25) {
return __('Order for Christmas!', 'woocommerce');
}
// New Year (January)
if ($current_month == 1 && $current_day <= 7) {
return __('New Year Special!', 'woocommerce');
}
// Valentine's Day (February 1-14)
if ($current_month == 2 && $current_day <= 14) {
return __('Gift for Valentine!', 'woocommerce');
}
return $text;
}
// ============================================
// USER ROLE BASED: Different text for different users
// ============================================
add_filter('woocommerce_product_add_to_cart_text', 'tpsc_user_role_button_text', 70, 2);
add_filter('woocommerce_product_single_add_to_cart_text', 'tpsc_user_role_button_text', 70, 2);
function tpsc_user_role_button_text($text, $product) {
// Check if user is logged in
if (is_user_logged_in()) {
$user = wp_get_current_user();
// Wholesale customers
if (in_array('wholesale_customer', $user->roles)) {
return __('Wholesale Order', 'woocommerce');
}
// VIP members
if (in_array('vip_member', $user->roles)) {
return __('VIP Purchase', 'woocommerce');
}
// First-time buyers (no previous orders)
$customer_orders = wc_get_orders([
'customer_id' => $user->ID,
'limit' => 1,
]);
if (empty($customer_orders)) {
return __('First Purchase - Buy Now!', 'woocommerce');
}
} else {
// Not logged in users
return __('Buy Now - Sign Up for Discount!', 'woocommerce');
}
return $text;
}
// ============================================
// AJAX ADD TO CART: Update button after adding
// ============================================
add_filter('woocommerce_product_add_to_cart_text', 'tpsc_ajax_added_to_cart_text', 80, 2);
function tpsc_ajax_added_to_cart_text($text, $product) {
// Check if this product is already in cart
if (tpsc_product_in_cart($product->get_id())) {
return __('✓ In Cart - Add More?', 'woocommerce');
}
return $text;
}
// Helper function to check if product is in cart
function tpsc_product_in_cart($product_id) {
if (!WC()->cart) {
return false;
}
foreach (WC()->cart->get_cart() as $cart_item) {
if ($cart_item['product_id'] == $product_id) {
return true;
}
}
return false;
}
// ============================================
// SHORTCODE: Display custom button anywhere
// ============================================
add_shortcode('tpsc_custom_add_to_cart', 'tpsc_custom_add_to_cart_shortcode');
function tpsc_custom_add_to_cart_shortcode($atts) {
$atts = shortcode_atts([
'id' => '',
'text' => 'Add to Cart',
'class' => 'button',
], $atts);
if (empty($atts['id'])) {
return '';
}
return sprintf(
'<a href="%s" data-product_id="%s" class="add_to_cart_button ajax_add_to_cart %s">%s</a>',
esc_url(wc_get_cart_url() . '?add-to-cart=' . $atts['id']),
esc_attr($atts['id']),
esc_attr($atts['class']),
esc_html($atts['text'])
);
}