Automattic\WooCommerce\Admin\API

OnboardingTasks::import_sample_products_from_csv │ public static │ WC 1.0

Import sample products from given CSV path.

Method of the class: OnboardingTasks{}

Returns

\WP_Error|array.

Usage

$result = OnboardingTasks::import_sample_products_from_csv( $csv_file );
$csv_file(string) (required)
CSV file path.

OnboardingTasks::import_sample_products_from_csv() code WC 11.2.1

public static function import_sample_products_from_csv( $csv_file ) {
	include_once WC_ABSPATH . 'includes/import/class-wc-product-csv-importer.php';

	if ( file_exists( $csv_file ) && class_exists( 'WC_Product_CSV_Importer' ) ) {
		// Override locale so we can return mappings from WooCommerce in English language stores.
		add_filter( 'locale', '__return_false', 9999 );
		$importer_class = apply_filters( 'woocommerce_product_csv_importer_class', 'WC_Product_CSV_Importer' );

		try {
			$args = array(
				'parse'   => true,
				'mapping' => self::get_header_mappings( $csv_file ),
			);

			/**
			 * Filter the arguments used by the product CSV importer.
			 *
			 * @since 3.1.0
			 *
			 * @param array  $args           Importer arguments.
			 * @param string $importer_class Importer class name.
			 */
			$args = apply_filters( 'woocommerce_product_csv_importer_args', $args, $importer_class );

			$importer = new $importer_class( $csv_file, $args );
			$import   = $importer->import();
			return $import;
		} catch ( \RuntimeException $e ) {
			return new \WP_Error( 'woocommerce_rest_import_error', $e->getMessage() );
		}
	} else {
		return new \WP_Error( 'woocommerce_rest_import_error', __( 'Sorry, the sample products data file was not found.', 'woocommerce' ) );
	}
}