search

Home  >  Q&A  >  body text

Change woocommerce "add to cart" button functionality to only forward to product page

Hello, I would like to change the functionality of the "Add to Cart" button to only forward to the product page without adding to the cart

add_filter( 'woocommerce_loop_add_to_cart_link', 'redirect_to_product_page', 10, 2 );
 
function redirect_to_product_page( $link, $product ) {
    global $woocommerce;
    $product_cat = wp_get_post_terms( $product->get_id(), 'product_cat', array( 'fields' => 'slugs' ) );
    $product_cat = isset( $product_cat[0] ) ? $product_cat[0] : '';
    $link = get_site_url() . '/product/' . $product_cat . '/' . $product->get_slug() . '/';
    return $link;
}

This is the code I tried but it just replaces "add to cart" with the correct url

P粉009828788P粉009828788457 days ago544

reply all(1)I'll reply

  • P粉810050669

    P粉8100506692023-09-13 00:26:25

    You can remove the button and put it back with your own information. This also allows you to change the button text:

    add_action('init', 'remove_loop_button');
    function remove_loop_button(){
        remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
    }
    
    add_action('woocommerce_after_shop_loop_item', 'replace_add_to_cart');
    function replace_add_to_cart() {
        global $product;
        $button_text = 'View Product';
        echo '<a class ="button product_type_simple add_to_cart_button ajax_add_to_cart" href="' . $product->get_permalink() . '">' . $button_text . '</a><br/>';
    }

    reply
    0
  • Cancelreply