我在函数中使用此代码。php在WordPress上生成基于访问者位置的附属链接,它工作得很好,但问题是如果打开页面缓存(W3 Total Cache),变量会被缓存,因此如果英国人是第一个打开页面的人,那么第二个德国人打开页面,他将获得与第一个访问者相同的链接。
请再说一件事,我对PHP和javascript还是很陌生,如果答案足够简单,我将不胜感激
<?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
}
?>