1. Home
  2. Developers
  3. wpinv_update_invoice()

wpinv_update_invoice()

Introduction | Parameters | Examples

Introduction

Update an invoice.

wpinv_update_invoice( array $invoice_data = array(), bool $wp_error = false )

This method allows you to add/remove items to an invoice, add/remove discount for invoice, update billing address, update payment details etc. To update an invoice the $invoice_data must have a parameter ‘ID’ with an invoice ID. Only invoice with status “pending payment” is allowed to updated. Recurring items are paid individually, so recurring item can not be added if invoice contains items other then that recurring item.

Parameters

$invoice_data

(array) (Required) An array of elements to update an invoice.

  • ID
    (int) (Required) The invoice ID. The invoice with this ID will be updated.
  • status
    (string) (Optional) The invoice status. Learn more about invoice statuses.
  • post_date
    (string) (Optional) The date-time in Y-m-d H:i:s format.
  • cart_details
    (array) (Optional) This contains invoice items that needs to be added to an invoice or removed from an invoice.
    • add_items
      (array) (Optional) Items details to add new items or to increase the quantity of existing items.
      • id
        (int) (Required) The item id to be added or updated.
      • quantity
        (int) (Optional) The item quantity to add for new item or increase for existing item.
        Default: 1
      • custom_price
        (float) (Optional) The item custom price. This price will overwrite the item price.
      • meta
        (array) (Optional) The item meta. This stores custom values that to be used for this line item.
    • remove_items
      (array) (Optional) Items details to remove existing items or to decrease the quantity of existing items.
      • id
        (int) (Required) The existing item id to be removed or updated.
      • quantity
        (int) (Optional) The item quantity to decrease.
        Default: 1
  • payment_details
    (array) (Optional) The invoice payment details. Required for paid invoice.
    • gateway
      (string) (Optional) The payment gateway name. Required for paid invoice.
    • transaction_id
      (string) (Optional) The payment transaction id for paid invoices.
  • user_info
    (array) (Optional) User billing address details.
    • first_name
      (string) (Optional) Billing address first name.
    • last_name
      (string) (Optional) Billing address last name or surname.
    • phone
      (string) (Optional) Billing address phone number.
    • address
      (string) (Optional) Billing address street name/street no./block no etc.
    • city
      (string) (Optional) Billing address city.
    • country
      (string) (Optional) The country code (ISO2). ex: US
    • state
      (string) (Optional) The state name or state code.
    • zip
      (string) (Optional) Billing address postcode.
    • company
      (string) (Optional) User’s company name.
    • vat_number
      (string) (Optional) User’s vat number.
    • discount
      (string) (Optional) The coupon code to apply discount. Use ‘none’ to remove existing discount.
  • due_date
    (string) (Optional) The invoice due date in Y-m-d. Only for invoice which has payment pending.
  • ip
    (string) (Optional) IP address of the system from which invoice created/paid.
  • user_note
    (string) (Optional) Short message or note for user. User notes will be included in user invoice email sent to users.
  • private_note
    (string) (Optional) Short message or note for system use. Only admin can see private notes from backend invoice page.
  • parent
    (int) (Optional) For renewal payment only. Parent invoice ID for which the renewal payment is being paid.
$wp_error

(bool) (Optional) Whether to return a WP_Error on failure.

Default: false

Return

(int|object|WP_Error) The value 0 or WP_Error on failure. The WPInv_Invoice object on success.

Examples

Example 1:

Add a new items to invoice. It will increase quantity if an item has already been added to invoice.

$data = array(
    'ID'            => 2559,
    'cart_details'  => array(
        'add_items'     => array( 
            array(
                'id'            => 2517,
                'quantity'      => 1,
                'meta'          => array(
                    'post_id'   => 1239
                )
            ),
            array(
                'id'            => 1864,
                'quantity'      => 2,
                'custom_price'  => '10.00',
            )
        ),
    ),
);

$invoice = wpinv_update_invoice( $data, true );

View at GitHub: https://github.com/AyeCode/wpinv-example/blob/master/invoices/update-invoice-add-item-to-invoice.php

Example 2:

Increase quantity for existing item. Following example will increase item quantity by 2.

$data = array(
    'ID'            => 2559,
    'cart_details'  => array(
        'add_items'     => array( 
            array(
                'id'        => 2517,
                'quantity'  => 2,
            )
        ),
    ),
);

$invoice = wpinv_update_invoice( $data, true );

View at GitHub: https://github.com/AyeCode/wpinv-example/blob/master/invoices/update-invoice-increase-item-quantity.php

Example 3:

