{"id":77,"date":"2017-06-26T13:34:10","date_gmt":"2017-06-26T13:34:10","guid":{"rendered":"https:\/\/wpinvoicing.com\/docs\/?p=77"},"modified":"2019-11-16T10:26:42","modified_gmt":"2019-11-16T10:26:42","slug":"adding-a-custom-item-type","status":"publish","type":"docs","link":"https:\/\/wpgetpaid.com\/docs\/developers\/adding-a-custom-item-type\/","title":{"rendered":"Adding a Custom Item Type"},"content":{"rendered":"<h3>Getting started<\/h3>\n<p>In this post describes example of how to create a custom item with custom item type and accept payments.<br \/>\nYou can download our sample plugin here: <a href=\"https:\/\/wpinvoicing.com\/docs\/wp-content\/uploads\/sites\/19\/2017\/06\/wpinv-custom-item.zip\">wpinv-custom-item.zip<\/a><\/p>\n<h3>Register a item type<\/h3>\n<p>Let&#8217;s start by registering a custom item type &#8220;product&#8221; with the title &#8220;Product&#8221;.<\/p>\n<pre>\/**\r\n * Register the custom item type.\r\n *\/\r\nfunction wpi_custom_register_item_type( $item_types ) {\r\n    $item_types['product'] = __( 'Product', 'textdomain' );\r\n        \r\n    return $item_types;\r\n}\r\nadd_filter( 'wpinv_get_item_types', 'wpi_custom_register_item_type', 10, 1 );\r\n<\/pre>\n<p><a href=\"https:\/\/wpinvoicing.com\/docs\/wp-content\/uploads\/sites\/19\/2017\/06\/item-info-meta-box.png\"><img class=\"alignnone size-full wp-image-79\" src=\"https:\/\/wpinvoicing.com\/docs\/wp-content\/uploads\/sites\/19\/2017\/06\/item-info-meta-box.png\" alt=\"\" width=\"280\" height=\"224\" \/><\/a><\/p>\n<p>Adding a some description about &#8220;Product&#8221; item type which will be displayed under Item Info metabox in add\/edit item form in backend.<\/p>\n<pre>function wpi_custom_product_type_info( $post ) {\r\n    ?>\r\n    <p class=\"wpi-m0\"><?php _e( 'Product: Info about product item type.', 'wpinv-custom' );?><\/p>\r\n    <?php\r\n}\r\nadd_action( 'wpinv_item_info_metabox_after', 'wpi_custom_product_type_info', 11, 1 ) ;\r\n<\/pre>\n<p class=\"wpi-m0\">\n<p>&nbsp;<\/p>\n<h3>Handle Item Payment Received<\/h3>\n<p>Here is example that explains how to handle payment received for \"product\" item.<\/p>\n<pre class=\"\">function wpi_custom_on_payment_complete( $invoice_id, $new_status, $old_status ) {\r\n    if ( $new_status == 'publish' || $new_status == 'wpi-renewal' ) {\r\n        $invoice = wpinv_get_invoice( $invoice_id );\r\n        \r\n        if ( !empty( $invoice ) ) {\r\n            $cart_items = $invoice->get_cart_details();\r\n            \r\n            if ( !empty( $cart_items ) ) {\r\n                foreach ( $cart_items as $key => $cart_item ) {\r\n                    $item = !empty( $cart_item['id'] ) ? new WPInv_Item( $cart_item['id'] ) : NULL;\r\n                    \r\n                    if ( !empty( $item ) && $item->get_type() == 'product' ) {\r\n                        $item_id = $item->ID;\r\n                        \/\/ Handle payment accepted for item.\r\n                    }\r\n                }\r\n            }\r\n        }\r\n    }\r\n}\r\nadd_action( 'wpinv_update_status', 'wpi_custom_on_payment_complete', 10, 3 );\r\n<\/pre>\n<h3>Handle Item for Refunded Payment<\/h3>\n<p>Here is example that explains how to handle refunded for \"product\" item.<\/p>\n<pre>function wpi_custom_product_on_refund_payment( $invoice ) {\r\n    $cart_items = !empty( $invoice ) ? $invoice->get_cart_details() : NULL;\r\n    \r\n    if ( !empty( $cart_items ) ) {\r\n        foreach ( $cart_items as $key => $cart_item ) {\r\n            $item = !empty( $cart_item['id'] ) ? new WPInv_Item( $cart_item['id'] ) : NULL;\r\n        \r\n            if ( !empty( $item ) && $item->get_type() == 'product' ) {\r\n                $item_id = $item->ID;\r\n                \/\/ Handle item refund .\r\n            }\r\n        }\r\n    }\r\n}\r\nadd_action( 'wpinv_post_refund_invoice', 'wpi_custom_product_on_refund_payment', 10, 1 );<\/pre>\n<h3>Custom Actions<\/h3>\n<p>When deleting invoicing item this will decide whether to allow or not to delete item with item type \"product\".<\/p>\n<pre>\/**\r\n * Allow \"product\" item type delete.\r\n *\/\r\nfunction wpi_custom_can_delete_product_item( $return, $post_id ) {\r\n    if ( get_post_meta( $post_id, '_wpinv_type', true ) == 'product' && $product_id = get_post_meta( $post_id, '_wpinv_product_id', true ) ) {\r\n        \/\/ $return = true; \/\/ Allow to delete \"product\" item.\r\n        \/\/ $return = false; \/\/ Don't allow to delete \"product\" item.\r\n    }\r\n\r\n    return $return;\r\n}\r\nadd_filter( 'wpinv_can_delete_item', 'wpi_custom_can_delete_product_item', 10, 2 );\r\n<\/pre>\n<h3>Add a item via API data<\/h3>\n<pre> \/\/=== Example 1: Create an simple standard item. ===\/\/\r\n \r\n \/\/ An array of elements that make up a item to update or insert.\r\n $itemarr = array(\r\n     'item_id'            => 1946,                                                    \/\/ Required. Any integer number. Must be unique within item type.\r\n     'title'              => __( 'Simple Standard Product', 'wpinv-custom' ),         \/\/ Required. Item title.\r\n     'price'              => '49.50',                                                 \/\/ Optional. Item price. Default '0.00'.\r\n     'status'             => 'publish',                                               \/\/ Optional. pending, publish\r\n );\r\n $item = wpinv_create_item( $itemarr ); \/\/ Returns invoicing item object on success. The value 0 or WP_Error on failure.\r\n    \r\n \/\/=== Example 2: Create an item with advance data. ===\/\/\r\n \r\n \/\/ An array of elements that make up a item to update or insert.\r\n $itemarr = array(\r\n     'type'               => 'product',                                               \/\/ Optional. Item type. Default 'custom'.\r\n     'item_id'            => 1947,                                                    \/\/ Required. Any integer number. Must be unique within item type.\r\n     'title'              => __( 'First Product', 'wpinv-custom' ),                   \/\/ Required. Item title.\r\n     'price'              => '99.50',                                                 \/\/ Optional. Item price. Default '0.00'.\r\n     'status'             => 'publish',                                               \/\/ Optional. pending, publish\r\n     'vat_rule'           => 'digital',                                               \/\/ Optional. digital => Digital item, physical => Physical item\r\n     'cpt_singular_name'  => 'For Sale',                                              \/\/ Optional. Sub title for item. Should be singular.\r\n     'cpt_name'           => '',                                                      \/\/ Optional. Sub title for item. Should be plural.\r\n     'is_recurring'       => 1,                                                       \/\/ Optional. 1 => Allow recurring or 0 => Don't allow recurring\r\n     'recurring_period'   => 'M',                                                     \/\/ Optional. D => Daily, W => Weekly, M => Monthly, Y => Yearly\r\n     'recurring_interval' => 1,                                                       \/\/ Optional. Integer value between 1 - 90.\r\n     'recurring_limit'    => 6,                                                       \/\/ Optional. Any integer number. 0 for recurring forever until cancelled.\r\n     'free_trial'         => 1,                                                       \/\/ Optional. 1 => Allow free trial or 0 => Don't free trial\r\n     'trial_period'       => 'M',                                                     \/\/ Optional. D => Daily, W => Weekly, M => Monthly, Y => Yearly\r\n     'trial_interval'     => 1,                                                       \/\/ Optional. Any integer number.\r\n     'excerpt'            => __( 'This is my first product.', 'wpinv-custom' ),       \/\/ Optional. Item short description\r\n );\r\n $item = wpinv_create_item( $itemarr ); \/\/ Returns invoicing item object on success. The value 0 or WP_Error on failure.<\/pre>\n","protected":false},"featured_media":0,"parent":241,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","doc_tag":[],"amp_validity":null,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/wpgetpaid.com\/docs\/wp-json\/wp\/v2\/docs\/77"}],"collection":[{"href":"https:\/\/wpgetpaid.com\/docs\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/wpgetpaid.com\/docs\/wp-json\/wp\/v2\/types\/docs"}],"replies":[{"embeddable":true,"href":"https:\/\/wpgetpaid.com\/docs\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":0,"href":"https:\/\/wpgetpaid.com\/docs\/wp-json\/wp\/v2\/docs\/77\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/wpgetpaid.com\/docs\/wp-json\/wp\/v2\/docs\/241"}],"wp:attachment":[{"href":"https:\/\/wpgetpaid.com\/docs\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/wpgetpaid.com\/docs\/wp-json\/wp\/v2\/doc_tag?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}