0

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

}

?>
Islam Mohamed
  • 91
  • 1
  • 8
  • where is the code that caches it? change the cache key to include a countrty or language, so they wont overwrite each other – delboy1978uk Sep 21 '18 at 11:21
  • Might not answer the question, i will use javascript detect the country and redirect based on that so I won't have to worry about caching. More info https://stackoverflow.com/questions/3489460/how-to-get-visitors-location-i-e-country-using-geolocation – cjmling Sep 21 '18 at 11:45

3 Answers3

0

A way to deactivate/disable W3 Total Cache on specific pages is to use another plugin called: "Plugin Organizer" by Jeff Sterup.

After installing and enabling it, go to settings (of the plugin) and follow the instructions to set it in the right way.

Once the plugin is enabled and settings are saved correctly, when you edit/create a new page using the editor you can see a checkbox that's show you which plugin to enable for that particular page and which to disable.

In this way you can disable W3 Total Cache on the page that's use your function on functions.php.

Hope it helps.

Sigma
  • 387
  • 3
  • 17
0

The most common way to bypass the cache is to serve this data via the WordPress json api and use java script to put the always fresh data into place.

  • Is it possible to just call that function with ajax? if it's possible then could you please tell me how to call a specific function from fuchtions.php and print it using ajax in a specific hook? – Islam Mohamed Sep 21 '18 at 11:57
0

The problem is not that the variable inside the code is being cached; what happens is that W3 Total Cache grabs the first page produced by your PHP code and store it in the hard disk of the server. Thenceforth, the browsers' requests are answered delivering the HTML static file stored in server's hard disk (PHP will not produce the same page again).

To solve this problem, you must transfer the "intelligence" inside your PHP code, which is responsible for create slightly different versions of your page, to your Javascript code. Bastian Haustein suggested one way to do that, using WordPress REST API. However, I would try first doing it in a simpler way: after page load, capture the link of your cached page and modify it according to the location of your visitor. Naturally, you will have to capture visitors geolocation also using JS - see the link suggested by cjmling: How to get visitor's location (i.e. country) using geolocation?

aldemarcalazans
  • 1,309
  • 13
  • 16