skip to Main Content

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


  1. 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)

        $add_to_cart = $someValueHere;
    
        if ($add_to_cart == $siteAValue){
    
            $add_to_cart_url .= '/?affiliateID-1';
    
        } else if ($add_to_cart == $siteBValue){
            
            $add_to_cart_url .= '/?affiliateID-2';
        }
            
    
        return $add_to_cart_url;
    }
    
    
    Login or Signup to reply.
  2. You can even hide the add to cart button and add a custom one and work with it.

    For the product page:

    add_action( 'woocommerce_single_product_summary', function () {
        global $product;
        if( $product->is_type('external') ) {
            echo '<a href="YOUR-URL/?affiliateID-1">Affiliate Button</a>';
        }
    }, 30 );
    

    Or like this:

    add_action( 'woocommerce_after_add_to_cart_button', function () {
        global $product;
    
        if( $product->is_type('external') ) {
            
            $link = 'YOUR-URL/?affiliateID-1'; // <== Here set button link
            $name = 'Affiliate Button'; // <== Here set button name 
            $class = 'button alt';
            $style = 'display: inline-block; margin-top: 12px;';
        
            // Output
            echo ' <a href="'.$link.'" class="'.$class.'" style="'.$style.'">'.$name.'</a>';
        }
    }, 20 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search