Read more button does not work in woocommerce store

Recently, I added a code snippet to the functions.php file for the children’s theme, the task of which is the “Read more” echo button under all products that lead the user to the click product page. Link to product identifier does not work. 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 the button text (without a class) that is not redirected to any product link. I want to add a main button to it.

+4
source share
1 answer

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

1) To add an additional button (below the 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>';
}

function.php ( ), .

.


2) , woocommerce_loop_add_to_cart_link:

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>';
}

function.php ( ), .

.

+3

Source: https://habr.com/ru/post/1686856/


All Articles