Example #1
0
/**
 * Displays a formatted price for a donation form
 *
 * @since 1.0
 *
 * @param int  $form_id  ID of the form price to show
 * @param bool $echo     Whether to echo or return the results
 * @param int  $price_id Optional price id for variable pricing
 *
 * @return void
 */
function give_price($form_id = 0, $echo = true, $price_id = false)
{
    if (empty($form_id)) {
        $form_id = get_the_ID();
    }
    if (give_has_variable_prices($form_id)) {
        $prices = give_get_variable_prices($form_id);
        if (false !== $price_id) {
            //loop through multi-prices to see which is default
            foreach ($prices as $price) {
                //this is the default price
                if (isset($price['_give_default']) && $price['_give_default'] === 'default') {
                    $price = (double) $price['_give_amount'];
                }
            }
        } else {
            $price = give_get_lowest_price_option($form_id);
        }
        $price = give_sanitize_amount($price);
    } else {
        $price = give_get_form_price($form_id);
    }
    $price = apply_filters('give_form_price', give_sanitize_amount($price), $form_id);
    $formatted_price = '<span class="give_price" id="give_price_' . $form_id . '">' . $price . '</span>';
    $formatted_price = apply_filters('give_form_price_after_html', $formatted_price, $form_id, $price);
    if ($echo) {
        echo $formatted_price;
    } else {
        return $formatted_price;
    }
}
Example #2
0
 /**
  * Add a donation to a given payment
  *
  * @since  1.5
  * @access public
  *
  * @param  int   $form_id The donation form to add
  * @param  array $args Other arguments to pass to the function
  * @param  array $options List of donation options
  *
  * @return bool           True when successful, false otherwise
  */
 public function add_donation($form_id = 0, $args = array(), $options = array())
 {
     $donation = new Give_Donate_Form($form_id);
     // Bail if this post isn't a give donation form
     if (!$donation || $donation->post_type !== 'give_forms') {
         return false;
     }
     // Set some defaults
     $defaults = array('price' => false, 'price_id' => false, 'fees' => array());
     $args = wp_parse_args(apply_filters('give_payment_add_donation_args', $args, $donation->ID), $defaults);
     // Allow overriding the price
     if (false !== $args['price']) {
         $item_price = $args['price'];
     } else {
         // Deal with variable pricing
         if (give_has_variable_prices($donation->ID)) {
             $prices = maybe_unserialize(get_post_meta($form_id, '_give_donation_levels', true));
             $item_price = '';
             //Loop through prices
             foreach ($prices as $price) {
                 //Find a match between price_id and level_id
                 //First verify array keys exists THEN make the match
                 if (isset($args['price_id']) && isset($price['_give_id']['level_id']) && $args['price_id'] == $price['_give_id']['level_id']) {
                     $item_price = $price['_give_amount'];
                 }
             }
             //Fallback to the lowest price point
             if ($item_price == '') {
                 $item_price = give_get_lowest_price_option($donation->ID);
                 $args['price_id'] = give_get_lowest_price_id($donation->ID);
             }
         } else {
             //Simple form price
             $item_price = give_get_form_price($donation->ID);
         }
     }
     // Sanitizing the price here so we don't have a dozen calls later
     $item_price = give_sanitize_amount($item_price);
     $total = round($item_price, give_currency_decimal_filter());
     //Add Options
     $default_options = array();
     if (false !== $args['price_id']) {
         $default_options['price_id'] = (int) $args['price_id'];
     }
     $options = wp_parse_args($options, $default_options);
     // Do not allow totals to go negative
     if ($total < 0) {
         $total = 0;
     }
     $donation = array('name' => $donation->post_title, 'id' => $donation->ID, 'price' => round($total, give_currency_decimal_filter()), 'subtotal' => round($total, give_currency_decimal_filter()), 'fees' => $args['fees'], 'price_id' => $args['price_id'], 'action' => 'add', 'options' => $options);
     $this->pending['donations'][] = $donation;
     $this->increase_subtotal($total);
     return true;
 }