wc_add_order_item

Add a item to an order (for example a line item).

Description

(int|bool) wc_add_order_item( (int) $order_id, (array) $item_array ); 

Returns (int|bool)

Item ID or false

Parameters (2)

0. $order_id (int)
The order id.
1. $item_array (array)
The item array.

Usage

  1. if ( !function_exists( 'wc_add_order_item' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-order-item-functions.php'; 
  3.  
  4. // The order id. 
  5. $order_id = -1; 
  6.  
  7. // The item array. 
  8. $item_array = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wc_add_order_item($order_id, $item_array); 
  12.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-order-item-functions.php  
  1. function wc_add_order_item( $order_id, $item_array ) { 
  2. if ( ! $order_id = absint( $order_id ) ) { 
  3. return false; 
  4.  
  5. $defaults = array( 
  6. 'order_item_name' => '',  
  7. 'order_item_type' => 'line_item',  
  8. ); 
  9.  
  10. $item_array = wp_parse_args( $item_array, $defaults ); 
  11. $data_store = WC_Data_Store::load( 'order-item' ); 
  12. $item_id = $data_store->add_order_item( $order_id, $item_array ); 
  13. $item = WC_Order_Factory::get_order_item( $item_id ); 
  14.  
  15. do_action( 'woocommerce_new_order_item', $item_id, $item, $order_id ); 
  16.  
  17. return $item_id;