Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Woocommerce "Add to cart" button to "view product" button

Trying to change the woocommerce button text from "add to cart" to "read more" and redirect it so that clicking the button takes the user to the individual product page. So far, the link works but all the text on the button says is "Button" when I need it to say "Read More". I'll place the code below, can anyone please tell me what the problem is.

/*STEP 1 - REMOVE ADD TO CART BUTTON ON PRODUCT ARCHIVE (SHOP) */

function remove_loop_button(){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}
add_action('init','remove_loop_button');



/*STEP 2 -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]');
}
like image 367
Amber Avatar asked Dec 02 '22 10:12

Amber


1 Answers

Try this alternative that will replace add to cart button by a linked button to the product in Shop and archives pages

// Replace add to cart button by a linked button to the product in Shop and archives pages
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  ) {
    // Not needed for variable products
    if( $product->is_type( 'variable' ) ) return $button;

    // Button text here
    $button_text = __( "View product", "woocommerce" );

    return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}

Code goes in function.php file of your active child theme (or theme). Tested and works.

like image 185
LoicTheAztec Avatar answered Dec 04 '22 04:12

LoicTheAztec