By default, in WooCommerce webshop, when you fill in the price of the product, the text on the add to cart button is "Add to cart", most of the time, this default setting is fine, when we need to personalize our webshop, we may need to change the text on the add to cart button, for example, change it to "Add to cart", when we need to personalize our webshop, we may need to change the text on the add to cart button, for example, change it to "Add to cart". When we need to personalize our online store and differentiate our brand, we may need to change the text on the Add to cart button, for example, to "Add to shopping bag". Of course, we can do this by modifying the template, but it's a bit of a pain in the ass. WooCommerce provides us with a Filter to accomplish this, so let's take a look at how this Filter is implemented.
In the following example, we change the default WooCommerce "Add to cart" to "Add to bag", the name of Filter has been changed in WooCommerce version 2.1 and later. In WooCommerce version 2.1 and later, the name of the Filter has changed, so the sample code is divided into version before 2.1 and version after 2.1, please pay attention to differentiate.
Modify the "Add to Cart" text on the product detail 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 the "Add to cart" text on the product archive 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_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 the "Add to Cart" text on the product detail page according to the 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')
return __( 'Buy product', 'woocommerce' );
return __( 'Buy product', 'woocommerce' ); break.
case 'grouped': return __( 'View products', 'woocommerce' ); break; }
return __( 'View products', 'woocommerce' ); break; case 'grouped'.
break; case 'grouped'.
return __( 'View products', 'woocommerce' ); break; case 'simple'.
return __( 'Add to bag', 'woocommerce' ); break; case 'simple'.
break; case 'simple'.
case 'variable'.
return __( 'Select options', 'woocommerce' ); break; case 'variable'.
break; default.
default: return __( 'Read more' ); break; case 'variable'.
return __( 'Read more', 'woocommerce' ); }
}
}
The above method is suitable for when only modifying the text added to the cart and nothing else, if your store does a larger WooCommerce Custom Development, directly modifying the text inside the template code is also a good option.
2 thoughts on “怎么修改WooCommerce网络商店中“添加到购物车” 的按钮文字”
Where do I go to modify this code?
Put it in the subject's
functions.php
The file will be fine.