net30 net30 - 1 month ago 20
HTML Question

Changing WooCommerce buy button link?

So I already have set up my WooCommerce child theme and organized the website with dummy data.

I can't find a way to change the link from the "Add to Cart" to an external link.
I want it so that each time someone clicks on the "Add to Cart" button the user be taken to external link example.com

How do I go about this?

P.S: I'm not worried about any of the security aspects since the site won't be used for real money transactions.

Answer

not going to ask about the idea behind your question, but using this filter will for sure do what you need:

<?php
add_filter( 'woocommerce_loop_add_to_cart_link', 'change_add_product_link' );
function change_add_product_link( $link ) {
    global $product;
    $product_id = $product->id;
    $product_sku = $product->get_sku();
    $link = '<a href="//www.yourtargeturl.com?id='.$product_id.'" rel="nofollow" data-product_id="'.$product_id.'" data-product_sku="'.$product_sku.'" data-quantity="1" class="button add_to_cart_button product_type_variable">'.sfws_woocommerce_product_add_to_cart_text().'</a>';
    return $link;
}
?>