WordPress at Your Fingertips
function is not described

WC_REST_Controller::batch_items() public WC 1.0

Bulk create, update and delete items.

{} It's a method of the class: WC_REST_Controller{}

No Hooks.

Return

Array. Of WP_Error or WP_REST_Response.

Usage

$WC_REST_Controller = new WC_REST_Controller();
$WC_REST_Controller->batch_items( $request );
$request(WP_REST_Request) (required)
Full details about the request.

Code of WC_REST_Controller::batch_items() WC 5.9.0

public function batch_items( $request ) {
	/**
	 * REST Server
	 *
	 * @var WP_REST_Server $wp_rest_server
	 */
	global $wp_rest_server;

	// Get the request params.
	$items    = array_filter( $request->get_params() );
	$query    = $request->get_query_params();
	$response = array();

	// Check batch limit.
	$limit = $this->check_batch_limit( $items );
	if ( is_wp_error( $limit ) ) {
		return $limit;
	}

	if ( ! empty( $items['create'] ) ) {
		foreach ( $items['create'] as $item ) {
			$_item = new WP_REST_Request( 'POST' );

			// Default parameters.
			$defaults = array();
			$schema   = $this->get_public_item_schema();
			foreach ( $schema['properties'] as $arg => $options ) {
				if ( isset( $options['default'] ) ) {
					$defaults[ $arg ] = $options['default'];
				}
			}
			$_item->set_default_params( $defaults );

			// Set request parameters.
			$_item->set_body_params( $item );

			// Set query (GET) parameters.
			$_item->set_query_params( $query );

			$_response = $this->create_item( $_item );

			if ( is_wp_error( $_response ) ) {
				$response['create'][] = array(
					'id'    => 0,
					'error' => array(
						'code'    => $_response->get_error_code(),
						'message' => $_response->get_error_message(),
						'data'    => $_response->get_error_data(),
					),
				);
			} else {
				$response['create'][] = $wp_rest_server->response_to_data( $_response, '' );
			}
		}
	}

	if ( ! empty( $items['update'] ) ) {
		foreach ( $items['update'] as $item ) {
			$_item = new WP_REST_Request( 'PUT' );
			$_item->set_body_params( $item );
			$_response = $this->update_item( $_item );

			if ( is_wp_error( $_response ) ) {
				$response['update'][] = array(
					'id'    => $item['id'],
					'error' => array(
						'code'    => $_response->get_error_code(),
						'message' => $_response->get_error_message(),
						'data'    => $_response->get_error_data(),
					),
				);
			} else {
				$response['update'][] = $wp_rest_server->response_to_data( $_response, '' );
			}
		}
	}

	if ( ! empty( $items['delete'] ) ) {
		foreach ( $items['delete'] as $id ) {
			$id = (int) $id;

			if ( 0 === $id ) {
				continue;
			}

			$_item = new WP_REST_Request( 'DELETE' );
			$_item->set_query_params(
				array(
					'id'    => $id,
					'force' => true,
				)
			);
			$_response = $this->delete_item( $_item );

			if ( is_wp_error( $_response ) ) {
				$response['delete'][] = array(
					'id'    => $id,
					'error' => array(
						'code'    => $_response->get_error_code(),
						'message' => $_response->get_error_message(),
						'data'    => $_response->get_error_data(),
					),
				);
			} else {
				$response['delete'][] = $wp_rest_server->response_to_data( $_response, '' );
			}
		}
	}

	return $response;
}