WC_Tax_Rate_Importer::import()publicWC 1.0

Import the file if it exists and is valid.

Method of the class: WC_Tax_Rate_Importer{}

No Hooks.

Return

null. Nothing (null).

Usage

$WC_Tax_Rate_Importer = new WC_Tax_Rate_Importer();
$WC_Tax_Rate_Importer->import( $file );
$file(mixed) (required)
File.

WC_Tax_Rate_Importer::import() code WC 8.7.0

public function import( $file ) {
	if ( ! is_file( $file ) ) {
		$this->import_error( __( 'The file does not exist, please try again.', 'woocommerce' ) );
	}

	$this->import_start();

	$loop   = 0;
	$handle = fopen( $file, 'r' );

	if ( false !== $handle ) {

		$header = fgetcsv( $handle, 0, $this->delimiter );
		$count  = is_countable( $header ) ? count( $header ) : 0;
		if ( 10 === $count ) {

			$row = fgetcsv( $handle, 0, $this->delimiter );

			while ( false !== $row ) {

				list( $country, $state, $postcode, $city, $rate, $name, $priority, $compound, $shipping, $class ) = $row;

				$tax_rate = array(
					'tax_rate_country'  => $country,
					'tax_rate_state'    => $state,
					'tax_rate'          => $rate,
					'tax_rate_name'     => $name,
					'tax_rate_priority' => $priority,
					'tax_rate_compound' => $compound ? 1 : 0,
					'tax_rate_shipping' => $shipping ? 1 : 0,
					'tax_rate_order'    => $loop ++,
					'tax_rate_class'    => $class,
				);

				$tax_rate_id = WC_Tax::_insert_tax_rate( $tax_rate );
				WC_Tax::_update_tax_rate_postcodes( $tax_rate_id, wc_clean( $postcode ) );
				WC_Tax::_update_tax_rate_cities( $tax_rate_id, wc_clean( $city ) );

				$row = fgetcsv( $handle, 0, $this->delimiter );
			}
		} else {
			$this->import_error( __( 'The CSV is invalid.', 'woocommerce' ) );
		}

		fclose( $handle );
	}

	// Show Result.
	echo '<div class="updated settings-error"><p>';
	printf(
		/* translators: %s: tax rates count */
		esc_html__( 'Import complete - imported %s tax rates.', 'woocommerce' ),
		'<strong>' . absint( $loop ) . '</strong>'
	);
	echo '</p></div>';

	$this->import_end();
}