• 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_Payment::add_download() – Add a download to a given payment

You appear to be a bot. Output may be restricted

Description

Add a download to a given payment

Usage

$bool = EDD_Payment::add_download( $download_id, $args, $options );

Parameters

$download_id
( int ) optional – The download to add
$args
( array ) optional – Other arguments to pass to the function
$options
( array ) optional – List of download options

Returns

bool True when successful, false otherwise

Source

File name: easy-digital-downloads/includes/payments/class-edd-payment.php
Lines:

1 to 100 of 119
  public function add_download( $download_id = 0, $args = array(), $options = array() ) {
    $download = new EDD_Download( $download_id );

    // Bail if this post isn't a download
    if( ! $download || $download->post_type !== 'download' ) {
      return false;
    }

    // Set some defaults
    $defaults = array(
      'quantity'    => 1,
      'price_id'    => false,
      'item_price'  => false,
      'discount'    => 0,
      'tax'         => 0.00,
      'fees'        => array(),
    );

    $args = wp_parse_args( apply_filters( 'edd_payment_add_download_args', $args, $download->ID ), $defaults );

    // Allow overriding the price
    if( false !== $args['item_price'] ) {
      $item_price = $args['item_price'];
    } else {
      // Deal with variable pricing
      if( edd_has_variable_prices( $download->ID ) ) {
        $prices = get_post_meta( $download->ID, 'edd_variable_prices', true );

        if( $args['price_id'] && array_key_exists( $args['price_id'], (array) $prices ) ) {
          $item_price = $prices[$args['price_id']]['amount'];
        } else {
          $item_price       = edd_get_lowest_price_option( $download->ID );
          $args['price_id'] = edd_get_lowest_price_id( $download->ID );
        }
      } else {
        $item_price = edd_get_download_price( $download->ID );
      }
    }

    // Sanitizing the price here so we don't have a dozen calls later
    $item_price = edd_sanitize_amount( $item_price );
    $quantity   = edd_item_quantities_enabled() ? absint( $args['quantity'] ) : 1;
    $amount     = round( $item_price * $quantity, edd_currency_decimal_filter() );

    // Setup the downloads meta item
    $new_download = array(
      'id'       => $download->ID,
      'quantity' => $quantity,
    );

    $default_options = array(
      'quantity' => $quantity,
    );

    if ( false !== $args['price_id'] ) {
      $default_options['price_id'] = (int) $args['price_id'];
    }

    $options                 = wp_parse_args( $options, $default_options );
    $new_download['options'] = $options;

    $this->downloads[] = $new_download;

    $discount   = $args['discount'];
    $subtotal   = $amount;
    $tax        = $args['tax'];

    if ( edd_prices_include_tax() ) {
      $subtotal -= round( $tax, edd_currency_decimal_filter() );
    }

    $fees = 0;
    if ( ! empty( $args['fees'] ) && is_array( $args['fees'] ) ) {
      foreach ( $args['fees'] as $feekey => $fee ) {
        $fees += $fee['amount'];
      }

      $fees = round( $fees, edd_currency_decimal_filter() );
    }

    $total      = $subtotal - $discount + $tax + $fees;

    // Do not allow totals to go negative
    if( $total < 0 ) {
      $total = 0;
    }

    // Silly item_number array
    $item_number = array(
      'id'        => $download->ID,
      'quantity'  => $quantity,
      'options'   => $options,
    );

    $this->cart_details[] = array(
      'name'        => $download->post_title,
      'id'          => $download->ID,
      'item_number' => $item_number,
      'item_price'  => round( $item_price, edd_currency_decimal_filter() ),
      'quantity'    => $quantity,
 
[1] [2] Next »

 View on GitHub View on Trac

Published: 26th November 2019 | Last updated: 6th June 2020

Primary Sidebar

Information

Function name: EDD_Payment::add_download
Class ref: EDD_Payment
Plugin ref: Easy Digital Downloads
Version: 2.11.6
Sourcefile: includes/payments/class-edd-payment.php
File ref: includes/payments/class-edd-payment.php
Deprecated?: No
API Letters: A,D,E,P

Footer

Easy Digital Downloads a2z
Easy Digital Downloads a2z
Easy Digital Downloads a2z
WordPress 5.9.3
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-2022. All rights reserved.


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

  • Home
  • Blog
  • Sitemap
  • Sites