wc_add_order_item

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

Description

wc_add_order_item( (int) $order_id, $item ); 

Parameters (2)

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

Usage

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

Defined (1)

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

/includes/wc-order-functions.php  
  1. function wc_add_order_item( $order_id, $item ) { 
  2. global $wpdb; 
  3.  
  4. $order_id = absint( $order_id ); 
  5.  
  6. if ( ! $order_id ) 
  7. return false; 
  8.  
  9. $defaults = array( 
  10. 'order_item_name' => '',  
  11. 'order_item_type' => 'line_item',  
  12. ); 
  13.  
  14. $item = wp_parse_args( $item, $defaults ); 
  15.  
  16. $wpdb->insert( 
  17. $wpdb->prefix . "woocommerce_order_items",  
  18. array( 
  19. 'order_item_name' => $item['order_item_name'],  
  20. 'order_item_type' => $item['order_item_type'],  
  21. 'order_id' => $order_id 
  22. ),  
  23. array( 
  24. '%s', '%s', '%d' 
  25. ); 
  26.  
  27. $item_id = absint( $wpdb->insert_id ); 
  28.  
  29. do_action( 'woocommerce_new_order_item', $item_id, $item, $order_id ); 
  30.  
  31. return $item_id;