wc_save_order_items( int $order_id, array $items )
Save order items. Uses the CRUD.
Description Description
Parameters Parameters
- $order_id
-
(Required) Order ID.
- $items
-
(Required) Order items to save.
Source Source
File: includes/admin/wc-admin-functions.php
function wc_save_order_items( $order_id, $items ) { // Allow other plugins to check change in order items before they are saved. do_action( 'woocommerce_before_save_order_items', $order_id, $items ); $qty_change_order_notes = array(); // Line items and fees. if ( isset( $items['order_item_id'] ) ) { $data_keys = array( 'line_tax' => array(), 'line_subtotal_tax' => array(), 'order_item_name' => null, 'order_item_qty' => null, 'order_item_tax_class' => null, 'line_total' => null, 'line_subtotal' => null, ); foreach ( $items['order_item_id'] as $item_id ) { $item = WC_Order_Factory::get_order_item( absint( $item_id ) ); if ( ! $item ) { continue; } $item_data = array(); foreach ( $data_keys as $key => $default ) { $item_data[ $key ] = isset( $items[ $key ][ $item_id ] ) ? wc_check_invalid_utf8( wp_unslash( $items[ $key ][ $item_id ] ) ) : $default; } if ( '0' === $item_data['order_item_qty'] ) { $changed_stock = wc_maybe_adjust_line_item_product_stock( $item, 0 ); if ( $changed_stock && ! is_wp_error( $changed_stock ) ) { $qty_change_order_notes[] = $item->get_name() . ' – ' . $changed_stock['from'] . '→' . $changed_stock['to']; } $item->delete(); continue; } $item->set_props( array( 'name' => $item_data['order_item_name'], 'quantity' => $item_data['order_item_qty'], 'tax_class' => $item_data['order_item_tax_class'], 'total' => $item_data['line_total'], 'subtotal' => $item_data['line_subtotal'], 'taxes' => array( 'total' => $item_data['line_tax'], 'subtotal' => $item_data['line_subtotal_tax'], ), ) ); if ( 'fee' === $item->get_type() ) { $item->set_amount( $item_data['line_total'] ); } if ( isset( $items['meta_key'][ $item_id ], $items['meta_value'][ $item_id ] ) ) { foreach ( $items['meta_key'][ $item_id ] as $meta_id => $meta_key ) { $meta_key = substr( wp_unslash( $meta_key ), 0, 255 ); $meta_value = isset( $items['meta_value'][ $item_id ][ $meta_id ] ) ? wp_unslash( $items['meta_value'][ $item_id ][ $meta_id ] ) : ''; if ( '' === $meta_key && '' === $meta_value ) { if ( ! strstr( $meta_id, 'new-' ) ) { $item->delete_meta_data_by_mid( $meta_id ); } } elseif ( strstr( $meta_id, 'new-' ) ) { $item->add_meta_data( $meta_key, $meta_value, false ); } else { $item->update_meta_data( $meta_key, $meta_value, $meta_id ); } } } // Allow other plugins to change item object before it is saved. do_action( 'woocommerce_before_save_order_item', $item ); $item->save(); $changed_stock = wc_maybe_adjust_line_item_product_stock( $item ); if ( $changed_stock && ! is_wp_error( $changed_stock ) ) { $qty_change_order_notes[] = $item->get_name() . ' (' . $changed_stock['from'] . '→' . $changed_stock['to'] . ')'; } } } // Shipping Rows. if ( isset( $items['shipping_method_id'] ) ) { $data_keys = array( 'shipping_method' => null, 'shipping_method_title' => null, 'shipping_cost' => 0, 'shipping_taxes' => array(), ); foreach ( $items['shipping_method_id'] as $item_id ) { $item = WC_Order_Factory::get_order_item( absint( $item_id ) ); if ( ! $item ) { continue; } $item_data = array(); foreach ( $data_keys as $key => $default ) { $item_data[ $key ] = isset( $items[ $key ][ $item_id ] ) ? wc_clean( wp_unslash( $items[ $key ][ $item_id ] ) ) : $default; } $item->set_props( array( 'method_id' => $item_data['shipping_method'], 'method_title' => $item_data['shipping_method_title'], 'total' => $item_data['shipping_cost'], 'taxes' => array( 'total' => $item_data['shipping_taxes'], ), ) ); if ( isset( $items['meta_key'][ $item_id ], $items['meta_value'][ $item_id ] ) ) { foreach ( $items['meta_key'][ $item_id ] as $meta_id => $meta_key ) { $meta_value = isset( $items['meta_value'][ $item_id ][ $meta_id ] ) ? wp_unslash( $items['meta_value'][ $item_id ][ $meta_id ] ) : ''; if ( '' === $meta_key && '' === $meta_value ) { if ( ! strstr( $meta_id, 'new-' ) ) { $item->delete_meta_data_by_mid( $meta_id ); } } elseif ( strstr( $meta_id, 'new-' ) ) { $item->add_meta_data( $meta_key, $meta_value, false ); } else { $item->update_meta_data( $meta_key, $meta_value, $meta_id ); } } } $item->save(); } } $order = wc_get_order( $order_id ); if ( ! empty( $qty_change_order_notes ) ) { /* translators: %s item name. */ $order->add_order_note( sprintf( __( 'Adjusted stock: %s', 'woocommerce' ), implode( ', ', $qty_change_order_notes ) ), false, true ); } $order->update_taxes(); $order->calculate_totals( false ); // Inform other plugins that the items have been saved. do_action( 'woocommerce_saved_order_items', $order_id, $items ); }
Changelog Changelog
Version | Description |
---|---|
2.2 | Introduced. |