简体   繁体   English

带有多个 URL 的 Woocommerce 自定义附属链接

[英]Woocommerce Custom affiliate link with multi URLs

I currently run a site which has products in Woocommerce.我目前经营一个网站,该网站在 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:产品 URL 链接到附属站点,自定义functions.php脚本使用以下代码将附属 ID 附加到外部 URL:

 *  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. 我希望做的是向此添加另一个附属站点,并根据`$add_to_cart_url` 中的 URL 确定附加哪个附属链接。

So currently if the $add_to_cart_url is www.siteA.com the link generated is www.siteA.com/?affiliateID-1所以目前如果$add_to_cart_urlwww.siteA.com生成的链接是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如果$add_to_cart_urlwww.siteA.com,则生成的链接是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如果$add_to_cart_urlwww.siteB.net,则生成的链接是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.我假设我需要某种形式的“如果$add_to_cart_url是这个,那么附加这个,否则附加这个”但是我不确定我需要如何调整我现有的代码来做到这一点。

I'm hoping someone out there can help me.我希望那里有人可以帮助我。

Many thanks.非常感谢。

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 );

You could try adding a second if statement within the if( $product->is_type('external') ) block.您可以尝试在if( $product->is_type('external') )块中添加第二个 if 语句。 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 分配一个值,以检查它是否包含 SiteA 或 SiteB 值等。然后在 if 语句中检查它,如下所示:(这将需要更改以适合您的论点)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM