Add a "view more" button to categories on WooCommerce shop page

I have woocommerce set to only show categories (instead of products) upon entering the shop page and I want every category to have a "View More" button with it's corresponding URL like a product would. Here's my code: add_action( 'woocommerce_after_subcategory_title', 'view_category_button', 10 ); function view_category_button() { $link = get_term_link( (int)$product_cat_id, 'product_cat' ); echo '<a href="' . $link . '"> View more </a>'; } However, the code fails to show the button and breaks the loading of the store. Here's a pic of what I want: https://imgur.com/98arCfL Here's a pic of what I get: https://imgur.com/xwLK6r0

Comment (1)

Jese Leos

August 30, 2024

Verified user

The main issue in your code is that $product_cat_id is not defined. Also, it is better to display the button after the existing link surrounding the thumbnail and the title. Try the following instead: add_action( 'woocommerce_after_subcategory_title', 'view_category_button', 20 ); function view_category_button( $category ) { printf('<a class="button" href="%s">%s</a>', esc_url( get_term_link( $category, 'product_cat' ) ), esc_html__( 'View more', 'woocommerce' ) ); } Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

You’ll be in good company