WC_API_Taxes::bulk()publicWC 2.5.0

Bulk update or insert taxes Accepts an array with taxes in the formats supported by WC_API_Taxes->create_tax() and WC_API_Taxes->edit_tax()

Method of the class: WC_API_Taxes{}

Return

Array|WP_Error.

Usage

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

Changelog

Since 2.5.0 Introduced.

WC_API_Taxes::bulk() code WC 8.6.1

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

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

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

		$taxes = array();

		foreach ( $data as $_tax ) {
			$tax_id = 0;

			// Try to get the tax rate ID
			if ( isset( $_tax['id'] ) ) {
				$tax_id = intval( $_tax['id'] );
			}

			if ( $tax_id ) {

				// Tax rate exists / edit tax rate
				$edit = $this->edit_tax( $tax_id, array( 'tax' => $_tax ) );

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

				// Tax rate don't exists / create tax rate
				$new = $this->create_tax( array( 'tax' => $_tax ) );

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

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