1
votes

I am trying to change the add to cart button for a specific product category on my WooCommerce archives pages to a "Read more" button that will be linked to the product page (but Not on the single product page itself).

(Woocommerce, with Elementor on Wordpress)

Using "Replace add to cart button with a read more linked to product page on shop pages in WooCommerce 3" answer code, how to restrict it to only one product category (the term name is "Classes" in this case)?

Any help is appreciated.

1

1 Answers

2
votes

You can use has_term() conditional function to target a product category, this way:

add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
    if ( has_term( 'Classes', 'product_cat', $product->get_id() ) ) {
        $button_text = __("Read more", "woocommerce");
        $button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
    }

    return $button;
}

Code goes in functions.php file of your active child theme (or active theme) or also in any plugin file.