Although the WordPress/WooCommerce universe is full of free plugins that solve our programming work, sometimes we encounter shortcomings that are difficult to fix and don’t have a custom plugin.
Recently, a client asked us to change the default cart button text “add to basket” to another text that should vary depending on the product category. In this case, we had to custom program the function and add the script manually.
Here is the example of the magic code you can add directly to the functions.php file of your WordPress child theme template:
get_id(), 'product_cat' );
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$product_cat = $term->slug;
break; // Take only the first category slug
}
switch ( $product_cat ) {
case 'tickets-talleres-y-sesiones':
return __( 'Sign up now', 'your_theme_text_domain' );
default:
return __( 'Add to cart', 'your_theme_text_domain' );
}
}
// Default button text if no category found
return __( 'Add to cart', 'your_theme_text_domain' );
}
You are welcome! 😉