-1

I'm using this code in functions.php on WordPress to generate an affiliate link based on the visitor location, It's working perfectly but the problem is that if page caching is turned on (W3 Total Cache), The variables get cached so if someone from the UK was the first one to open the page then the second one from Germany opened the page he will get the same link that the first visitor got.

One more thing please, I'm still very new to PHP and javascript so I would appreciate if the answer was simplified enough

 <?php

add_action( 'woocommerce_before_add_to_cart_button', 'affiliate_link', 10);

function affiliate_link() { 
$not_avilable_country = '<div id="amz_not_avilable" class="amz_not_avilable">This product is not avilable in your country yet</div>';


// IP Geolocation   
$country_code = $_SERVER ["HTTP_CF_IPCOUNTRY"];
// Get Custom Fields
$de_asin = get_post_meta(get_post()->ID, "wccaf_de_asin", true );
$uk_asin = get_post_meta(get_post()->ID, "wccaf_uk_asin", true );



//////////////////////////////////////////////


if ($country_code=="DE" or $country_code=="DE") {
    $amazon_domain = 'https://www.amazon.de';
 // $associate_id = 'bonstato-21';
    $asin = $de_asin;
}

else if ($country_code=="GB" && $uk_asin!=="") {
    $amazon_domain = 'https://www.amazon.co.uk';
 // $associate_id = 'bonmedico-21';
    $asin = $uk_asin;
}


///////////////////////////////////////////////

        if( wp_is_mobile() ) {

// Amazon Link For Mobile       
?>
<script>
    function amzGo(){
                window.location='<?php echo $amazon_domain ?>/dp/<?php echo $asin ?>/?tag=<?php echo $associate_id ?>';
  }
</script>
<?php
        }

else {

// Amazon Link For PC
    ?>
<script>
    function amzGo(){
        window.location='<?php echo $amazon_domain ?>/gp/aws/cart/add.html?AssociateTag=<?php echo $associate_id ?>&ASIN.1=<?php echo $asin ?>&Quantity.1=1';
  }
</script>
<?php
}


?>
     <div class="buy_amz_btn_wrap" >  
<button type="button" id="buy_amz_btn" class="buy_amz_btn" onclick="amzGo();"><i class="fa fa-amazon fa-amz"></i><?php echo $amz_btn_title ?></button>                           
</div>
    <?php

}

?>
  • 1
    this kind of functionality isn't going to be compatible with full page caching. It's the entire page that's being cached, the final output, not the variables – Tom J Nowell Sep 21 '18 at 0:56
  • sorry but this is a general caching question which is not specific to wordpress. It fits more the stackoverflow site – Mark Kaplun Sep 21 '18 at 1:58
1

Page caching generates a static HTML version of the page so IP geolocation occurs on the first rendering and is not triggered on subsequent renderings - your use case is not compatible with W3 Total Cache's page caching.

It's possible to accomplish what you are trying to accomplish with page caching, however, you would need to write your own caching implementation to check against a visitor geolocation cookie or $_GET parameter to serve different static HTML documents dependent upon the contents of its value (well beyond the scope of any simple coding).

  • Thanks Gary for your reply, there is one point i think you may not be aware of... I'm using cloudflare cdn geolocation which add the location of the visitor to the http headers and it works perfectly with any page caching, so my proplem is not that the visitor country value is cached, my proplem is that the php variables that u can see in my code got cached – Islam Mohamed Sep 21 '18 at 0:57
  • That's not how PHP or caching works. The variables are not being cached, the HTML page output is. If you're using Cloudflare for caching then you should not be using W3 Total Cache. All that's doing is preventing this Geolocation from working, because no matter what location they're from they're being served the W3 cached version which is not depending on location. – Jacob Peattie Sep 21 '18 at 0:59
  • ah i see, do you think that adding that code on a javascript file somehow and calling that js file from funchtions.php can be a solution or maybe call that php code via ajax somehow? If you think that idea can work and very simple to explain then i would really appreciate if you can tell me how to do it – Islam Mohamed Sep 21 '18 at 1:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.