· 7 years ago · Sep 27, 2018, 11:34 PM
1function simple_amz_link_ajax() {
2 ?>
3 <script>
4 jQuery(document).ready(function(){
5
6 jQuery.ajax({
7 url: "<?php echo admin_url('admin-ajax.php?action=get_amz_btn'); ?>",
8 type: 'POST',
9 data: {
10 action: 'get_simple_amz_button',
11 postId: <?php echo get_post()->ID; ?>
12 },
13 dataType: 'html',
14 success: function(response) {
15 jQuery("#buy_amz_btn_wrap").html(response);
16
17 }
18
19 });
20 });
21 </script>
22 <!-- end Ajax call to get_simple_amz_button -->
23
24 <div id="buy_amz_btn_wrap">
25
26 <div class="spinner">
27
28 <div class="bounce1"></div>
29 <div class="bounce2"></div>
30 <div class="bounce3"></div>
31 </div>
32
33 </div>
34 <?php
35}
36
37
38
39add_action('wp_ajax_get_simple_amz_button', 'simple_amz_button');
40add_action('wp_ajax_nopriv_get_simple_amz_button', 'simple_amz_button');
41
42function simple_amz_button() {
43// Variables Declaration
44 $postId = filter_input( INPUT_POST, 'postId', FILTER_SANITIZE_NUMBER_INT );
45 $de_asin = get_post_meta( $postId, "wccaf_de_asin", true );
46
47 $country_code = $_SERVER ["HTTP_CF_IPCOUNTRY"];
48 $not_avilable_country = '<div id="amz_not_avilable" class="amz_not_avilable">This product is not avilable in your country yet</div>';
49
50 // Get Amazon Button Title
51 if (ICL_LANGUAGE_CODE == "de") {
52 $amz_btn_title = 'Kaufen auf Amazon';
53 $not_avilable_country = '<div id="amz_not_avilable" class="amz_not_avilable">Dieses Produkt ist in Ihrem Land noch nicht verfügbar</div>';
54 }
55 if (ICL_LANGUAGE_CODE == "en") {
56 $amz_btn_title = 'Buy on Amazon';
57 $not_avilable_country = '<div id="amz_not_avilable" class="amz_not_avilable">This product is not avilable in your country yet</div>';
58 }
59 //////////////////////////////////////////////
60 // Geolocation Condition
61 if ($country_code=="DE" or $country_code=="DE" or $country_code=="AT" or $country_code=="CH" or $country_code=="LI" or $country_code=="EG") {
62 $associate_id = "bonstato-21";
63 $access_key = "HDUHWUIDIUWJDWDWDWD";
64 $secret_key = "HDUIWQDUQWUDJUIQJWDJWQD";
65 $amazon_domain = "amazon.de";
66 $asin = $de_asin;
67 }
68
69
70
71 /**********************************************************************************/
72
73 // Get price from amazon
74
75 $amazon = new AmazonAPI($associate_id , $access_key, $secret_key , $amazon_domain);
76 $item = $amazon->item_lookup($asin)->get_item_data();
77 if ($item->price != "0" && $item->price != null ) {
78 ?><div class="amz_price_wrap_wrap" >Price: <?php echo $item->price; ?></div><?php
79 }
80
81 global $post;
82 $product = wc_get_product( $postId );
83 $type = $product->get_type();
84 if( $type == 'simple' && $item->price != "0" && $item->price != null ){
85 if( wp_is_mobile() ) {
86 // Amazon Link For Mobile
87 ?>
88 <div class="buy_amz_btn_wrap" >
89 <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>
90 </div>
91 <?php
92 }
93
94 else {
95 // Amazon Link For PC
96 ?>
97 <div class="buy_amz_btn_wrap" >
98 <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>
99 </div>
100 <?php
101 }
102 }
103
104 else if( $type == 'simple' && $item->price == "0"){
105 echo $not_avilable_country;
106 }
107
108
109 if(is_null($item->price)){
110 echo $not_avilable_country;
111 }
112
113
114
115
116
117die();
118
119}