1 <?php
2 3 4 5 6 7 8 9 10 11 12
13
14
15 if ( ! defined( 'ABSPATH' ) ) exit;
16
17 18 19 20 21 22
23 function edd_get_ajax_url() {
24 $scheme = defined( 'FORCE_SSL_ADMIN' ) && FORCE_SSL_ADMIN ? 'https' : 'admin';
25
26 $current_url = edd_get_current_page_url();
27 $ajax_url = admin_url( 'admin-ajax.php', $scheme );
28
29 if ( preg_match( '/^https/', $current_url ) && ! preg_match( '/^https/', $ajax_url ) ) {
30 $ajax_url = preg_replace( '/^http/', 'https', $ajax_url );
31 }
32
33 return apply_filters( 'edd_ajax_url', $ajax_url );
34 }
35
36 37 38 39 40 41
42 function edd_ajax_remove_from_cart() {
43 if ( isset( $_POST['cart_item'] ) && check_ajax_referer( 'edd_ajax_nonce', 'nonce' ) ) {
44 edd_remove_from_cart( $_POST['cart_item'] );
45 echo 'removed';
46 }
47 die();
48 }
49 add_action( 'wp_ajax_edd_remove_from_cart', 'edd_ajax_remove_from_cart' );
50 add_action( 'wp_ajax_nopriv_edd_remove_from_cart', 'edd_ajax_remove_from_cart' );
51
52 53 54 55 56 57
58 function edd_ajax_add_to_cart() {
59 if ( isset( $_POST['download_id'] ) && check_ajax_referer( 'edd_ajax_nonce', 'nonce' ) ) {
60 global $post;
61
62 $to_add = array();
63
64 if ( isset( $_POST['price_ids'] ) && is_array( $_POST['price_ids'] ) ) {
65 foreach ( $_POST['price_ids'] as $price ) {
66 $to_add[] = array( 'price_id' => $price );
67 }
68 }
69
70 foreach ( $to_add as $options ) {
71 if ( ! edd_item_in_cart( $_POST['download_id'], $options ) ) {
72 $key = edd_add_to_cart( $_POST['download_id'], $options );
73
74 $item = array(
75 'id' => $_POST['download_id'],
76 'options' => $options
77 );
78
79 $item = apply_filters( 'edd_ajax_pre_cart_item_template', $item );
80
81 $cart_item = edd_get_cart_item_template( $key, $item, true );
82
83 echo $cart_item;
84 } else {
85 echo 'incart';
86 }
87 }
88 }
89 die();
90 }
91 add_action( 'wp_ajax_edd_add_to_cart', 'edd_ajax_add_to_cart' );
92 add_action( 'wp_ajax_nopriv_edd_add_to_cart', 'edd_ajax_add_to_cart' );
93
94 95 96 97 98 99
100 function edd_ajax_apply_discount() {
101 if ( isset( $_POST['code'] ) && check_ajax_referer( 'edd_checkout_nonce', 'nonce' ) ) {
102 $user = isset( $_POST['user'] ) ? $_POST['user'] : $_POST['email'];
103
104 $return = array(
105 'msg' => '',
106 'code' => $_POST['code']
107 );
108
109 if ( edd_is_discount_used( $_POST['code'], $user ) ) {
110 $return['msg'] = __('This discount code has been used already', 'edd');
111 } else {
112 if ( edd_is_discount_valid( $_POST['code'], $user ) ) {
113 $discount = edd_get_discount_by_code( $_POST['code'] );
114 $amount = edd_format_discount_rate( edd_get_discount_type( $discount->ID ), edd_get_discount_amount( $discount->ID ) );
115 $discounts = edd_set_cart_discount( $_POST['code'] );
116 $total = edd_get_cart_total( $discounts );
117
118 $return = array(
119 'msg' => 'valid',
120 'amount' => $amount,
121 'total' => html_entity_decode( edd_currency_filter( edd_format_amount( $total ) ), ENT_COMPAT, 'UTF-8' ),
122 'code' => $_POST['code'],
123 'html' => edd_get_cart_discounts_html( $discounts )
124 );
125 } else {
126 $return['msg'] = __('The discount you entered is invalid', 'edd');
127 }
128 }
129 echo json_encode($return);
130 }
131 die();
132 }
133 add_action( 'wp_ajax_edd_apply_discount', 'edd_ajax_apply_discount' );
134 add_action( 'wp_ajax_nopriv_edd_apply_discount', 'edd_ajax_apply_discount' );
135
136 137 138 139 140 141
142 function edd_load_checkout_login_fields() {
143 do_action( 'edd_purchase_form_login_fields' );
144 die();
145 }
146 add_action('wp_ajax_nopriv_checkout_login', 'edd_load_checkout_login_fields');
147
148 149 150 151 152 153
154 function edd_load_checkout_register_fields() {
155 do_action( 'edd_purchase_form_register_fields' );
156 die();
157 }
158 add_action('wp_ajax_nopriv_checkout_register', 'edd_load_checkout_register_fields');
159
160 161 162 163 164 165
166 function edd_ajax_get_download_title() {
167 if ( isset( $_POST['download_id'] ) ) {
168 $title = get_the_title( $_POST['download_id'] );
169 if ( $title ) {
170 echo $title;
171 } else {
172 echo 'fail';
173 }
174 }
175 die();
176 }
177 add_action( 'wp_ajax_edd_get_download_title', 'edd_ajax_get_download_title' );
178 add_action( 'wp_ajax_nopriv_edd_get_download_title', 'edd_ajax_get_download_title' );
179
180 181 182 183 184 185
186 function edd_ajax_opt_into_local_taxes() {
187 if ( ! check_ajax_referer( 'edd_checkout_nonce', 'nonce' ) )
188 return false;
189
190 edd_opt_into_local_taxes();
191
192 ob_start();
193 edd_checkout_cart();
194 $cart = ob_get_contents();
195 ob_end_clean();
196
197 $response = array(
198 'html' => $cart,
199 'total' => html_entity_decode( edd_cart_total( false ), ENT_COMPAT, 'UTF-8' ),
200 );
201
202 echo json_encode( $response );
203
204 exit;
205 }
206 add_action( 'wp_ajax_edd_local_tax_opt_in', 'edd_ajax_opt_into_local_taxes' );
207 add_action( 'wp_ajax_nopriv_edd_local_tax_opt_in', 'edd_ajax_opt_into_local_taxes' );
208
209 210 211 212 213 214
215 function edd_ajax_opt_out_local_taxes() {
216 if ( ! check_ajax_referer( 'edd_checkout_nonce', 'nonce' ) )
217 return false;
218
219 edd_opt_out_local_taxes();
220
221 ob_start();
222 edd_checkout_cart();
223 $cart = ob_get_contents();
224 ob_end_clean();
225
226 $response = array(
227 'html' => $cart,
228 'total' => html_entity_decode( edd_cart_total( false ), ENT_COMPAT, 'UTF-8' ),
229 );
230
231 echo json_encode( $response );
232
233 exit;
234 }
235 add_action( 'wp_ajax_edd_local_tax_opt_out', 'edd_ajax_opt_out_local_taxes' );
236 add_action( 'wp_ajax_nopriv_edd_local_tax_opt_out', 'edd_ajax_opt_out_local_taxes' );
237
238 239 240 241 242 243 244 245 246 247 248 249
250 function edd_check_for_download_price_variations() {
251 if ( isset( $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'edd_add_downloads_to_purchase_nonce' ) ) {
252
253 $download_id = intval( $_POST['download_id'] );
254
255 if ( edd_has_variable_prices( $download_id ) ) {
256 $variable_prices = get_post_meta( $download_id, 'edd_variable_prices', true );
257
258 if ( $variable_prices ) {
259 $ajax_response = '<select name="downloads[' . intval( $_POST['array_key'] ) . '][options][price_id]" class="edd-variable-prices-select">';
260 foreach ( $variable_prices as $key => $price ) {
261 $ajax_response .= '<option value="' . $key . '">' . $price['name'] . '</option>';
262 }
263 $ajax_response .= '</select>';
264 }
265
266 echo $ajax_response;
267 }
268
269 die();
270 }
271 }
272 add_action( 'wp_ajax_edd_check_for_download_price_variations', 'edd_check_for_download_price_variations' );