我有一些函数代码。我在前端通过ajax调用的php,我使用的是admin ajax,加载大约需要2到2.5秒,因为我有很多插件,ajax处理程序需要加载所有wp核心和插件数据,我想知道如何编写一个自定义ajax处理程序,只加载运行下面代码段所需的内容,我的代码的要点是,它通过获取元数据和读取HTTP头来获取woocommerce自定义字段,以便进行地理定位,并实现我定义的一些变量来创建带有链接的按钮。
function simple_amz_link_ajax() {
?>
<script>
jQuery(document).ready(function(){
jQuery.ajax({
url: "<?php echo admin_url(\'admin-ajax.php?action=get_amz_btn\'); ?>",
type: \'POST\',
data: {
action: \'get_simple_amz_button\',
postId: <?php echo get_post()->ID; ?>
},
dataType: \'html\',
success: function(response) {
jQuery("#buy_amz_btn_wrap").html(response);
}
});
});
</script>
<!-- end Ajax call to get_simple_amz_button -->
<div id="buy_amz_btn_wrap">
<div class="spinner">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</div>
<?php
}
add_action(\'wp_ajax_get_simple_amz_button\', \'simple_amz_button\');
add_action(\'wp_ajax_nopriv_get_simple_amz_button\', \'simple_amz_button\');
function simple_amz_button() {
// Variables Declaration
$postId = filter_input( INPUT_POST, \'postId\', FILTER_SANITIZE_NUMBER_INT );
$de_asin = get_post_meta( $postId, "wccaf_de_asin", true );
$country_code = $_SERVER ["HTTP_CF_IPCOUNTRY"];
$not_avilable_country = \'<div id="amz_not_avilable" class="amz_not_avilable">This product is not avilable in your country yet</div>\';
// Get Amazon Button Title
if (ICL_LANGUAGE_CODE == "de") {
$amz_btn_title = \'Kaufen auf Amazon\';
$not_avilable_country = \'<div id="amz_not_avilable" class="amz_not_avilable">Dieses Produkt ist in Ihrem Land noch nicht verfügbar</div>\';
}
if (ICL_LANGUAGE_CODE == "en") {
$amz_btn_title = \'Buy on Amazon\';
$not_avilable_country = \'<div id="amz_not_avilable" class="amz_not_avilable">This product is not avilable in your country yet</div>\';
}
//////////////////////////////////////////////
// Geolocation Condition
if ($country_code=="DE" or $country_code=="DE" or $country_code=="AT" or $country_code=="CH" or $country_code=="LI" or $country_code=="EG") {
$associate_id = "bonstato-21";
$access_key = "HDUHWUIDIUWJDWDWDWD";
$secret_key = "HDUIWQDUQWUDJUIQJWDJWQD";
$amazon_domain = "amazon.de";
$asin = $de_asin;
}
/**********************************************************************************/
// Get price from amazon
$amazon = new AmazonAPI($associate_id , $access_key, $secret_key , $amazon_domain);
$item = $amazon->item_lookup($asin)->get_item_data();
if ($item->price != "0" && $item->price != null ) {
?><div class="amz_price_wrap_wrap" >Price: <?php echo $item->price; ?></div><?php
}
global $post;
$product = wc_get_product( $postId );
$type = $product->get_type();
if( $type == \'simple\' && $item->price != "0" && $item->price != null ){
if( wp_is_mobile() ) {
// Amazon Link For Mobile
?>
<div class="buy_amz_btn_wrap" >
<button type="button" id="buy_amz_btn" class="buy_amz_btn" onclick="window.location=\'https://<?php echo $amazon_domain ?>/dp/<?php echo $asin ?>/?tag=<?php echo $associate_id ?>\';"><i class="fa fa-amazon fa-amz"></i><?php echo $amz_btn_title ?></button>
</div>
<?php
}
else {
// Amazon Link For PC
?>
<div class="buy_amz_btn_wrap" >
<button type="button" id="buy_amz_btn" class="buy_amz_btn" onclick="window.location=\'https://<?php echo $amazon_domain ?>/gp/aws/cart/add.html?AssociateTag=<?php echo $associate_id ?>&ASIN.1=<?php echo $asin ?>&Quantity.1=1\';"><i class="fa fa-amazon fa-amz"></i><?php echo $amz_btn_title ?></button>
</div>
<?php
}
}
else if( $type == \'simple\' && $item->price == "0"){
echo $not_avilable_country;
}
if(is_null($item->price)){
echo $not_avilable_country;
}
die();
}