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?
If you want to replace add to cart button with link in WooCommerce, you must understand how the add to cart works since it uses simple URLs with product parameter. You can create WooCommerce add to cart links that work just the same way as the add to cart button.
To Add One Variable Product to Cart and Redirect to Any Page, use this link: If your WooCommerce store has Grouped Products, you can also add custom links to the Add to Cart button. A grouped product can be defined as a combination of two or more sub-products, and each one can be added with a custom quantity to cart.
To add a custom URL all you need is the Variation ID and you can check the steps below to know where you can find it. Log into your WordPress site and access the Dashboard as the admin user. From the Dashboard menu, click on Products > All Products. This will open all the products that are in your WooCommerce store.
1/ Click a custom add to cart link. Product is successfully added to the cart. 2/ Navigate to another page. 4/ Product is added to the cart again, because the URL includes the custom link query string. This is really undesirable behaviour unfortunately.
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 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With