Woocommerce replace add to cart url to link to product page

woocommerce

I am using this shortcode to display products [product_category category="extras" orderby="date"].

Variable products show "select options" and single products show "add to cart". I was able to change text of both to say "View Product".

The problem now is that I need to change the url of those that used to say "add to cart", because they don't link to the product page but to "Add to the cart".

I know I can edit the woocommerce template, but I would need this as a function to be added to function.php

I don't need any button involved, just replacing the url.

So again purpose:
Replace/redirect "Add to Cart" url to link to product page (only in loop, obviously not in product page).

Can someone help?

Best Answer

If someone does elect to change the woocommerce file (in child theme of course!).

In file: /loop/add-to-cart.php

Change:

global $product;

echo apply_filters( 'woocommerce_loop_add_to_cart_link',
    sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button %s product_type_%s">%s</a>',
        esc_url( $product->add_to_cart_url() ),
        esc_attr( $product->id ),
        esc_attr( $product->get_sku() ),
        esc_attr( isset( $quantity ) ? $quantity : 1 ),
        $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
        esc_attr( $product->product_type ),
        esc_html( $product->add_to_cart_text() )
    ),
$product );

To:

global $product;

if ( $product->product_type == "simple" ) {
    $simpleURL = get_permalink();
    $simpleLabel =  "View Product";  // BUTTON LABEL HERE
} else {
    $simpleURL =  $product->add_to_cart_url();  
    $simpleLabel = $product->add_to_cart_text();
};

echo apply_filters( 'woocommerce_loop_add_to_cart_link',
    sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button %s product_type_%s">%s</a>',
        esc_url( $simpleURL ),
        esc_attr( $product->id ),
        esc_attr( $product->get_sku() ),
        esc_attr( isset( $quantity ) ? $quantity : 1 ),
        $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
        esc_attr( $product->product_type ),
        esc_html( $simpleLabel )
    ),
$product );
Related Topic