• 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::insert_discount() – Build the base of the discount.

You appear to be a bot. Output may be restricted

Description

Build the base of the discount.

Usage

$int|bool = EDD_Discount::insert_discount();

Parameters

Returns

int|bool Discount ID or false on error.

Source

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

1 to 72 of 72
  private function insert_discount() {
    $discount_data = array(
      'code'              => isset( $this->code )              ? $this->code              : '',
      'name'              => isset( $this->name )              ? $this->name              : '',
      'status'            => isset( $this->status )            ? $this->status            : 'active',
      'uses'              => isset( $this->uses )              ? $this->uses              : '',
      'max_uses'          => isset( $this->max_uses )          ? $this->max_uses          : '',
      'amount'            => isset( $this->amount )            ? $this->amount            : '',
      'start'             => isset( $this->start )             ? $this->start             : '',
      'expiration'        => isset( $this->expiration )        ? $this->expiration        : '',
      'type'              => isset( $this->type )              ? $this->type              : '',
      'min_price'         => isset( $this->min_price )         ? $this->min_price        : '',
      'product_reqs'      => isset( $this->product_reqs )      ? $this->product_reqs      : array(),
      'product_condition' => isset( $this->product_condition ) ? $this->product_condition : '',
      'excluded_products' => isset( $this->excluded_products ) ? $this->excluded_products : array(),
      'is_not_global'     => isset( $this->is_not_global )     ? $this->is_not_global     : false,
      'is_single_use'     => isset( $this->is_single_use )     ? $this->is_single_use     : false,
    );

    $start_timestamp = strtotime( $discount_data['start'] );

    if ( ! empty( $discount_data['start'] ) ) {
      $discount_data['start'] = date( 'm/d/Y H:i:s', $start_timestamp );
    }

    if ( ! empty( $discount_data['expiration'] ) ) {
      $discount_data['expiration'] = date( 'm/d/Y H:i:s', strtotime( date( 'm/d/Y', strtotime( $discount_data['expiration'] ) ) . ' 23:59:59' ) );
      $end_timestamp = strtotime( $discount_data['expiration'] );

      if ( ! empty( $discount_data['start'] ) && $start_timestamp > $end_timestamp ) {
        // Set the expiration date to the start date if start is later than expiration
        $discount_data['expiration'] = $discount_data['start'];
      }
    }

    if ( ! empty( $discount_data['excluded_products'] ) ) {
      foreach ( $discount_data['excluded_products'] as $key => $product ) {
        if ( 0 === intval( $product ) ) {
          unset( $discount_data['excluded_products'][ $key ] );
        }
      }
    }

    $args = apply_filters( 'edd_insert_discount_args', array(
      'post_title'    => $this->name,
      'post_status'   => $discount_data['status'],
      'post_type'     => 'edd_discount',
      'post_date'     => ! empty( $this->date ) ? $this->date : null,
      'post_date_gmt' => ! empty( $this->date ) ? get_gmt_from_date( $this->date ) : null
    ), $discount_data );

    // Create a blank edd_discount post
    $discount_id = wp_insert_post( $args );

    if ( ! empty( $discount_id ) ) {

      $this->ID  = $discount_id;

      foreach ( $discount_data as $key => $value ) {

        if( ! empty( $value ) ) {

          $this->update_meta( $key, $value );

        }

      }

    }

    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::insert_discount
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: D,E,I

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