4

I recently added code snippet to my functions.php child theme file which task is to echo a "Read More" button under all products which leads the user to the click product page. The product-id link is not working. Here is the code:

/*-ADD NEW BUTTON THAT LINKS TO PRODUCT PAGE FOR EACH PRODUCT */

add_action('woocommerce_after_shop_loop_item','replace_add_to_cart');

function replace_add_to_cart() {

global $product;

$link = $product->get_permalink();

echo do_shortcode('<br><button link="' . esc_attr($link) . '">Read more</button>');
}

Right now, It just shows a button text(without class) which does not redirect to any product link. I want to add the primary button to it as well.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Umair Rasool
  • 41
  • 1
  • 8
  • can you show us the pageo your site ? to see how is the output – Temani Afif Oct 04 '17 at 08:53
  • the question was simple guys! I removed the add to cart button from archive products page. Now, Added this new " Read More" button which suppose to redirect to each product page. So how to do it put the product-id link to that "Read More" and the primary button class? Here is the link of its implementation: http://northgolf.eu/shop/ – Umair Rasool Oct 04 '17 at 10:12

1 Answers1

4

There is many different errors in your code and your question is not so clear. So you can:

1) To add an additional button (below existing add-to-cart button):

add_action('woocommerce_after_shop_loop_item', 'replace_add_to_cart' );
function replace_add_to_cart() {
    global $product;
    echo '<br><a class="button" href="' . esc_attr( $product->get_permalink() ) . '">' . __( "Read more" ) . '</a>';
}

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

Tested and works.


2) Replace add to cart button using the woocommerce_loop_add_to_cart_link filter hook this way:

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  ) {
    return '<a class="button" href="' . $product->get_permalink() . '">' . __( "Read more" ) . '</a>';
}

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

Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399