简体   繁体   中英

Customize Add to Cart button on specific WooCommerce product categories

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM