Hello WooCommerce Customizers!

Today is episode #12 of my “18 snippets in 18 days” challenge. With 6 days to go, I hope you’ve enjoyed the snippets so far and that your WooCommerce code snippet library has been growing steadily 🙂

I ask you to kindly share on social media and leave blog comments to support my marathon, a lot of work is going into this and feel free to share some love 🙂

Today we take a look at the WooCommerce “Add to Cart” buttons. What if you wanted to change the “Add to Cart” text depending on the Product Category? For example, you may want to show “Buy Now” for books and “Add to Basket” for cds.

WooCommerce: Change Add to Cart Text depending on the Category
WooCommerce: Change Add to Cart Text depending on the Category WooCommerce: Change Add to Cart Text depending on the Category

WooCommerce Snippet: Change “Add to Cart” Text by Product Category (2 Categories Only)


/**
 * @snippet       WooCommerce: Edit "Add to Cart" Text by Product Category
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=19944
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.3.3
 */

add_filter( 'woocommerce_product_add_to_cart_text', 'bbloomer_archive_custom_cart_button_text' );

function bbloomer_archive_custom_cart_button_text() {
global $product;       
if ( has_term( 'category1', 'product_cat', $product->ID ) ) {           
return 'Category 1 Add Cart';
} else {
return 'Category 2 Buy Now';
}
}

WooCommerce Snippet: Change “Add to Cart” Text by Product Category (Improved Snippet by Fabio Tielen)


/**
 * @snippet       WooCommerce: Edit "Add to Cart" Text by Product Category
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=19944
 * @author        Fabio Tielen & Rodolfo Melogli
 * @testedwith    WooCommerce 3.3.3
 */

add_filter( 'woocommerce_product_add_to_cart_text', 'bbloomer_archive_custom_cart_button_text' );
 
function bbloomer_archive_custom_cart_button_text() {
global $product;

$terms = get_the_terms( $product->ID, 'product_cat' );
 foreach ($terms as $term) {
            $product_cat = $term->name;
            break;
}

switch($product_cat)
{
    case 'category1';
        return 'Category 1 button text'; break;
    case 'category2';
        return 'Category 2 button text'; break;
// case 'category3'; etc...
// return 'Category 3 button text'; break;

    default;
        return 'Default button text when no match found'; break;
}
}

Share:

Leave a Reply