wc_save_order_items

Save order items.

Description

wc_save_order_items( (int) $order_id, (array) $items ); 

Uses the CRUD.

Parameters (2)

0. $order_id (int)
The order id.
1. $items (array)
Order items to save

Usage

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

Defined (1)

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

/includes/admin/wc-admin-functions.php  
  1. function wc_save_order_items( $order_id, $items ) { 
  2. // Allow other plugins to check change in order items before they are saved 
  3. do_action( 'woocommerce_before_save_order_items', $order_id, $items ); 
  4.  
  5. $order = wc_get_order( $order_id ); 
  6.  
  7. // Line items and fees 
  8. if ( isset( $items['order_item_id'] ) ) { 
  9. $data_keys = array( 
  10. 'line_tax' => array(),  
  11. 'line_subtotal_tax' => array(),  
  12. 'order_item_name' => null,  
  13. 'order_item_qty' => null,  
  14. 'order_item_tax_class' => null,  
  15. 'line_total' => null,  
  16. 'line_subtotal' => null,  
  17. ); 
  18. foreach ( $items['order_item_id'] as $item_id ) { 
  19. if ( ! $item = $order->get_item( absint( $item_id ) ) ) { 
  20. continue; 
  21.  
  22. $item_data = array(); 
  23.  
  24. foreach ( $data_keys as $key => $default ) { 
  25. $item_data[ $key ] = isset( $items[ $key ][ $item_id ] ) ? wc_clean( wp_unslash( $items[ $key ][ $item_id ] ) ) : $default; 
  26.  
  27. if ( '0' === $item_data['order_item_qty'] ) { 
  28. $item->delete(); 
  29. continue; 
  30.  
  31. $item->set_props( array( 
  32. 'name' => $item_data['order_item_name'],  
  33. 'quantity' => $item_data['order_item_qty'],  
  34. 'tax_class' => $item_data['order_item_tax_class'],  
  35. 'total' => $item_data['line_total'],  
  36. 'subtotal' => $item_data['line_subtotal'],  
  37. 'taxes' => array( 
  38. 'total' => $item_data['line_tax'],  
  39. 'subtotal' => $item_data['line_subtotal_tax'],  
  40. ),  
  41. ) ); 
  42.  
  43. if ( isset( $items['meta_key'][ $item_id ], $items['meta_value'][ $item_id ] ) ) { 
  44. foreach ( $items['meta_key'][ $item_id ] as $meta_id => $meta_key ) { 
  45. $meta_value = isset( $items['meta_value'][ $item_id ][ $meta_id ] ) ? wp_unslash( $items['meta_value'][ $item_id ][ $meta_id ] ) : ''; 
  46.  
  47. if ( '' === $meta_key && '' === $meta_value ) { 
  48. if ( ! strstr( $meta_id, 'new-' ) ) { 
  49. $item->delete_meta_data_by_mid( $meta_id ); 
  50. } elseif ( strstr( $meta_id, 'new-' ) ) { 
  51. $item->add_meta_data( $meta_key, $meta_value, false ); 
  52. } else { 
  53. $item->update_meta_data( $meta_key, $meta_value, $meta_id ); 
  54.  
  55. $item->save(); 
  56.  
  57. // Shipping Rows 
  58. if ( isset( $items['shipping_method_id'] ) ) { 
  59. $data_keys = array( 
  60. 'shipping_method' => null,  
  61. 'shipping_method_title' => null,  
  62. 'shipping_cost' => 0,  
  63. 'shipping_taxes' => array(),  
  64. ); 
  65.  
  66. foreach ( $items['shipping_method_id'] as $item_id ) { 
  67. if ( ! $item = $order->get_item( absint( $item_id ) ) ) { 
  68. continue; 
  69.  
  70. $item_data = array(); 
  71.  
  72. foreach ( $data_keys as $key => $default ) { 
  73. $item_data[ $key ] = isset( $items[ $key ][ $item_id ] ) ? wc_clean( wp_unslash( $items[ $key ][ $item_id ] ) ) : $default; 
  74.  
  75. $item->set_props( array( 
  76. 'method_id' => $item_data['shipping_method'],  
  77. 'method_title' => $item_data['shipping_method_title'],  
  78. 'total' => $item_data['shipping_cost'],  
  79. 'taxes' => array( 
  80. 'total' => $item_data['shipping_taxes'],  
  81. ),  
  82. ) ); 
  83.  
  84. if ( isset( $items['meta_key'][ $item_id ], $items['meta_value'][ $item_id ] ) ) { 
  85. foreach ( $items['meta_key'][ $item_id ] as $meta_id => $meta_key ) { 
  86. $meta_value = isset( $items['meta_value'][ $item_id ][ $meta_id ] ) ? wp_unslash( $items['meta_value'][ $item_id ][ $meta_id ] ) : ''; 
  87.  
  88. if ( '' === $meta_key && '' === $meta_value ) { 
  89. if ( ! strstr( $meta_id, 'new-' ) ) { 
  90. $item->delete_meta_data_by_mid( $meta_id ); 
  91. } elseif ( strstr( $meta_id, 'new-' ) ) { 
  92. $item->add_meta_data( $meta_key, $meta_value, false ); 
  93. } else { 
  94. $item->update_meta_data( $meta_key, $meta_value, $meta_id ); 
  95.  
  96. $item->save(); 
  97.  
  98. // Updates tax totals 
  99. $order->update_taxes(); 
  100.  
  101. // Calc totals - this also triggers save 
  102. $order->calculate_totals( false ); 
  103.  
  104. // Inform other plugins that the items have been saved 
  105. do_action( 'woocommerce_saved_order_items', $order_id, $items );