Remove items or decrease quantity for existing invoice items. Following example will decrease item quantity by 1. If item reaches to the zero quantity then item will be removed from invoice items.

$data = array(
    'ID'            => 2559,
    'cart_details'  => array(
        'remove_items'  => array( 
            array(
                'id'        => 2517,
                'quantity'  => 1,
            )
        ),
    ),
);

$invoice = wpinv_update_invoice( $data, true );

View at GitHub: https://github.com/AyeCode/wpinv-example/blob/master/invoices/update-invoice-increase-item-quantity.php

Example 4:

Apply discount coupon to invoice. It will overwrite existing discount if invoice has other discount coupon already applied.

$data = array(
    'ID'            => 2559,
    'user_info'     => array(
        'discount'      => '10P'
    ),
);

$invoice = wpinv_update_invoice( $data, true );

View at GitHub: https://github.com/AyeCode/wpinv-example/blob/master/invoices/update-invoice-apply-discount.php

Example 5:

Remove discount applied to the invoice. Use ‘none’ to remove discount applied to the invoice.

$data = array(
    'ID'            => 2559,
    'user_info'     => array(
        'discount'      => 'none'
    ),
);

$invoice = wpinv_update_invoice( $data, true );

View at GitHub: https://github.com/AyeCode/wpinv-example/blob/master/invoices/update-invoice-remove-discount.php

Example 6:

Update invoice payment details.

$data = array(
    'ID'                => 2559,
    'payment_details'   => array(
        'gateway'           => 'paypal',
        'transaction_id'    => '65756756756756'
    ),
);

$invoice = wpinv_update_invoice( $data, true );

View at GitHub: https://github.com/AyeCode/wpinv-example/blob/master/invoices/update-invoice-payment-details.php

Example 7:

Update invoice user billing address.

$data = array(
    'ID'                => 2559,
    'user_info'         => array(
        'first_name'        => 'GD',
        'last_name'         => 'User',
        'phone'             => '+91 1119348834',
        'address'           => 'Kankariya Lake',
        'city'              => 'Ahmedabad',
        'country'           => 'IN',
        'state'             => 'GJ',
        'zip'               => '380002',
        'company'           => 'GD INC',
        'vat_number'        => 'GB684565898',
    ),
);

$invoice = wpinv_update_invoice( $data, true );

View at GitHub: https://github.com/AyeCode/wpinv-example/blob/master/invoices/update-invoice-billing-address.php

Example 8:

Update invoice other details like invoice date, IP address, payment due date, add user note, add private note etc.

$data = array(
    'ID'              => 2559,
    'status'          => 'wpi-pending',
    'post_date'       => '2017-08-17 18:50:30',
    'ip'              => '1.39.51.23',
    'due_date'        => '2017-08-20',
    'private_note'    => 'This is a private note.',
    'user_note'       => 'This is a user note.',
);

$invoice = wpinv_update_invoice( $data, true );

View at GitHub: https://github.com/AyeCode/wpinv-example/blob/master/invoices/update-invoice-misc-details.php

Example 9:

Update all invoice details at once.

$data = array(
    'ID'              => 2559,
    'status'          => 'wpi-pending',
    'post_date'       => '2017-08-16 18:50:30',
    'cart_details'    => array(
        'add_items'     => array( 
            array(
                'id'            => 2517,
                'quantity'      => 1,
                'meta'          => array(
                    'post_id'   => 1239
                )
            ),
            array(
                'id'            => 1864,
                'quantity'      => 2,
                'custom_price'  => '10.00',
            ),
        ),
        'remove_items'  => array(
            array(
                'id'            => 1864,
                'quantity'      => 1,
            ),
        ),
    ),
    'user_info'         => array(
        'first_name'        => 'GD',
        'last_name'         => 'User',
        'phone'             => '+91 1119348834',
        'address'           => 'Kankariya Lake',
        'city'              => 'Ahmedabad',
        'country'           => 'IN',
        'state'             => 'GJ',
        'zip'               => '380002',
        'company'           => 'GD INC',
        'vat_number'        => 'GB684565898',
        'discount'          => '10P'
    ),
    'payment_details'   => array(
        'gateway'           => 'paypal',
        'transaction_id'    => '65756756756756'
    ),
    'ip'              => '1.39.51.23',
    'due_date'        => '2017-08-19',
    'private_note'    => 'This is a private note.',
    'user_note'       => 'This is a user note.',
);

$invoice = wpinv_update_invoice( $data, true );

View at GitHub: https://github.com/AyeCode/wpinv-example/blob/master/invoices/update-invoice-all-details.php

Was this helpful to you? Yes No