Changing WooCommerce buy button link?

2019-09-05 19:24发布

问题:

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.

回答1:

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;
}
?>


回答2:

Woo-commerce plugin for Wordpress have custom add-to-cart features. You can add it from product or using shortcode to your script or posts.

Here is the details about -

  • WooCommerce Custom “Add to Cart”
  • Creating a custom “Add to Cart” URL


回答3:

Depending on the product type you use. You can find all add_to_cart template file in the folder.

woocommerce/templates/single-product/add-to-cart/

You can set your product as external product, and it will redirect user to the external page.

If you want to stay with simple or variation product, you'll need to copy/paste the file you need in your child theme and modify it.