• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Easy Digital Downloads a2z

Easy Digital Downloads a2z

Easy Digital Downloads a2z

  • Home
  • Plugins
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / edd_ajax_add_to_cart() – Adds item to the cart via AJAX.

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();
}
 

 View on GitHub View on Trac

Published: 26th November 2019 | Last updated: 26th November 2019

Primary Sidebar

Information

Function name: edd_ajax_add_to_cart
Plugin ref: Easy Digital Downloads
Version: 2.10.1
Sourcefile: includes/ajax-functions.php
File ref: includes/ajax-functions.php
Deprecated?: No
API Letters: A,C,E,T

Footer

Easy Digital Downloads a2z
Easy Digital Downloads a2z
Easy Digital Downloads a2z
WordPress 5.7.1
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  edd.wp-a2z.org
© Copyright Easy Digital Downloads a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites