Change woocommerce "add to cart" button functionality to only forward to product page
P粉009828788
P粉009828788 2023-09-12 09:47:11
0
1
553

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粉009828788
P粉009828788

reply all(1)
P粉810050669

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/>';
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template