Give_Payment::remove_donation PHP Method

remove_donation() public method

Remove a donation from the payment
Since: 1.5
public remove_donation ( integer $form_id, array $args = [] ) : boolean
$form_id integer The form ID to remove
$args array Arguments to pass to identify (quantity, amount, price_id)
return boolean If the item was removed or not
    public function remove_donation($form_id, $args = array())
    {
        // Set some defaults
        $defaults = array('quantity' => 1, 'price' => false, 'price_id' => false);
        $args = wp_parse_args($args, $defaults);
        $form = new Give_Donate_Form($form_id);
        // Bail if this post isn't a valid give donation form
        if (!$form || $form->post_type !== 'give_forms') {
            return false;
        }
        $pending_args = $args;
        $pending_args['id'] = $form_id;
        $pending_args['amount'] = $this->total;
        $pending_args['price_id'] = false !== $args['price_id'] ? (int) $args['price_id'] : false;
        $pending_args['quantity'] = $args['quantity'];
        $pending_args['action'] = 'remove';
        $this->pending['donations'][] = $pending_args;
        $this->decrease_subtotal($this->total);
        return true;
    }

Usage Example

 /**
  * Test Remove Donation Payment by Price ID
  */
 public function test_remove_with_multi_price_points_by_price_id()
 {
     Give_Helper_Payment::delete_payment($this->_payment_id);
     $form = Give_Helper_Form::create_multilevel_form();
     $payment = new Give_Payment();
     //Add a multi-level donation amount
     $payment->add_donation($form->ID, array('price_id' => 2));
     $this->assertEquals(25, $payment->total);
     $payment->status = 'complete';
     $payment->save();
     //Now remove it
     $payment->remove_donation($form->ID, array('price_id' => 2));
     $payment->save();
     $this->assertEmpty($payment->price_id);
     $this->assertEquals(0, $payment->total);
 }