By default, in the WooCommerce online store, when the product price is filled in, the text added to the shopping cart is “Add to cart”. In most cases, this default setting is fine. When we need to personalize our online store and highlight brand differentiation, we may need to modify the text on the add to cart button, for example, to “Add to shopping bag.” Of course, we can achieve this requirement by modifying the template, but it is a little troublesome. WooCommerce provides us with a Filter to achieve this requirement. Let’s take a look at the specific implementation method of this Filter.
In the following example, we modify WooCommerce’s default “Add to cart” to “Add to bag”. In WooCommerce 2.1 and later versions, the name of the Filter has changed. Therefore, the example code is divided into versions before 2.1 and versions after 2.1. Please pay attention to the distinction.
Modify the “Add to Cart” text on the product details page
add_filter( 'add_to_cart_text', 'woo_custom_cart_button_text' ); // < 2.1
function woo_custom_cart_button_text() {
return __( 'Add to bag', 'woocommerce' );
}
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 bag', 'woocommerce' );
}
Modify “Add to Cart” text on product archive pages
add_filter( 'add_to_cart_text', 'woo_custom_cart_button_text' ); // < 2.1
function woo_custom_cart_button_text() {
return __( 'Add to bag', 'woocommerce' );
}
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 bag', 'woocommerce' );
}
Modify “Add to Cart” text on product detail pages based on product type
add_filter( 'woocommerce_product_add_to_cart_text' , 'custom_woocommerce_product_add_to_cart_text' );
function custom_woocommerce_product_add_to_cart_text() {
global $product;
$product_type = $product->product_type;
switch ( $product_type ) {
case 'external':
return __( 'Buy product', 'woocommerce' );
break;
case 'grouped':
return __( 'View products', 'woocommerce' );
break;
case 'simple':
return __( 'Add to bag', 'woocommerce' );
break;
case 'variable':
return __( 'Select options', 'woocommerce' );
break;
default:
return __( 'Read more', 'woocommerce' );
}
}
The above method is suitable for modifying only the text added to the shopping cart without modifying other content. If your store has done a relatively largeWooCommerce custom development, directly modifying the text in the template code is also a good choice.
