• 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_Discount::add() – Create a new discount. If the discount already exists in the database, update it.

You appear to be a bot. Output may be restricted

Description

Create a new discount. If the discount already exists in the database, update it.

Usage

$mixed = EDD_Discount::add( $args );

Parameters

$args
( array ) required – Discount details.

Returns

mixed bool|int false if data isn't passed and class not instantiated for creation, or post ID for the new discount.

Source

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

1 to 74 of 74
  public function add( $args ) {

    // If no code is provided, return early with false
    if ( empty( $args['code'] ) ) {
      return false;
    }

    $meta = $this->build_meta( $args );

    if ( ! empty( $this->ID ) && $this->exists() ) {
      return $this->update( $args );
    } else {
      
/**
 * Add a new discount to the database.
 */

      
/**
 * Filters the metadata before being inserted into the database.
 *
 * @since 2.7
 *
 * @param array $meta Discount meta.
 * @param int   $ID   Discount ID.
 */
      $meta = apply_filters( 'edd_insert_discount', $meta );

      
/**
 * Fires before the discount has been added to the database.
 *
 * @since 2.7
 *
 * @param array $meta Discount meta.
 */
      do_action( 'edd_pre_insert_discount', $meta );

      $this->ID = wp_insert_post( array(
        'post_type'   => 'edd_discount',
        'post_title'  => $meta['name'],
        'post_status' => 'active'
      ) );

      foreach ( $meta as $key => $value ) {
        $this->update_meta( $key, $value );
      }

      
/**
 * Fires after the discount code is inserted.
 *
 * @param array $meta {
 *     The discount details.
 *
 *     @type string $code              The discount code.
 *     @type string $name              The name of the discount.
 *     @type string $status            The discount status. Defaults to active.
 *     @type int    $uses              The current number of uses.
 *     @type int    $max_uses          The max number of uses.
 *     @type string $start             The start date.
 *     @type int    $min_price         The minimum price required to use the discount code.
 *     @type array  $product_reqs      The product IDs required to use the discount code.
 *     @type string $product_condition The conditions in which a product(s) must meet to use the discount code.
 *     @type array  $excluded_products Product IDs excluded from this discount code.
 *     @type bool   $is_not_global     If the discount code is not globally applied to all products. Defaults to false.
 *     @type bool   $is_single_use     If the code cannot be used more than once per customer. Defaults to false.
 * }
 * @param int $ID The ID of the discount that was inserted.
 */
      do_action( 'edd_post_insert_discount', $meta, $this->ID );

      $this->setup_discount( WP_Post::get_instance( $this->ID ) );

      // Discount code created
      return $this->ID;
    }
  }
 

 View on GitHub View on Trac

Published: 26th November 2019 | Last updated: 13th March 2021

Primary Sidebar

Information

Function name: EDD_Discount::add
Class ref: EDD_Discount
Plugin ref: Easy Digital Downloads
Version: 2.11.6
Sourcefile: includes/class-edd-discount.php
File ref: includes/class-edd-discount.php
Deprecated?: No
API Letters: A,D,E

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