1. Home
  2. Developers
  3. wpinv_insert_invoice()

wpinv_insert_invoice()

Introduction | Parameters | Examples

Introduction

Create an invoice.

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

Parameters

$invoice_data

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

  • status
    (string) (Optional) The invoice status. Learn more about invoice statuses.
    Default: ‘wpi-pending’
  • user_id
    (int) (Optional) The invoice user id.
    Default: user ID of current logged user
  • post_date
    (string) (Optional) The date-time in Y-m-d H:i:s format.
    Default: Default is the current time.
  • cart_details
    (array) (Required) The invoice items details.
    • id
      (int) (Required) The item id.
    • quantity
      (int) (Optional) The item quantity.
      Default: 1
    • custom_price
      (float) (Optional) The item custom price. This price will overwrite the item price.
      Default: empty
    • meta
      (array) (Optional) The item meta. This stores custom values that to be used for this line item.
  • payment_details
    (array) (Optional) The invoice payment details. Required for paid invoice.
    • gateway
      (string) (Optional) The payment gateway name. Required for paid invoice.
      Default: ‘manual’
    • currency
      (string) (Optional) The currency code (ISO 4217). ex: USD
      Default: Store’s default currency.
    • transaction_id
      (string) (Optional) The payment transaction id for paid invoices.
      Default: Current invoice ID.
  • user_info
    (array) (Optional) User billing address details.
    • first_name
      (string) (Optional) Billing address first name.
      Default: User’s saved first name.
    • last_name
      (string) (Optional) Billing address last name or surname.
      Default: User’s saved last name.
    • phone
      (string) (Optional) Billing address phone number.
      Default: User’s saved phone number.
    • address
      (string) (Optional) Billing address street name/street no./block no etc.
      Default: User’s saved phone address.
    • city
      (string) (Optional) Billing address city.
      Default: User’s saved city.
    • country
      (string) (Optional) The country code (ISO2). ex: US
      Default: User’s saved country or store’s default country.
    • state
      (string) (Optional) The state name or state code.
      Default: User’s saved state or store’s default state.
    • zip
      (string) (Optional) Billing address postcode.
      Default: User’s saved postcode.
    • company
      (string) (Optional) User’s company name.
      Default: empty
    • vat_number
      (string) (Optional) User’s vat number.
      Default: empty
    • discount
      (string) (Optional) The coupon code to apply discount.
      Default: empty
  • 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.
    Default: IP address of the current system.
  • user_note
    (string) (Optional) Short message or note for user. User notes will be included in user invoice email sent to users.
    Default: empty
  • private_note
    (string) (Optional) Short message or note for system use. Only admin can see private notes from backend invoice page.
    Default: empty
  • parent
    (int) (Optional) For renewal payment only. Parent invoice ID for which the renewal payment is being paid.
    Default: 0
$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:

Create a invoice with simple data with status “pending payment”.

$data = array(
    'status'        => 'wpi-pending',
    'user_id'       => 1,
    'cart_details'  => array(
        array(
            'id'    => 2118
        ),
    )
);

$invoice = wpinv_insert_invoice( $data, true );

View at GitHub: https://github.com/AyeCode/wpinv-example/blob/master/invoices/create-invoice-pending-payment-simple.php

Example 2:

Create a invoice with advance data with status “pending payment”.

$data = array(
    'status'            => 'wpi-pending',
    'user_id'           => 1,
    'cart_details'      => array(
        array(
            'id'            => 2118,
            'quantity'      => 1,
            'custom_price'  => '100.00',
            'meta'          => array(
                'post_id'   => 1239
            )
        ),
    ),
    'user_info'         => array(
        'first_name'        => 'GD',
        'last_name'         => 'User',
        'phone'             => '+91 1119348834',
        'address'           => 'Kankariya Lake',
        'city'              => 'Ahmedabad',
        'country'           => 'IN',
        'state'             => 'GJ',
        'zip'               => '380002',
        'company'           => '',
        'vat_number'        => '',
        'discount'          => '10P'
    ),
    'due_date'          => '2017-08-10',
    'ip'                => '1.39.51.23',
    'user_note'         => 'This is user note.',
    'private_note'      => 'This is a system note.',
);

$invoice = wpinv_insert_invoice( $data, true );

View at GitHub: https://github.com/AyeCode/wpinv-example/blob/master/invoices/create-invoice-pending-payment-advanced

Example 3:

Create a invoice with simple data with status “paid payment”.

$data = array(
    'status'            => 'publish',
    'user_id'           => 1,
    'post_date'         => '2017-08-08 11:20:38',
    'cart_details'      => array(
        array(
            'id'        => 2118
        ),
    ),
    'payment_details'   => array(
        'gateway'           => 'paypal',
        'currency'          => 'USD',
        'transaction_id'    => '9TK485797B9101342'
    ),
);

$invoice = wpinv_insert_invoice( $data, true );

View at GitHub: https://github.com/AyeCode/wpinv-example/blob/master/invoices/create-invoice-paid-payment-simple.php

Example 4:

Create a invoice with advance data with status “paid payment”.

$data = array(
    'status'            => 'publish',
    'user_id'           => 1,
    'post_date'         => '2017-08-08 10:10:15',
    'cart_details'      => array(
        array(
            'id'            => 2118,
            'quantity'      => 1,
            'custom_price'  => '100.00',
            'meta'          => array(
                'post_id'   => 1239
            )
        ),
    ),
    'payment_details'   => array(
        'gateway'           => 'bank_transfer',
        'currency'          => 'USD',
        'transaction_id'    => 'BNK443545454557'
    ),
    'user_info'         => array(
        'first_name'        => 'GD',
        'last_name'         => 'User',
        'phone'             => '+91 1119348834',
        'address'           => 'Kankariya Lake',
        'city'              => 'Ahmedabad',
        'country'           => 'IN',
        'state'             => 'GJ',
        'zip'               => '380002',
        'company'           => '',
        'vat_number'        => '',
        'discount'          => '10P'
    ),
    'ip'                => '1.39.51.23',
    'user_note'         => 'This is user note.',
    'private_note'      => 'This is a system note.',
    'parent'            => 0
);

$invoice = wpinv_insert_invoice( $data, true );

View at GitHub: https://github.com/AyeCode/wpinv-example/blob/master/invoices/create-invoice-paid-payment-advanced.php

Was this helpful to you? Yes No