Automattic\WooCommerce\Admin\API

OnboardingTasks::import_sample_products_from_csv()public staticWC 1.0

Import sample products from given CSV path.

Method of the class: OnboardingTasks{}

Return

WP_Error|WP_REST_Response.

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 8.7.0

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' );
		$args           = array(
			'parse'   => true,
			'mapping' => self::get_header_mappings( $csv_file ),
		);
		$args           = apply_filters( 'woocommerce_product_csv_importer_args', $args, $importer_class );

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