WC_AJAX::add_order_item
Add order item via ajax. Used on the edit order screen in WP Admin.
Method of the class: WC_AJAX{}
No Hooks.
Returns
null. Nothing (null).
Usage
$result = WC_AJAX::add_order_item();
WC_AJAX::add_order_item() WC AJAX::add order item code WC 10.5.0
public static function add_order_item() {
check_ajax_referer( 'order-item', 'security' );
if ( ! current_user_can( 'edit_shop_orders' ) ) {
wp_die( -1 );
}
if ( ! isset( $_POST['order_id'] ) ) {
throw new Exception( __( 'Invalid order', 'woocommerce' ) );
}
$order_id = absint( wp_unslash( $_POST['order_id'] ) );
// If we passed through items it means we need to save first before adding a new one.
$items = ( ! empty( $_POST['items'] ) ) ? wp_unslash( $_POST['items'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$items_to_add = isset( $_POST['data'] ) ? array_filter( wp_unslash( (array) $_POST['data'] ) ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
try {
$response = self::maybe_add_order_item( $order_id, $items, $items_to_add );
wp_send_json_success( $response );
} catch ( Exception $e ) {
wp_send_json_error( array( 'error' => $e->getMessage() ) );
}
}