WC_API_Orders::bulk()publicWC 2.4.0

Bulk update or insert orders Accepts an array with orders in the formats supported by WC_API_Orders->create_order() and WC_API_Orders->edit_order()

Method of the class: WC_API_Orders{}

Return

Array|WP_Error.

Usage

$WC_API_Orders = new WC_API_Orders();
$WC_API_Orders->bulk( $data );
$data(array) (required)
-

Changelog

Since 2.4.0 Introduced.

WC_API_Orders::bulk() code WC 8.7.0

public function bulk( $data ) {

	try {
		if ( ! isset( $data['orders'] ) ) {
			throw new WC_API_Exception( 'woocommerce_api_missing_orders_data', sprintf( __( 'No %1$s data specified to create/edit %1$s', 'woocommerce' ), 'orders' ), 400 );
		}

		$data  = $data['orders'];
		$limit = apply_filters( 'woocommerce_api_bulk_limit', 100, 'orders' );

		// Limit bulk operation
		if ( count( $data ) > $limit ) {
			throw new WC_API_Exception( 'woocommerce_api_orders_request_entity_too_large', sprintf( __( 'Unable to accept more than %s items for this request.', 'woocommerce' ), $limit ), 413 );
		}

		$orders = array();

		foreach ( $data as $_order ) {
			$order_id = 0;

			// Try to get the order ID
			if ( isset( $_order['id'] ) ) {
				$order_id = intval( $_order['id'] );
			}

			if ( $order_id ) {

				// Order exists / edit order
				$edit = $this->edit_order( $order_id, array( 'order' => $_order ) );

				if ( is_wp_error( $edit ) ) {
					$orders[] = array(
						'id'    => $order_id,
						'error' => array( 'code' => $edit->get_error_code(), 'message' => $edit->get_error_message() ),
					);
				} else {
					$orders[] = $edit['order'];
				}
			} else {

				// Order don't exists / create order
				$new = $this->create_order( array( 'order' => $_order ) );

				if ( is_wp_error( $new ) ) {
					$orders[] = array(
						'id'    => $order_id,
						'error' => array( 'code' => $new->get_error_code(), 'message' => $new->get_error_message() ),
					);
				} else {
					$orders[] = $new['order'];
				}
			}
		}

		return array( 'orders' => apply_filters( 'woocommerce_api_orders_bulk_response', $orders, $this ) );
	} catch ( WC_Data_Exception $e ) {
		return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => 400 ) );
	} catch ( WC_API_Exception $e ) {
		return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
	}
}