我编写了一个基于访问者位置生成链接的代码,它工作得很好,但我发现生成的代码会被缓存,因为我使用的是全页缓存,所以我认为为了解决这个问题,我可以使用ajax加载该链接。我使用了下面的代码,这些代码在获取一些我需要的变量时非常有效,例如位置变量和链接域变量等。。但是,我无法获取WooCommerce自定义字段数据,甚至无法获取产品id,它只返回空白。
我使用这段代码来获取自定义字段,该字段在函数中直接使用时效果很好,但在ajax中无法正常工作
$uk_asin = get_post_meta(get_post()->ID, "wccaf_uk_asin", true );
我在函数中使用了该代码。phpadd_action( \'woocommerce_before_add_to_cart_button\', \'affiliate_link_ajax\', 11);
function affiliate_link_ajax() {
?>
<script>
jQuery(document).ready(function(){
jQuery.ajax({
url: "<?php echo admin_url(\'admin-ajax.php\'); ?>",
type: \'POST\',
data: {
action: \'getmyfunctionform1\'
},
dataType: \'html\',
success: function(response) {
jQuery("#myResultsform1").html(response);
}
});
});
</script>
<!-- end Ajax call to getmyfunctionform1 smc 11-22-2013 -->
<div id="myResultsform1"></div>
<?php
}
这是功能代码。还有php// Ajax Function to Load PHP Function myfunctionform1 smc 11/22/2013
add_action(\'wp_ajax_getmyfunctionform1\', \'myfunctionform1\');
add_action(\'wp_ajax_nopriv_getmyfunctionform1\', \'myfunctionform1\');
function myfunctionform1() {
// Whatever php and or html you want outputed by the ajax call in template file
die(); } // important must use
// end Ajax Function to Load PHP Function myfunctionform1 smc 11/22/2013
如果答案很简单,我会非常感激,因为我对编码还是很陌生