1. Home
  2. Developers
  3. Adding a Custom Item Type

Adding a Custom Item Type

Getting started

In this post describes example of how to create a custom item with custom item type and accept payments.
You can download our sample plugin here: wpinv-custom-item.zip

Register a item type

Let’s start by registering a custom item type “product” with the title “Product”.

/**
 * Register the custom item type.
 */
function wpi_custom_register_item_type( $item_types ) {
    $item_types['product'] = __( 'Product', 'textdomain' );
        
    return $item_types;
}
add_filter( 'wpinv_get_item_types', 'wpi_custom_register_item_type', 10, 1 );

Adding a some description about “Product” item type which will be displayed under Item Info metabox in add/edit item form in backend.

function wpi_custom_product_type_info( $post ) {
    ?>
    

 

Handle Item Payment Received

Here is example that explains how to handle payment received for "product" item.

function wpi_custom_on_payment_complete( $invoice_id, $new_status, $old_status ) {
    if ( $new_status == 'publish' || $new_status == 'wpi-renewal' ) {
        $invoice = wpinv_get_invoice( $invoice_id );
        
        if ( !empty( $invoice ) ) {
            $cart_items = $invoice->get_cart_details();
            
            if ( !empty( $cart_items ) ) {
                foreach ( $cart_items as $key => $cart_item ) {
                    $item = !empty( $cart_item['id'] ) ? new WPInv_Item( $cart_item['id'] ) : NULL;
                    
                    if ( !empty( $item ) && $item->get_type() == 'product' ) {
                        $item_id = $item->ID;
                        // Handle payment accepted for item.
                    }
                }
            }
        }
    }
}
add_action( 'wpinv_update_status', 'wpi_custom_on_payment_complete', 10, 3 );

Handle Item for Refunded Payment

Here is example that explains how to handle refunded for "product" item.

function wpi_custom_product_on_refund_payment( $invoice ) {
    $cart_items = !empty( $invoice ) ? $invoice->get_cart_details() : NULL;
    
    if ( !empty( $cart_items ) ) {
        foreach ( $cart_items as $key => $cart_item ) {
            $item = !empty( $cart_item['id'] ) ? new WPInv_Item( $cart_item['id'] ) : NULL;
        
            if ( !empty( $item ) && $item->get_type() == 'product' ) {
                $item_id = $item->ID;
                // Handle item refund .
            }
        }
    }
}
add_action( 'wpinv_post_refund_invoice', 'wpi_custom_product_on_refund_payment', 10, 1 );

Custom Actions

When deleting invoicing item this will decide whether to allow or not to delete item with item type "product".

/**
 * Allow "product" item type delete.
 */
function wpi_custom_can_delete_product_item( $return, $post_id ) {
    if ( get_post_meta( $post_id, '_wpinv_type', true ) == 'product' && $product_id = get_post_meta( $post_id, '_wpinv_product_id', true ) ) {
        // $return = true; // Allow to delete "product" item.
        // $return = false; // Don't allow to delete "product" item.
    }

    return $return;
}
add_filter( 'wpinv_can_delete_item', 'wpi_custom_can_delete_product_item', 10, 2 );

Add a item via API data

 //=== Example 1: Create an simple standard item. ===//
 
 // An array of elements that make up a item to update or insert.
 $itemarr = array(
     'item_id'            => 1946,                                                    // Required. Any integer number. Must be unique within item type.
     'title'              => __( 'Simple Standard Product', 'wpinv-custom' ),         // Required. Item title.
     'price'              => '49.50',                                                 // Optional. Item price. Default '0.00'.
     'status'             => 'publish',                                               // Optional. pending, publish
 );
 $item = wpinv_create_item( $itemarr ); // Returns invoicing item object on success. The value 0 or WP_Error on failure.
    
 //=== Example 2: Create an item with advance data. ===//
 
 // An array of elements that make up a item to update or insert.
 $itemarr = array(
     'type'               => 'product',                                               // Optional. Item type. Default 'custom'.
     'item_id'            => 1947,                                                    // Required. Any integer number. Must be unique within item type.
     'title'              => __( 'First Product', 'wpinv-custom' ),                   // Required. Item title.
     'price'              => '99.50',                                                 // Optional. Item price. Default '0.00'.
     'status'             => 'publish',                                               // Optional. pending, publish
     'vat_rule'           => 'digital',                                               // Optional. digital => Digital item, physical => Physical item
     'cpt_singular_name'  => 'For Sale',                                              // Optional. Sub title for item. Should be singular.
     'cpt_name'           => '',                                                      // Optional. Sub title for item. Should be plural.
     'is_recurring'       => 1,                                                       // Optional. 1 => Allow recurring or 0 => Don't allow recurring
     'recurring_period'   => 'M',                                                     // Optional. D => Daily, W => Weekly, M => Monthly, Y => Yearly
     'recurring_interval' => 1,                                                       // Optional. Integer value between 1 - 90.
     'recurring_limit'    => 6,                                                       // Optional. Any integer number. 0 for recurring forever until cancelled.
     'free_trial'         => 1,                                                       // Optional. 1 => Allow free trial or 0 => Don't free trial
     'trial_period'       => 'M',                                                     // Optional. D => Daily, W => Weekly, M => Monthly, Y => Yearly
     'trial_interval'     => 1,                                                       // Optional. Any integer number.
     'excerpt'            => __( 'This is my first product.', 'wpinv-custom' ),       // Optional. Item short description
 );
 $item = wpinv_create_item( $itemarr ); // Returns invoicing item object on success. The value 0 or WP_Error on failure.
Was this helpful to you? Yes 5 No 5