You appear to be a bot. Output may be restricted
Description
Adds item to the cart via AJAX.
Usage
$void = edd_ajax_add_to_cart();
Parameters
Returns
void
Source
File name: easy-digital-downloads/includes/ajax-functions.php
Lines:
1 to 73 of 73
function edd_ajax_add_to_cart() { if ( ! isset( $_POST['nonce'] ) ) { edd_debug_log( __( 'Missing nonce when adding an item to the cart. Please read the following for more information: https://easydigitaldownloads.com/development/2018/07/05/important-update-to-ajax-requests-in-easy-digital-downloads-2-9-4', 'easy-digital-downloads' ), true ); } if ( isset( $_POST['download_id'] ) && isset( $_POST['nonce'] ) ) { $download_id = absint( $_POST['download_id'] ); $nonce = sanitize_text_field( $_POST['nonce'] ); $nonce_verified = wp_verify_nonce( $nonce, 'edd-add-to-cart-' . $download_id ); if ( false === $nonce_verified ) { edd_die( '', '', 403 ); } $to_add = array(); if ( isset( $_POST['price_ids'] ) && is_array( $_POST['price_ids'] ) ) { foreach ( $_POST['price_ids'] as $price ) { $to_add[] = array( 'price_id' => $price ); } } $items = ''; foreach ( $to_add as $options ) { if( $_POST['download_id'] == $options['price_id'] ) { $options = array(); } parse_str( $_POST['post_data'], $post_data ); if( isset( $options['price_id'] ) && isset( $post_data['edd_download_quantity_' . $options['price_id'] ] ) ) { $options['quantity'] = absint( $post_data['edd_download_quantity_' . $options['price_id'] ] ); } else { $options['quantity'] = isset( $post_data['edd_download_quantity'] ) ? absint( $post_data['edd_download_quantity'] ) : 1; } $key = edd_add_to_cart( $_POST['download_id'], $options ); $item = array( 'id' => $_POST['download_id'], 'options' => $options ); $item = apply_filters( 'edd_ajax_pre_cart_item_template', $item ); $items .= html_entity_decode( edd_get_cart_item_template( $key, $item, true ), ENT_COMPAT, 'UTF-8' ); } $return = array( 'subtotal' => html_entity_decode( edd_currency_filter( edd_format_amount( edd_get_cart_subtotal() ) ), ENT_COMPAT, 'UTF-8' ), 'total' => html_entity_decode( edd_currency_filter( edd_format_amount( edd_get_cart_total() ) ), ENT_COMPAT, 'UTF-8' ), 'cart_item' => $items, 'cart_quantity' => html_entity_decode( edd_get_cart_quantity() ) ); if ( edd_use_taxes() ) { $cart_tax = (float) edd_get_cart_tax(); $return['tax'] = html_entity_decode( edd_currency_filter( edd_format_amount( $cart_tax ) ), ENT_COMPAT, 'UTF-8' ); } $return = apply_filters( 'edd_ajax_add_to_cart_response', $return ); echo json_encode( $return ); } edd_die(); }