skip to Main Content

What would the php code be to change the add to cart text to something unique for each product category, on both the product archives and single product pages using one code snippet if possible? I have six product categories. I am using WooCommerce.

Basically, I have 6 categories, I’d like 5 of them to say "Order" and then 1 of them to say "Book Now"

The 6 categories are:

  1. Cakes
  2. Cupcakes
  3. Halloween
  4. Sweet Treats
  5. Petal and Post
  6. Baking Class

i.e. all of the categories except for "Baking Class" will say Order, while "Baking Class" will say "Book Now".

I’d like to apply to the buttons all over the website, no matter where they are displayed.

I am using Elementor to create custom Product Archives.

Update 25th October, 2022:

I have tried using the code from mujuonly’s answer below. It works except for on the product archive for the Baking Classes category; which I designed using Elementor Pro: https://bunsbakery.co.za/product-category/baking-classes/

Here is the code I used:

add_filter('woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text', 10, 2);
add_filter('woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text', 10, 2);

function woocommerce_custom_product_add_to_cart_text($text, $product) {

    if( has_term( array( 'Cakes', 'Cupcakes', 'Halloween', 'Sweet Treats', 'Petal and Post' ), 'product_cat', $product->get_id() ) ){
        $text = __('Order', 'woocommerce');
    }
    if( has_term( array( 'baking-classes' ), 'product_cat', $product->get_id() ) ){
        $text = __('Book Now', 'woocommerce');
    }
    return $text;
}

2

Answers


  1. I have not tested this, but this might be doable like this:

    // To change add to cart text on product archives(Collection) page
    add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text' );  
    function woocommerce_custom_product_add_to_cart_text() {
        global $product;
        if( has_term( array( 'sneakers', 'backpacks' ), 'product_cat', $product ) 
        {
            // do something if product is either in category "sneakers" or "backpacks"
            return __( 'Buy Sneakers or backback now', 'woocommerce' );
        } else {
            return __( 'Buy Now', 'woocommerce' );
        }
           
    }
    

    If you want to change it on the single product pages, you should use the filter woocommerce_product_single_add_to_cart_text instead.
    Or both if you want it on both.

    Toy around with it, and see if you can get i to work.

    UPDATE

    So this could be your finalfunction, based on your criteria and mujuonlys answer.

    add_filter('woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text', 10, 2);
    
    function woocommerce_custom_product_add_to_cart_text($text, $product) {
    
        $text = __('Order', 'woocommerce');
    
        if( has_term( array( 'Baking Class' ), 'product_cat', $product->get_id() ) ){
            $text = __('Book Now', 'woocommerce');
        }
        return $text;
    }
    
    Login or Signup to reply.
  2. add_filter('woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text', 10, 2);
    add_filter('woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text', 10, 2);
        
        function woocommerce_custom_product_add_to_cart_text($text, $product) {
        
            if( has_term( array( 'Cakes', 'Cupcakes', 'Halloween', 'Sweet Treats', 'Petal and Post' ), 'product_cat', $product->get_id() ) ){
                $text = __('Order', 'woocommerce');
            }
            if( has_term( array( 'Baking Class' ), 'product_cat', $product->get_id() ) ){
                $text = __('Book Now', 'woocommerce');
            }
            return $text;
        }
    

    Tested OK in Single product page and Shop page.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search