There are a few steps you can take to replace the “add to cart” link in WooCommerce with a link that redirects to the vendor’s store:

  1. Create a custom function that will output the desired link. The function should include the link to the vendor’s store, and any relevant information such as the product ID or vendor ID.
  2. Use the WooCommerce hook ‘woocommerce_loop_add_to_cart_link’ to override the default “add to cart” link with your custom link. This hook passes the default link as an argument and allows you to replace it with your custom link.
  3. In your custom function, you can use WordPress function get_post_meta() to get the vendor’s store url and product id.

Here is an example of how the custom function could look like:

function custom_add_to_cart_link( $link ) {
    global $product;
    $product_id = $product->get_id();
    $vendor_store_url = get_post_meta( $product_id, 'vendor_store_url', true );
    $link = '<a href="' . esc_url( $vendor_store_url ) . '" class="button">Buy from vendor</a>';
    return $link;
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'custom_add_to_cart_link' );

This code will replace the default “add to cart” link with a link that redirects to the vendor’s store. Make sure to replace ‘vendor_store_url’ with the actual meta key that you use to store the vendor’s store URL.

Please note that, this code will change the add to cart link in all the pages that use the loop of products, if you want to change it only in specific pages, you’ll need to use conditional tags to check for the pages where you want to show the custom.

(Visited 17 times, 1 visits today)
Was this article helpful?
YesNo
Close Search Window