Merge branch 'refactor/fix-importer-and-improve-unit-tests' into feature/product-csv-import-export

This commit is contained in:
Mike Jolley 2017-05-31 12:20:46 +01:00
commit 714fafbb28
6 changed files with 559 additions and 117 deletions

View File

@ -399,9 +399,9 @@ class WC_Product_CSV_Importer_Controller {
include( dirname( __FILE__ ) . '/mappings/mappings.php' ); include( dirname( __FILE__ ) . '/mappings/mappings.php' );
/** /**
* @hooked wc_importer_generic_mappings - 10 * @hooked wc_importer_generic_mappings - 10
* @hooked wc_importer_wordpress_mappings - 10 * @hooked wc_importer_wordpress_mappings - 10
*/ */
$default_columns = apply_filters( 'woocommerce_csv_product_import_mapping_default_columns', array( $default_columns = apply_filters( 'woocommerce_csv_product_import_mapping_default_columns', array(
__( 'ID', 'woocommerce' ) => 'id', __( 'ID', 'woocommerce' ) => 'id',
__( 'Type', 'woocommerce' ) => 'type', __( 'Type', 'woocommerce' ) => 'type',
@ -414,9 +414,10 @@ class WC_Product_CSV_Importer_Controller {
__( 'Description', 'woocommerce' ) => 'description', __( 'Description', 'woocommerce' ) => 'description',
__( 'Date sale price starts', 'woocommerce' ) => 'date_on_sale_from', __( 'Date sale price starts', 'woocommerce' ) => 'date_on_sale_from',
__( 'Date sale price ends', 'woocommerce' ) => 'date_on_sale_to', __( 'Date sale price ends', 'woocommerce' ) => 'date_on_sale_to',
__( 'Tax class', 'woocommerce' ) => 'tax_status', __( 'Tax status', 'woocommerce' ) => 'tax_status',
__( 'Tax class', 'woocommerce' ) => 'tax_class',
__( 'In stock?', 'woocommerce' ) => 'stock_status', __( 'In stock?', 'woocommerce' ) => 'stock_status',
__( 'Stock', 'woocommerce' ) => 'stock', __( 'Stock', 'woocommerce' ) => 'stock_quantity',
__( 'Backorders allowed?', 'woocommerce' ) => 'backorders', __( 'Backorders allowed?', 'woocommerce' ) => 'backorders',
__( 'Sold individually?', 'woocommerce' ) => 'sold_individually', __( 'Sold individually?', 'woocommerce' ) => 'sold_individually',
sprintf( __( 'Weight (%s)', 'woocommerce' ), $weight_unit ) => 'weight', sprintf( __( 'Weight (%s)', 'woocommerce' ), $weight_unit ) => 'weight',
@ -430,13 +431,13 @@ class WC_Product_CSV_Importer_Controller {
__( 'Categories', 'woocommerce' ) => 'category_ids', __( 'Categories', 'woocommerce' ) => 'category_ids',
__( 'Tags', 'woocommerce' ) => 'tag_ids', __( 'Tags', 'woocommerce' ) => 'tag_ids',
__( 'Shipping class', 'woocommerce' ) => 'shipping_class_id', __( 'Shipping class', 'woocommerce' ) => 'shipping_class_id',
__( 'Images', 'woocommerce' ) => 'image_id', __( 'Images', 'woocommerce' ) => 'images',
__( 'Download limit', 'woocommerce' ) => 'download_limit', __( 'Download limit', 'woocommerce' ) => 'download_limit',
__( 'Download expiry days', 'woocommerce' ) => 'download_expiry', __( 'Download expiry days', 'woocommerce' ) => 'download_expiry',
__( 'Parent', 'woocommerce' ) => 'parent_id', __( 'Parent', 'woocommerce' ) => 'parent_id',
__( 'Grouped products', 'woocommerce' ) => 'grouped_products',
__( 'Upsells', 'woocommerce' ) => 'upsell_ids', __( 'Upsells', 'woocommerce' ) => 'upsell_ids',
__( 'Cross-sells', 'woocommerce' ) => 'cross_sell_ids', __( 'Cross-sells', 'woocommerce' ) => 'cross_sell_ids',
__( 'Grouped products', 'woocommerce' ) => 'grouped_products',
__( 'External URL', 'woocommerce' ) => 'product_url', __( 'External URL', 'woocommerce' ) => 'product_url',
__( 'Button text', 'woocommerce' ) => 'button_text', __( 'Button text', 'woocommerce' ) => 'button_text',
) ); ) );
@ -541,15 +542,15 @@ class WC_Product_CSV_Importer_Controller {
'purchase_note' => __( 'Purchase note', 'woocommerce' ), 'purchase_note' => __( 'Purchase note', 'woocommerce' ),
'sale_price' => __( 'Sale price', 'woocommerce' ), 'sale_price' => __( 'Sale price', 'woocommerce' ),
'regular_price' => __( 'Regular Price', 'woocommerce' ), 'regular_price' => __( 'Regular Price', 'woocommerce' ),
'stock' => __( 'Stock', 'woocommerce' ), 'stock_quantity' => __( 'Stock', 'woocommerce' ),
'category_ids' => __( 'Categories', 'woocommerce' ), 'category_ids' => __( 'Categories', 'woocommerce' ),
'tag_ids' => __( 'Tags', 'woocommerce' ), 'tag_ids' => __( 'Tags', 'woocommerce' ),
'shipping_class_id' => __( 'Shipping class', 'woocommerce' ), 'shipping_class_id' => __( 'Shipping class', 'woocommerce' ),
'image_id' => __( 'Images', 'woocommerce' ), 'images' => __( 'Images', 'woocommerce' ),
'parent_id' => __( 'Parent', 'woocommerce' ), 'parent_id' => __( 'Parent', 'woocommerce' ),
'grouped_products' => __( 'Grouped products', 'woocommerce' ),
'upsell_ids' => __( 'Upsells', 'woocommerce' ), 'upsell_ids' => __( 'Upsells', 'woocommerce' ),
'cross_sell_ids' => __( 'Cross-sells', 'woocommerce' ), 'cross_sell_ids' => __( 'Cross-sells', 'woocommerce' ),
'grouped_products' => __( 'Grouped products', 'woocommerce' ),
'external' => array( 'external' => array(
'name' => __( 'External product', 'woocommerce' ), 'name' => __( 'External product', 'woocommerce' ),
'options' => array( 'options' => array(

View File

@ -17,7 +17,7 @@ function wc_importer_generic_mappings( $mappings ) {
__( 'Product Title', 'woocommerce' ) => 'name', __( 'Product Title', 'woocommerce' ) => 'name',
__( 'Price', 'woocommerce' ) => 'regular_price', __( 'Price', 'woocommerce' ) => 'regular_price',
__( 'Parent SKU', 'woocommerce' ) => 'parent_id', __( 'Parent SKU', 'woocommerce' ) => 'parent_id',
__( 'Quantity', 'woocommerce' ) => 'stock', __( 'Quantity', 'woocommerce' ) => 'stock_quantity',
); );
return array_merge( $mappings, $generic_mappings ); return array_merge( $mappings, $generic_mappings );

View File

@ -134,7 +134,7 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
/** /**
* Prepare a single product for create or update. * Prepare a single product for create or update.
* *
* @param array $data Row data. * @param array $data Item data.
* @return WC_Product|WP_Error * @return WC_Product|WP_Error
*/ */
protected function get_product_object( $data ) { protected function get_product_object( $data ) {
@ -177,6 +177,11 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
*/ */
protected function process_item( $data ) { protected function process_item( $data ) {
try { try {
// Get product ID from SKU if created during the importation.
if ( empty( $data['id'] ) && ! empty( $data['sku'] ) && ( $product_id = wc_get_product_id_by_sku( $data['sku'] ) ) ) {
$data['id'] = $product_id;
}
$object = $this->get_product_object( $data ); $object = $this->get_product_object( $data );
$updating = false; $updating = false;
@ -226,8 +231,8 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
/** /**
* Convert raw image URLs to IDs and set. * Convert raw image URLs to IDs and set.
* *
* @param WC_Product $product * @param WC_Product $product Product instance.
* @param array $data * @param array $data Item data.
*/ */
protected function set_image_data( &$product, $data ) { protected function set_image_data( &$product, $data ) {
// Image URLs need converting to IDs before inserting. // Image URLs need converting to IDs before inserting.
@ -249,8 +254,8 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
/** /**
* Append meta data. * Append meta data.
* *
* @param WC_Product $product * @param WC_Product $product Product instance.
* @param array $data * @param array $data Item data.
*/ */
protected function set_meta_data( &$product, $data ) { protected function set_meta_data( &$product, $data ) {
if ( isset( $data['meta_data'] ) ) { if ( isset( $data['meta_data'] ) ) {
@ -264,7 +269,7 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
* Set product data. * Set product data.
* *
* @param WC_Product $product Product instance. * @param WC_Product $product Product instance.
* @param array $data Row data. * @param array $data Item data.
* *
* @return WC_Product|WP_Error * @return WC_Product|WP_Error
* @throws Exception * @throws Exception
@ -356,7 +361,7 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
* Set variation data. * Set variation data.
* *
* @param WC_Product $variation Product instance. * @param WC_Product $variation Product instance.
* @param array $data Row data. * @param array $data Item data.
* *
* @return WC_Product|WP_Error * @return WC_Product|WP_Error
* @throws Exception * @throws Exception

View File

@ -304,6 +304,47 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
return array_map( 'esc_url_raw', array_map( 'trim', explode( ',', $field ) ) ); return array_map( 'esc_url_raw', array_map( 'trim', explode( ',', $field ) ) );
} }
/**
* Parse dates from a CSV.
* Dates requires the format YYYY-MM-DD.
*
* @param string $field Field value.
* @return string|null
*/
protected function parse_date_field( $field ) {
if ( empty( $field ) ) {
return null;
}
if ( preg_match( '/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $field ) ) {
return $field;
}
return null;
}
/**
* Parse backorders from a CSV.
*
* @param string $field Field value.
* @return string
*/
protected function parse_backorders_field( $field ) {
if ( empty( $field ) ) {
return '';
}
$field = $this->parse_bool_field( $field );
if ( 'notify' === $field ) {
return 'notify';
} elseif ( is_bool( $field ) ) {
return $field ? 'yes' : 'no';
}
return '';
}
/** /**
* Get formatting callback. * Get formatting callback.
* *
@ -320,13 +361,13 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
'type' => array( $this, 'parse_comma_field' ), 'type' => array( $this, 'parse_comma_field' ),
'published' => array( $this, 'parse_bool_field' ), 'published' => array( $this, 'parse_bool_field' ),
'featured' => array( $this, 'parse_bool_field' ), 'featured' => array( $this, 'parse_bool_field' ),
'date_on_sale_from' => 'strtotime', 'date_on_sale_from' => array( $this, 'parse_date_field' ),
'date_on_sale_to' => 'strtotime', 'date_on_sale_to' => array( $this, 'parse_date_field' ),
'name' => 'wp_filter_post_kses', 'name' => 'wp_filter_post_kses',
'short_description' => 'wp_filter_post_kses', 'short_description' => 'wp_filter_post_kses',
'description' => 'wp_filter_post_kses', 'description' => 'wp_filter_post_kses',
'manage_stock' => array( $this, 'parse_bool_field' ), 'manage_stock' => array( $this, 'parse_bool_field' ),
'backorders' => array( $this, 'parse_bool_field' ), 'backorders' => array( $this, 'parse_backorders_field' ),
'stock_status' => array( $this, 'parse_bool_field' ), 'stock_status' => array( $this, 'parse_bool_field' ),
'sold_individually' => array( $this, 'parse_bool_field' ), 'sold_individually' => array( $this, 'parse_bool_field' ),
'width' => array( $this, 'parse_float_field' ), 'width' => array( $this, 'parse_float_field' ),
@ -337,7 +378,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
'purchase_note' => 'wp_filter_post_kses', 'purchase_note' => 'wp_filter_post_kses',
'price' => 'wc_format_decimal', 'price' => 'wc_format_decimal',
'regular_price' => 'wc_format_decimal', 'regular_price' => 'wc_format_decimal',
'stock_quantity' => 'absint', 'stock_quantity' => 'wc_stock_amount',
'category_ids' => array( $this, 'parse_categories_field' ), 'category_ids' => array( $this, 'parse_categories_field' ),
'tag_ids' => array( $this, 'parse_tags_field' ), 'tag_ids' => array( $this, 'parse_tags_field' ),
'shipping_class_id' => array( $this, 'parse_shipping_class_field' ), 'shipping_class_id' => array( $this, 'parse_shipping_class_field' ),
@ -405,11 +446,6 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
protected function expand_data( $data ) { protected function expand_data( $data ) {
$data = apply_filters( 'woocommerce_product_importer_pre_expand_data', $data ); $data = apply_filters( 'woocommerce_product_importer_pre_expand_data', $data );
// Product ID and SKU mapping.
if ( empty( $data['id'] ) && ! empty( $data['sku'] ) && ( $product_id = wc_get_product_id_by_sku( $data['sku'] ) ) ) {
$data['id'] = $product_id;
}
// Status is mapped from a special published field. // Status is mapped from a special published field.
if ( isset( $data['published'] ) ) { if ( isset( $data['published'] ) ) {
$data['status'] = ( $data['published'] ? 'publish' : 'draft' ); $data['status'] = ( $data['published'] ? 'publish' : 'draft' );
@ -446,11 +482,6 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
$data['stock_status'] = $data['stock_status'] ? 'instock' : 'outofstock'; $data['stock_status'] = $data['stock_status'] ? 'instock' : 'outofstock';
} }
// Backorders is bool.
if ( isset( $data['backorders'] ) ) {
$data['backorders'] = $data['backorders'] ? 'yes' : 'no';
}
// Prepare grouped products. // Prepare grouped products.
if ( isset( $data['grouped_products'] ) ) { if ( isset( $data['grouped_products'] ) ) {
$data['children'] = $data['grouped_products']; $data['children'] = $data['grouped_products'];
@ -483,7 +514,9 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
unset( $data[ $key ] ); unset( $data[ $key ] );
} elseif ( $this->starts_with( $key, 'attributes:default' ) ) { } elseif ( $this->starts_with( $key, 'attributes:default' ) ) {
$attributes[ str_replace( 'attributes:default', '', $key ) ]['default'] = $value; if ( ! empty( $value ) ) {
$attributes[ str_replace( 'attributes:default', '', $key ) ]['default'] = $value;
}
unset( $data[ $key ] ); unset( $data[ $key ] );
// Downloads. // Downloads.
@ -510,7 +543,14 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
} }
if ( ! empty( $attributes ) ) { if ( ! empty( $attributes ) ) {
$data['raw_attributes'] = $attributes; // Remove empty attributes and clear indexes.
foreach ( $attributes as $attribute ) {
if ( empty( $attribute['name'] ) ) {
continue;
}
$data['raw_attributes'][] = $attribute;
}
} }
if ( ! empty( $downloads ) ) { if ( ! empty( $downloads ) ) {
@ -523,7 +563,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
$data['downloads'][] = array( $data['downloads'][] = array(
'name' => $file['name'] ? $file['name'] : wc_get_filename_from_url( $file['url'] ), 'name' => $file['name'] ? $file['name'] : wc_get_filename_from_url( $file['url'] ),
'file' => apply_filters( 'woocommerce_file_download_path', $file['url'], $product, $key ), 'file' => $file['url'],
); );
} }
} }

View File

@ -23,30 +23,78 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case {
require_once $bootstrap->plugin_dir . '/includes/import/class-wc-product-csv-importer.php'; require_once $bootstrap->plugin_dir . '/includes/import/class-wc-product-csv-importer.php';
} }
/**
* Get CSV mapped items.
*
* @since 3.1.0
* @return array
*/
private function get_csv_mapped_items() {
return array(
'Type' => 'type',
'SKU' => 'sku',
'Name' => 'name',
'Published' => 'published',
'Is featured?' => 'featured',
'Visibility in catalog' => 'catalog_visibility',
'Short description' => 'short_description',
'Description' => 'description',
'Date sale price starts' => 'date_on_sale_from',
'Date sale price ends' => 'date_on_sale_to',
'Tax status' => 'tax_status',
'Tax class' => 'tax_class',
'In stock?' => 'stock_status',
'Stock' => 'stock_quantity',
'Backorders allowed?' => 'backorders',
'Sold individually?' => 'sold_individually',
'Weight (kg)' => 'weight',
'Length (cm)' => 'length',
'Width (cm)' => 'width',
'Height (cm)' => 'height',
'Allow customer reviews?' => 'reviews_allowed',
'Purchase note' => 'purchase_note',
'Sale price' => 'sale_price',
'Regular price' => 'regular_price',
'Categories' => 'category_ids',
'Tags' => 'tag_ids',
'Shipping class' => 'shipping_class_id',
'Images' => 'images',
'Download limit' => 'download_limit',
'Download expiry days' => 'download_expiry',
'Parent' => 'parent_id',
'Upsells' => 'upsell_ids',
'Cross-sells' => 'cross_sell_ids',
'Grouped products' => 'grouped_products',
'External URL' => 'product_url',
'Button text' => 'button_text',
'Attribute 1 name' => 'attributes:name1',
'Attribute 1 value(s)' => 'attributes:value2',
'Attribute 2 name' => 'attributes:name2',
'Attribute 2 value(s)' => 'attributes:value2',
'Attribute 1 default' => 'attributes:default1',
'Attribute 2 default' => 'attributes:default2',
'Download 1 name' => 'downloads:name1',
'Download 1 URL' => 'downloads:url1',
);
}
/** /**
* Test import. * Test import.
* @todo enable the importer again after conclude the parser.
* @since 3.1.0 * @since 3.1.0
*/ */
public function test_import() { public function test_import() {
$mapped = array(
'Type' => 'type',
'SKU' => 'sku',
'Name' => 'name',
'Published' => 'published',
'Regular price' => 'regular_price',
);
$args = array( $args = array(
'mapping' => $mapped, 'mapping' => $this->get_csv_mapped_items(),
'parse' => true, 'parse' => true,
); );
$importer = new WC_Product_CSV_Importer( $this->csv_file, $args ); $importer = new WC_Product_CSV_Importer( $this->csv_file, $args );
$results = $importer->import(); $results = $importer->import();
$this->assertEquals( 3, count( $results['imported'] ) ); $this->assertEquals( 7, count( $results['imported'] ) );
$this->assertEquals( 0, count( $results['failed'] ) ); $this->assertEquals( 0, count( $results['failed'] ) );
$this->assertEquals( 0, count( $results['updated'] ) );
$this->assertEquals( 0, count( $results['skipped'] ) );
// Exclude imported products. // Exclude imported products.
foreach ( $results['imported'] as $id ) { foreach ( $results['imported'] as $id ) {
@ -59,14 +107,8 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case {
* @since 3.1.0 * @since 3.1.0
*/ */
public function test_get_raw_keys() { public function test_get_raw_keys() {
$importer = new WC_Product_CSV_Importer( $this->csv_file ); $importer = new WC_Product_CSV_Importer( $this->csv_file, array( 'lines' => 1 ) );
$raw_keys = array( $raw_keys = array_keys( $this->get_csv_mapped_items() );
'Type',
'SKU',
'Name',
'Published',
'Regular price',
);
$this->assertEquals( $raw_keys, $importer->get_raw_keys() ); $this->assertEquals( $raw_keys, $importer->get_raw_keys() );
} }
@ -76,21 +118,14 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case {
* @since 3.1.0 * @since 3.1.0
*/ */
public function test_get_mapped_keys() { public function test_get_mapped_keys() {
$mapped = array(
'Type' => 'type',
'SKU' => 'sku',
'Name' => 'name',
'Published' => 'published',
'Regular price' => 'regular_price',
);
$args = array( $args = array(
'mapping' => $mapped, 'mapping' => $this->get_csv_mapped_items(),
'lines' => 1,
); );
$importer = new WC_Product_CSV_Importer( $this->csv_file, $args ); $importer = new WC_Product_CSV_Importer( $this->csv_file, $args );
$this->assertEquals( array_values( $mapped ), $importer->get_mapped_keys() ); $this->assertEquals( array_values( $args['mapping'] ), $importer->get_mapped_keys() );
} }
/** /**
@ -98,28 +133,99 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case {
* @since 3.1.0 * @since 3.1.0
*/ */
public function test_get_raw_data() { public function test_get_raw_data() {
$importer = new WC_Product_CSV_Importer( $this->csv_file, array( 'parse' => false ) ); $importer = new WC_Product_CSV_Importer( $this->csv_file, array( 'parse' => false, 'lines' => 2 ) );
$items = array( $items = array(
array( array(
'simple', 'simple',
'PRODUCT-01', 'WOOLOGO',
'Imported Product 1', 'Woo Logo',
1, '1',
40, '',
'visible',
'Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.',
'Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.',
'2017-01-01',
'2030-01-01',
'taxable',
'standard',
'1',
'5',
'notify',
'1',
'1',
'1',
'20',
'40',
'1',
'Lorem ipsum dolor sit amet.',
'18',
'20',
'Clothing, Clothing > T-shirts',
'',
'',
'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_1_front.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_1_back.jpg',
'',
'',
'',
'WOOALBUM',
'WOOALBUM',
'',
'',
'',
'Color',
'Red',
'',
'',
'',
'',
'',
'',
), ),
array( array(
'simple', 'simple, downloadable, virtual',
'PRODUCT-02', 'WOOALBUM',
'Imported Product 2', 'Woo Album #1',
1, '1',
41, '1',
), 'visible',
array( 'Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.',
'simple', 'Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.',
'PRODUCT-03', '',
'Imported Product 3', '',
1, 'taxable',
42, 'standard',
'1',
'',
'',
'',
'',
'',
'',
'',
'1',
'Lorem ipsum dolor sit amet.',
'',
'5',
'Music > Albums, Music',
'Woo',
'',
'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_1_angle.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_1_flat.jpg',
'10',
'90',
'',
'WOOLOGO',
'WOOLOGO',
'',
'',
'',
'Label',
'WooCommerce',
'Vinyl',
'180-Gram',
'',
'',
'Album flac',
'http://woo.dev/albums/album.flac',
), ),
); );
@ -131,50 +237,336 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case {
* @since 3.1.0 * @since 3.1.0
*/ */
public function test_get_parsed_data() { public function test_get_parsed_data() {
$mapped = array(
'Type' => 'type',
'SKU' => 'sku',
'Name' => 'name',
'Published' => 'published',
'Regular price' => 'regular_price',
);
$args = array( $args = array(
'mapping' => $mapped, 'mapping' => $this->get_csv_mapped_items(),
'parse' => true, 'parse' => true
); );
$importer = new WC_Product_CSV_Importer( $this->csv_file, $args ); $importer = new WC_Product_CSV_Importer( $this->csv_file, $args );
$items = array( $items = array(
array( array(
'type' => 'simple', 'type' => 'simple',
'sku' => 'PRODUCT-01', 'sku' => 'WOOLOGO',
'name' => 'Imported Product 1', 'name' => 'Woo Logo',
'regular_price' => '40', 'featured' => '',
'virtual' => false, 'catalog_visibility' => 'visible',
'downloadable' => false, 'short_description' => 'Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.',
'status' => 'publish', 'description' => 'Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.',
'date_on_sale_from' => '2017-01-01',
'date_on_sale_to' => '2030-01-01',
'tax_status' => 'taxable',
'tax_class' => 'standard',
'stock_status' => 'instock',
'stock_quantity' => 5,
'backorders' => 'notify',
'sold_individually' => true,
'weight' => 1.0,
'length' => 1.0,
'width' => 20.0,
'height' => 40.0,
'reviews_allowed' => true,
'purchase_note' => 'Lorem ipsum dolor sit amet.',
'sale_price' => '18',
'regular_price' => '20',
'shipping_class_id' => 0,
'download_limit' => 0,
'download_expiry' => 0,
'product_url' => '',
'button_text' => '',
'status' => 'publish',
'raw_image_id' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_1_front.jpg',
'raw_gallery_image_ids' => array( 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_1_back.jpg' ),
'virtual' => '',
'downloadable' => '',
'manage_stock' => true,
'virtual' => false,
'downloadable' => false,
'raw_attributes' => array(
array(
'name' => 'Color',
),
),
), ),
array( array(
'type' => 'simple', 'type' => 'simple',
'sku' => 'PRODUCT-02', 'sku' => 'WOOALBUM',
'name' => 'Imported Product 2', 'name' => 'Woo Album #1',
'regular_price' => '41', 'featured' => true,
'virtual' => false, 'catalog_visibility' => 'visible',
'downloadable' => false, 'short_description' => 'Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.',
'status' => 'publish', 'description' => 'Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.',
'date_on_sale_from' => null,
'date_on_sale_to' => null,
'tax_status' => 'taxable',
'tax_class' => 'standard',
'stock_status' => 'instock',
'stock_quantity' => 0,
'backorders' => '',
'sold_individually' => '',
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'reviews_allowed' => true,
'purchase_note' => 'Lorem ipsum dolor sit amet.',
'sale_price' => '',
'regular_price' => '5',
'shipping_class_id' => 0,
'download_limit' => 10,
'download_expiry' => 90,
'product_url' => '',
'button_text' => '',
'status' => 'publish',
'raw_image_id' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_1_angle.jpg',
'raw_gallery_image_ids' => array( 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_1_flat.jpg' ),
'virtual' => true,
'downloadable' => true,
'manage_stock' => false,
'raw_attributes' => array(
array(
'name' => 'Label',
),
array(
'value' => array( '180-Gram' ),
'name' => 'Vinyl',
),
),
'downloads' => array(
array(
'name' => 'Album flac',
'file' => 'http://woo.dev/albums/album.flac',
),
),
), ),
array( array(
'type' => 'simple', 'type' => 'external',
'sku' => 'PRODUCT-03', 'sku' => '',
'name' => 'Imported Product 3', 'name' => 'WooCommerce Product CSV Suite',
'regular_price' => '42', 'featured' => '',
'virtual' => false, 'catalog_visibility' => 'visible',
'downloadable' => false, 'short_description' => 'Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.',
'status' => 'publish', 'description' => 'Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.',
'date_on_sale_from' => null,
'date_on_sale_to' => null,
'tax_status' => 'taxable',
'tax_class' => 'standard',
'stock_status' => 'instock',
'stock_quantity' => 0,
'backorders' => '',
'sold_individually' => '',
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'reviews_allowed' => false,
'purchase_note' => 'Lorem ipsum dolor sit amet.',
'sale_price' => '',
'regular_price' => '199',
'shipping_class_id' => 0,
'download_limit' => 0,
'download_expiry' => 0,
'product_url' => 'https://woocommerce.com/products/product-csv-import-suite/',
'button_text' => 'Buy on WooCommerce.com',
'status' => 'publish',
'raw_image_id' => null,
'virtual' => false,
'downloadable' => false,
'manage_stock' => false,
),
array(
'type' => 'variable',
'sku' => 'WOOIDEA',
'name' => 'Ship Your Idea',
'featured' => '',
'catalog_visibility' => 'visible',
'short_description' => 'Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.',
'description' => 'Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.',
'date_on_sale_from' => null,
'date_on_sale_to' => null,
'tax_status' => '',
'tax_class' => '',
'stock_status' => 'outofstock',
'stock_quantity' => 0,
'backorders' => '',
'sold_individually' => '',
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'reviews_allowed' => true,
'purchase_note' => 'Lorem ipsum dolor sit amet.',
'sale_price' => '',
'regular_price' => '',
'shipping_class_id' => 0,
'download_limit' => 0,
'download_expiry' => 0,
'product_url' => '',
'button_text' => '',
'status' => 'publish',
'raw_image_id' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg',
'raw_gallery_image_ids' => array(
'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_back.jpg',
'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_front.jpg',
'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_back.jpg',
),
'virtual' => false,
'downloadable' => false,
'manage_stock' => false,
'raw_attributes' => array(
array(
'name' => 'Color',
'default' => 'Green',
),
array(
'value' => array( 'M', 'L' ),
'name' => 'Size',
'default' => 'L'
),
),
),
array(
'type' => 'variation',
'sku' => '',
'name' => '',
'featured' => '',
'catalog_visibility' => 'visible',
'short_description' => '',
'description' => 'Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.',
'date_on_sale_from' => null,
'date_on_sale_to' => null,
'tax_status' => 'taxable',
'tax_class' => 'standard',
'stock_status' => 'instock',
'stock_quantity' => 6,
'backorders' => '',
'sold_individually' => '',
'weight' => 1.0,
'length' => 2.0,
'width' => 25.0,
'height' => 55.0,
'reviews_allowed' => '',
'purchase_note' => '',
'sale_price' => '',
'regular_price' => '20',
'shipping_class_id' => 0,
'download_limit' => 0,
'download_expiry' => 0,
'product_url' => '',
'button_text' => '',
'status' => 'publish',
'raw_image_id' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg',
'virtual' => false,
'downloadable' => false,
'manage_stock' => true,
'raw_attributes' => array(
array(
'name' => 'Color',
),
array(
'value' => array( 'M' ),
'name' => 'Size',
),
),
),
array(
'type' => 'variation',
'sku' => '',
'name' => '',
'featured' => '',
'catalog_visibility' => 'visible',
'short_description' => '',
'description' => 'Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.',
'date_on_sale_from' => null,
'date_on_sale_to' => null,
'tax_status' => 'taxable',
'tax_class' => 'standard',
'stock_status' => 'instock',
'stock_quantity' => 10,
'backorders' => 'yes',
'sold_individually' => '',
'weight' => 1.0,
'length' => 2.0,
'width' => 25.0,
'height' => 55.0,
'reviews_allowed' => '',
'purchase_note' => '',
'sale_price' => '17.99',
'regular_price' => '20',
'shipping_class_id' => 0,
'download_limit' => 0,
'download_expiry' => 0,
'product_url' => '',
'button_text' => '',
'status' => 'publish',
'raw_image_id' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_front.jpg',
'virtual' => false,
'downloadable' => false,
'manage_stock' => true,
'raw_attributes' => array(
array(
'name' => 'Color',
),
array(
'value' => array( 'L' ),
'name' => 'Size'
)
),
),
array(
'type' => 'grouped',
'sku' => '',
'name' => 'Best Woo Products',
'featured' => true,
'catalog_visibility' => 'visible',
'short_description' => 'Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.',
'description' => 'Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.',
'date_on_sale_from' => null,
'date_on_sale_to' => null,
'tax_status' => '',
'tax_class' => '',
'stock_status' => 'instock',
'stock_quantity' => 0,
'backorders' => '',
'sold_individually' => '',
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'reviews_allowed' => '',
'purchase_note' => '',
'sale_price' => '',
'regular_price' => '',
'shipping_class_id' => 0,
'download_limit' => 0,
'download_expiry' => 0,
'product_url' => '',
'button_text' => '',
'status' => 'publish',
'raw_image_id' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_1_front.jpg',
'raw_gallery_image_ids' => array( 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_1_angle.jpg' ),
'virtual' => false,
'downloadable' => false,
'manage_stock' => false,
), ),
); );
$this->assertEquals( $items, $importer->get_parsed_data() ); $parsed_data = $importer->get_parsed_data();
// Remove fields that depends on product ID or term ID.
foreach ( $parsed_data as &$data ) {
unset( $data['parent_id'], $data['upsell_ids'], $data['cross_sell_ids'], $data['children'], $data['category_ids'], $data['tag_ids'] );
}
$this->assertEquals( $items, $parsed_data );
// Remove temporary products.
$temp_products = get_posts( array(
'post_status' => 'importing',
'post_type' => 'product',
'fields' => 'ids',
) );
foreach ( $temp_products as $id ) {
wp_delete_post( $id, true );
}
} }
} }

View File

@ -1,4 +1,8 @@
Type,SKU,Name,Published,Regular price Type,SKU,Name,Published,Is featured?,Visibility in catalog,Short description,Description,Date sale price starts,Date sale price ends,Tax status,Tax class,In stock?,Stock,Backorders allowed?,Sold individually?,Weight (kg),Length (cm),Width (cm),Height (cm),Allow customer reviews?,Purchase note,Sale price,Regular price,Categories,Tags,Shipping class,Images,Download limit,Download expiry days,Parent,Upsells,Cross-sells,Grouped products,External URL,Button text,Attribute 1 name,Attribute 1 value(s),Attribute 2 name,Attribute 2 value(s),Attribute 1 default,Attribute 2 default,Download 1 name,Download 1 URL
simple,PRODUCT-01,Imported Product 1,1,40 simple,WOOLOGO,Woo Logo,1,,visible,"Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.","Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.",2017-01-01,2030-01-01,taxable,standard,1,5,notify,1,1,1,20,40,1,Lorem ipsum dolor sit amet.,18,20,"Clothing, Clothing > T-shirts",,,"http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_1_front.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_1_back.jpg",,,,WOOALBUM,WOOALBUM,,,,Color,Red,,,,,,
simple,PRODUCT-02,Imported Product 2,1,41 "simple, downloadable, virtual",WOOALBUM,Woo Album #1,1,1,visible,"Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.","Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.",,,taxable,standard,1,,,,,,,,1,Lorem ipsum dolor sit amet.,,5,"Music > Albums, Music",Woo,,"http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_1_angle.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_1_flat.jpg",10,90,,WOOLOGO,WOOLOGO,,,,Label,WooCommerce,Vinyl,180-Gram,,,Album flac,http://woo.dev/albums/album.flac
simple,PRODUCT-03,Imported Product 3,1,42 external,,WooCommerce Product CSV Suite,1,,visible,"Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.","Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.",,,taxable,standard,1,,,,,,,,0,Lorem ipsum dolor sit amet.,,199,Software,WooCommerce,,,,,,,,,https://woocommerce.com/products/product-csv-import-suite/,Buy on WooCommerce.com,,,,,,,,
variable,WOOIDEA,Ship Your Idea,1,,visible,"Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.","Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.",,,,,,,,,,,,,1,Lorem ipsum dolor sit amet.,,,"Clothing, Clothing > T-shirts",,,"http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_back.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_front.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_back.jpg",,,,,,,,,Color,"Black, Green",Size,"M, L",Green,L,,
variation,,,1,,visible,,"Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.",,,taxable,standard,1,6,0,,1,2,25,55,,,,20,,,,http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg,,,WOOIDEA,,,,,,Color,Black,Size,M,,,,
variation,,,1,,visible,,"Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.",,,taxable,standard,1,10,1,,1,2,25,55,,,17.99,20,,,,http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_front.jpg,,,WOOIDEA,,,,,,Color,Green,Size,L,,,,
grouped,,Best Woo Products,1,1,visible,"Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.","Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.",,,,,1,,,,,,,,,,,,"Clothing, Clothing > T-shirts, Music > Albums, Music",,,"http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_1_front.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_1_angle.jpg",,,,,,"WOOLOGO, WOOALBUM",,,,,,,,,,

1 Type SKU Name Published Is featured? Visibility in catalog Short description Description Date sale price starts Date sale price ends Tax status Tax class In stock? Stock Backorders allowed? Sold individually? Weight (kg) Length (cm) Width (cm) Height (cm) Allow customer reviews? Purchase note Sale price Regular price Categories Tags Shipping class Images Download limit Download expiry days Parent Upsells Cross-sells Grouped products External URL Button text Attribute 1 name Attribute 1 value(s) Attribute 2 name Attribute 2 value(s) Attribute 1 default Attribute 2 default Download 1 name Download 1 URL
2 simple PRODUCT-01 WOOLOGO Imported Product 1 Woo Logo 1 visible Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an. Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an. 2017-01-01 2030-01-01 taxable standard 1 5 notify 1 1 1 20 40 1 Lorem ipsum dolor sit amet. 18 40 20 Clothing, Clothing > T-shirts http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_1_front.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_1_back.jpg WOOALBUM WOOALBUM Color Red
3 simple simple, downloadable, virtual PRODUCT-02 WOOALBUM Imported Product 2 Woo Album #1 1 1 visible Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an. Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an. taxable standard 1 1 Lorem ipsum dolor sit amet. 41 5 Music > Albums, Music Woo http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_1_angle.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_1_flat.jpg 10 90 WOOLOGO WOOLOGO Label WooCommerce Vinyl 180-Gram Album flac http://woo.dev/albums/album.flac
4 simple external PRODUCT-03 Imported Product 3 WooCommerce Product CSV Suite 1 visible Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an. Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an. taxable standard 1 0 Lorem ipsum dolor sit amet. 42 199 Software WooCommerce https://woocommerce.com/products/product-csv-import-suite/ Buy on WooCommerce.com
5 variable WOOIDEA Ship Your Idea 1 visible Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an. Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an. 1 Lorem ipsum dolor sit amet. Clothing, Clothing > T-shirts http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_back.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_front.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_back.jpg Color Black, Green Size M, L Green L
6 variation 1 visible Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an. taxable standard 1 6 0 1 2 25 55 20 http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg WOOIDEA Color Black Size M
7 variation 1 visible Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an. taxable standard 1 10 1 1 2 25 55 17.99 20 http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_front.jpg WOOIDEA Color Green Size L
8 grouped Best Woo Products 1 1 visible Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an. Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an. 1 Clothing, Clothing > T-shirts, Music > Albums, Music http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_1_front.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_1_angle.jpg WOOLOGO, WOOALBUM