skip to Main Content

I have 2 categories on my WooCommerce website. Let’s say:

  • "cat1"
  • "cat2"

I have implemented the following code to modify the "Add To Cart" text to "View Product" for all the products.

add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product  ) {
    $button_text = __( "View Product", "woocommerce" );
    return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}   

But what I don’t know is how to modify the button text to "View Cat 1" for category 1 products, and "View Cat 2" for category 2 products.

2

Answers


  1. Chosen as BEST ANSWER

    Found out the solution:

    add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
    function replace_loop_add_to_cart_button( $button, $product  ) {
        if( has_term( 'cat-1', 'product_cat' ) ){
        // Category 1 Button text here
        $button_text = __( "View Cat 1", "woocommerce" );
        return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
        } else {
        // Category 2 Button text here
        $button_text = __( "View Cat 2", "woocommerce" );
        return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
        }
    }
    

    Hope it helps those in a similar situation :)


  2. Hope you are doing well i add pretty simple solution for you below is your magic.

    add_filter('woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2);
    function replace_loop_add_to_cart_button($button, $product)
    {
        $terms = get_the_terms($product->get_id(), 'product_cat');
        $cat_list = [];
        foreach ($terms as $cat) {
             $cat_list[] = $cat->slug;
         }
         $display_cat = ['cat-1', 'cat-2'];
         if (count(array_intersect($display_cat, $cat_list)) > 0) {
              $button_text = __("View Product", "woocommerce");
              return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
         } else {
              return $button;
         }
    }
    

    your second question answer is

    add_filter('woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2);
    function replace_loop_add_to_cart_button($button, $product)
     {
         $terms = get_the_terms($product->get_id(), 'product_cat');
         $cat_list = [];
         foreach ($terms as $cat) {
              $cat_list[] = $cat->slug;
         }
         if (in_array_any('cat-1', $cat_list)) {
               $button_text = __("View Product cat-1", "woocommerce");
         } else if (in_array_any('cat-2', $cat_list)) {
               $button_text = __("View Product cat-2", "woocommerce");
         } else {
               return $button;
         }
    
          return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search