Easy Digital Downloads
  • Package
  • Function
  • Tree

Packages

  • EDD
    • Admin
      • Actions
      • Add-ons
      • Dashboard
      • Discounts
      • Downloads
      • Export
      • Notices
      • Pages
      • Payments
      • Reports
      • Settings
      • System
      • Upgrades
      • Upload
      • Welcome
    • Cart
    • Checkout
    • Classes
      • API
      • Fees
      • HTML
      • Roles
      • Session
    • Emails
    • Functions
      • AJAX
      • Compatibility
      • Errors
      • Formatting
      • Install
      • Login
      • Taxes
      • Templates
    • Gateways
    • Logging
    • Payments
    • Shortcodes
    • Widgets

Functions

  • edd_ajax_add_to_cart
  • edd_ajax_apply_discount
  • edd_ajax_get_download_title
  • edd_ajax_opt_into_local_taxes
  • edd_ajax_opt_out_local_taxes
  • edd_ajax_remove_from_cart
  • edd_check_for_download_price_variations
  • edd_get_ajax_url
  • edd_load_checkout_login_fields
  • edd_load_checkout_register_fields
  1 <?php
  2 /**
  3  * AJAX Functions
  4  *
  5  * Process the front-end AJAX actions.
  6  *
  7  * @package     EDD
  8  * @subpackage  Functions/AJAX
  9  * @copyright   Copyright (c) 2013, Pippin Williamson
 10  * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
 11  * @since       1.0
 12  */
 13 
 14 // Exit if accessed directly
 15 if ( ! defined( 'ABSPATH' ) ) exit;
 16 
 17 /**
 18  * Get AJAX URL
 19  *
 20  * @since 1.3
 21  * @return string
 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  * Removes item from cart via AJAX.
 38  *
 39  * @since 1.0
 40  * @return void
 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  * Adds item to the cart via AJAX.
 54  *
 55  * @since 1.0
 56  * @return void
 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  * Validates the supplied discount sent via AJAX.
 96  *
 97  * @since 1.0
 98  * @return void
 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 ) ) {  // Called twice if discount is not used (again by edd_is_discount_valid) but allows for beter usr msg and less execution if discount is used.
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  * Loads Checkout Login Fields the via AJAX
138  *
139  * @since 1.0
140  * @return void
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  * Load Checkout Register Fields via AJAX
150  *
151  * @since 1.0
152  * @return void
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  * Get Download Title via AJAX (used only in WordPress Admin)
162  *
163  * @since 1.0
164  * @return void
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  * Opt into local taxes via AJAX
182  *
183  * @since 1.4.1
184  * @return void
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  * Opt out of local taxes via AJAX
211  *
212  * @since 1.4.1
213  * @return void
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  * Check for Download Price Variations via AJAX (this function can only be used
240  * in WordPress Admin). This function isused for the Edit Payment screen when downloads
241  * are added to the purchase. When each download is chosen, an AJAX call is fired
242  * to this function which will check if variable prices exist for that download.
243  * If they do, it will output a dropdown of all the variable prices available for
244  * that download.
245  *
246  * @author Sunny Ratilal
247  * @since 1.5
248  * @return void
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' );
Easy Digital Downloads API documentation generated by ApiGen 2.8.0