edd_ajax_add_to_cart

Adds item to the cart via AJAX.

Description

edd_ajax_add_to_cart(); 

Usage

  1. if ( !function_exists( 'edd_ajax_add_to_cart' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'easy-digital-downloads/includes/ajax-functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = edd_ajax_add_to_cart(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/includes/ajax-functions.php  
  1. function edd_ajax_add_to_cart() { 
  2. if ( isset( $_POST['download_id'] ) ) { 
  3. $to_add = array(); 
  4.  
  5. if ( isset( $_POST['price_ids'] ) && is_array( $_POST['price_ids'] ) ) { 
  6. foreach ( $_POST['price_ids'] as $price ) { 
  7. $to_add[] = array( 'price_id' => $price ); 
  8.  
  9. $items = ''; 
  10.  
  11. foreach ( $to_add as $options ) { 
  12.  
  13. if( $_POST['download_id'] == $options['price_id'] ) { 
  14. $options = array(); 
  15.  
  16. parse_str( $_POST['post_data'], $post_data ); 
  17.  
  18. if( isset( $options['price_id'] ) && isset( $post_data['edd_download_quantity_' . $options['price_id'] ] ) ) { 
  19.  
  20. $options['quantity'] = absint( $post_data['edd_download_quantity_' . $options['price_id'] ] ); 
  21.  
  22. } else { 
  23.  
  24. $options['quantity'] = isset( $post_data['edd_download_quantity'] ) ? absint( $post_data['edd_download_quantity'] ) : 1; 
  25.  
  26.  
  27. $key = edd_add_to_cart( $_POST['download_id'], $options ); 
  28.  
  29. $item = array( 
  30. 'id' => $_POST['download_id'],  
  31. 'options' => $options 
  32. ); 
  33.  
  34. $item = apply_filters( 'edd_ajax_pre_cart_item_template', $item ); 
  35. $items .= html_entity_decode( edd_get_cart_item_template( $key, $item, true ), ENT_COMPAT, 'UTF-8' ); 
  36.  
  37.  
  38. $return = array( 
  39. 'subtotal' => html_entity_decode( edd_currency_filter( edd_format_amount( edd_get_cart_subtotal() ) ), ENT_COMPAT, 'UTF-8' ),  
  40. 'total' => html_entity_decode( edd_currency_filter( edd_format_amount( edd_get_cart_total() ) ), ENT_COMPAT, 'UTF-8' ),  
  41. 'cart_item' => $items,  
  42. 'cart_quantity' => html_entity_decode( edd_get_cart_quantity() ) 
  43. ); 
  44.  
  45. if ( edd_use_taxes() ) { 
  46. $cart_tax = (float) edd_get_cart_tax(); 
  47. $return['tax'] = html_entity_decode( edd_currency_filter( edd_format_amount( $cart_tax ) ), ENT_COMPAT, 'UTF-8' ); 
  48.  
  49. echo json_encode( $return ); 
  50. edd_die();