Search code examples
phpwordpresswoocommerceproducttaxonomy-terms

Replace Loop Add to cart button with a button linked to the product and different text based on categories


In WooCommerce I changed "Add to cart button" to "View product" on shop page with the following:

// First, remove Add to Cart Button
add_action( 'after_setup_theme', 'my_remove_add_to_cart', 99 );
function my_remove_add_to_cart() {
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );    
}

// Second, add View Product Button
add_action( 'woocommerce_after_shop_loop_item', 'custom_view_product_button', 10 );
function custom_view_product_button() {
    global $product;
    $link = $product->get_permalink();
    echo '<a href="' . $link . '" class="button addtocartbutton">View product</a>';
}  

But also I need different text on "View product" button for different categories.

I need 5 different texts for 5 different categories on "View product" button on shop page only (on single product page, there should stay "add to cart button".

I used this code for change to View product button, but I dont know how to display a different "View product" text for different product categories.

So somehow add to this code different "View product" text for 5 different categories on shop page.

Category 1 -> View product text 1

Category 2 -> View product text 2 etc


Solution

  • Instead of removing add to cart button and replace it with a custom button, you can use woocommerce_loop_add_to_cart_link filter hook.

    The button text will be different depending on the product categories.

    As you doesn't use it, you can also disable Ajax add to cart (in WooCommerce settings):

    enter image description here

    Now in the first function below, you will define your product categories term Id(s), name(s) or slug(s) and for each category(ies), the corresponding text to be displayed in the button. You can define one or multiple category terms for each text.

    The code:

    // Settings function: Define for each categories, the corresponding button text to display
    function get_category_button_text() {
        return array(
            array(
                'terms' => array('hoodies', 'pants'), // Accept multiple categories term Ids, term slugs or term names
                'text' => __('Button Text 1', 'woocommerce') // Button text
            ),
            array(
                'terms' => array('tshirts', 'shirts'), 
                'text' => __('Button Text 2', 'woocommerce')
            ),
            array(
                'terms' => array('accessories'), 
                'text' => __('Button Text 3', 'woocommerce')
            ),
            array(
                'terms' => array('music', 'video'), 
                'text' => __('Button Text 4', 'woocommerce')
            ),
            array(
                'terms' => array('furniture'), 
                'text' => __('Button Text 5', 'woocommerce')
            ),
        );
    }
    
    // Replace Loop Add to Cart with a button linked to the product
    add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 1000, 2 );
    function replacing_add_to_cart_button( $button, $product  ) {
        $taxonomy    = 'product_cat';
        $product_id  = $product->get_id();
        $button_text = __('View product', 'woocommerce'); // The default text
    
        // Loop through defined category terms / text pairs
        foreach( get_category_button_text() as $values ) {
            if ( has_term($values['terms'], $taxonomy, $product_id ) ) {
                $button_text = $values['text'];
                break;
            } 
        }
        return sprintf('<a href="%s" class="button addtocartbutton">%s</a>', $product->get_permalink(), $button_text );
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.


    Code alternative:

    You can also use the following alternative (similar):

    // Settings function: Define for each categories, the corresponding button text to display
    function get_category_button_text() {
        return array(
            array(
                'terms' => array('hoodies', 'Hoodtwo', 'pants'), // Accept multiple categories term Ids, term slugs or term names
                'text' => __('Button Text 1', 'woocommerce') // Button text
            ),
            array(
                'terms' => array('tshirts', 'shirts'), 
                'text' => __('Button Text 2', 'woocommerce')
            ),
            array(
                'terms' => array('accessories'), 
                'text' => __('Button Text 3', 'woocommerce')
            ),
            array(
                'terms' => array('music', 'video'), 
                'text' => __('Button Text 4', 'woocommerce')
            ),
            array(
                'terms' => array('furniture'), 
                'text' => __('Button Text 5', 'woocommerce')
            ),
        );
    }
    
    // Replace Loop Add to Cart with a button linked to the product
    add_action( 'after_setup_theme', 'replace_loop_add_to_cart_button', 999 );
    function replace_loop_add_to_cart_button() {
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 ); 
        add_action( 'woocommerce_after_shop_loop_item', 'custom_loop_button_replacement', 10 );
    }
    
    // Custom button
    function custom_loop_button_replacement() {
        global $product;
    
        $taxonomy    = 'product_cat';
        $product_id  = $product->get_id();
        $button_text = __('View product', 'woocommerce'); // The default text
    
        // Loop through defined category terms / text pairs
        foreach( get_category_button_text() as $values ) {
            if ( has_term($values['terms'], $taxonomy, $product_id ) ) {
                $button_text = $values['text'];
                break;
            } 
        }
        printf('<a href="%s" class="button addtocartbutton">%s</a>', $product->get_permalink(), $button_text );
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and also works.