I currently run a site which has products in Woocommerce. The product URL links to an affiliate site and the custom functions.php
script appends the affiliate ID to the extenal URL using this code:
* Custom Add Affiliate link to Buy Product
*/
add_filter( 'woocommerce_product_add_to_cart_url', 'custom_product_add_to_cart_url', 20, 2 );
function custom_product_add_to_cart_url( $add_to_cart_url, $product ){
if( $product->is_type('external') )
$add_to_cart_url .= '/?affiliateID-1';
return $add_to_cart_url;
}
/**
* End Custom Add Affiliate link to Buy Product
What I am hoping to do is add another affilate site to this and depending which URL is in the `$add_to_cart_url` would determine which affiliate link is appended.
So currently if the $add_to_cart_url
is www.siteA.com the link generated is www.siteA.com/?affiliateID-1
I want to make a change so that:
if the $add_to_cart_url
is www.siteA.com the link generated is www.siteA.com/?affiliateID-1
OR
if the $add_to_cart_url
is www.siteB.net the link generated is www.siteB.net/?affiliateID-2
I assume I need some form of "if $add_to_cart_url
is this, then append with this, else append with this" however I’m not sure how I need to adjust my existing code to do this.
I’m hoping someone out there can help me.
Many thanks.
2
Answers
You could try adding a second if statement within the
if( $product->is_type('external') )
block. You would need to assign a value to the $add_to_cart first to check what if it holds the SiteA or SiteB value etc. Then check that within the if statement, like so: (This will need changing to suit your argument)You can even hide the add to cart button and add a custom one and work with it.
For the product page:
Or like this: