WC_AJAX::add_order_item │ public static │ WC 1.0

Add order item via ajax. Used on the edit order screen in WP Admin.

Метод класса: WC_AJAX{}

Хуков нет.

Возвращает

void. Ничего (null).

Использование

$result = WC_AJAX::add_order_item();

Код WC_AJAX::add_order_item() WC 11.3.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
	$tax_location = array();
	foreach ( array( 'country', 'state', 'postcode', 'city' ) as $key ) {
		$tax_location[ $key ] = isset( $_POST[ $key ] ) && is_string( $_POST[ $key ] ) ? wc_strtoupper( sanitize_text_field( wp_unslash( $_POST[ $key ] ) ) ) : '';
	}

	try {
		$response = self::maybe_add_order_item( $order_id, $items, $items_to_add, $tax_location );
	} catch ( Exception $e ) {
		wp_send_json_error( array( 'error' => $e->getMessage() ) );
	}
	wp_send_json_success( $response );
}