1. Home
  2. Developers
  3. wpinv_create_item()

wpinv_create_item()

Introduction | Parameters | Examples

Introduction

Create a new item.

wpinv_create_item( array $args = array(), bool $wp_error = false, bool $force_update = false )

If the $args parameter has ‘custom_id’ set to a value and $force_update is true, then item will be updated for matching ‘item_id’ and ‘type’.

Parameters

$args

(array) (Required) An array of elements to create a new item.

  • type
    (string) (Optional) The item type.
    Default: ‘custom’
  • title
    (string) (Required) The item title.
  • custom_id
    (string) (Optional) Any integer or non numeric id. Must be unique within item type.
    Default: item ID will be set after save.
  • price
    (float) (Optional) The item price.
    Default: ‘0.00’
  • status
    (string) (Optional) The item status. ‘publish’ => Published item, ‘pending’ => Pending item.
    Default: ‘pending’
  • custom_name
    (string) (Optional) Plural sub title for the item.
    Default: empty
  • custom_singular_name
    (string) (Optional) Singular sub title for the item.
    Default: empty
  • vat_rule
    (string) (Optional) VAT rule to apply if tax enabled. ‘digital’ => Digital item, ‘physical’ => Physical item.
    Default: ‘digital’
  • editable
    (bool) (Optional) Item editable from backend items page?
    Default: true
  • excerpt
    (string) (Optional) The item excerpt(short description).
    Default: empty
  • is_recurring
    (bool) (Optional) Whether to enable recurring feature for the item? Use 1 => Allow recurring or 0 => Don’t allow recurring.
    Default: 0
  • recurring_period
    (string) (Optional) Recurring time period. ‘D’ => Daily, ‘W’ => Weekly, ‘M’ => Monthly, ‘Y’ => Yearly.
    Default: ‘M’
  • recurring_interval
    (int) (Optional) Recurring interval. Integer number between 1 – 90.
    Default: 1
  • recurring_limit
    (int) (Optional) Recurring limit. 0 for recurring forever until cancelled.
    Default: 0
  • free_trial
    (bool) (Optional) Whether to enable free trial if recurring feature is enabled?
    Default: 0
  • trial_period
    (int) (Optional) Free trial time period. ‘D’ => Daily, ‘W’ => Weekly, ‘M’ => Monthly, ‘Y’ => Yearly.
    Default: ‘M’
  • trial_interval
    (int) (Optional) Free trial interval. Integer number between 1 – 90.
    Default: 1
$wp_error

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

Default: false

$force_update

(bool) (Optional) Whether to update an item if parameter ‘custom_id’ has set value and item already exists with that custom_id.

Default: false

Return

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

Examples

Example 1:

Create an item with simple data.

$data = array(
    'title'    => __( 'Simple Item', 'textdomain' ),
    'price'    => '199.00',
    'status'   => 'publish'
 );
 
$item = wpinv_create_item( $data, true );

View at GitHub: https://github.com/AyeCode/wpinv-example/blob/master/items/create-item-simple.php

Example 2:

Create an item with advance data.

$data = array(
    'type'                 => 'custom',
    'title'                => __( 'Advance Item', 'textdomain' ),
    'custom_id'            => 123456,
    'price'                => '99.00',
    'status'               => 'publish',
    'custom_name'          => 'Custom name',
    'custom_singular_name' => 'For Sale',
    'vat_rule'             => 'digital',
    'editable'             => true,
    'excerpt'              => __( 'This is my first item.', 'textdomain' ),
 );
 
$item = wpinv_create_item( $data, true );

View at GitHub: https://github.com/AyeCode/wpinv-example/blob/master/items/create-item-advanced.php

Example 3:

Create an recurring item with simple data.

$data = array(
    'title'                 => __( 'Simple Recurring Item', 'textdomain' ),
    'price'                 => '199.00',
    'status'                => 'publish',
    'is_recurring'          => 1,
    'recurring_period'      => 'M',
    'recurring_interval'    => 1,
    'recurring_limit'       => 6,
);
 
$item = wpinv_create_item( $data, true );

View at GitHub: https://github.com/AyeCode/wpinv-example/blob/master/items/create-recurring-item-simple.php

Example 4:

Create an recurring item with advance data.

$data = array(
    'type'                 => 'custom',
    'title'                => __( 'Advance Recurring Item', 'textdomain' ),
    'custom_id'            => 123457,
    'price'                => '99.00',
    'status'               => 'publish',
    'custom_name'          => 'Custom name',
    'custom_singular_name' => 'For Sale',
    'vat_rule'             => 'digital',
    'editable'             => true,
    'excerpt'              => __( 'This is my first item.', 'textdomain' ),
    'is_recurring'         => 1,
    'recurring_period'     => 'M',
    'recurring_interval'   => 1,
    'recurring_limit'      => 6,
    'free_trial'           => 1,
    'trial_period'         => 'M',
    'trial_interval'       => 1,
);
 
$item = wpinv_create_item( $data, true );

View at GitHub: https://github.com/AyeCode/wpinv-example/blob/master/items/create-recurring-item-advanced.php

Was this helpful to you? Yes No