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

This commit is contained in:
Claudio Sanches 2017-05-30 13:03:17 -03:00
commit ffd248f19d
6 changed files with 147 additions and 43 deletions

View File

@ -375,6 +375,7 @@ class WC_Product_CSV_Importer_Controller {
'download_limit',
'download_expiry',
'parent_id',
'grouped_products',
'upsell_ids',
'cross_sell_ids',
'product_url',
@ -394,44 +395,52 @@ class WC_Product_CSV_Importer_Controller {
protected function auto_map_columns( $raw_headers, $num_indexes = true ) {
$weight_unit = get_option( 'woocommerce_weight_unit' );
$dimension_unit = get_option( 'woocommerce_dimension_unit' );
$default_columns = array_flip( apply_filters( 'woocommerce_csv_product_import_mapping_default_columns', array(
'id' => __( 'ID', 'woocommerce' ),
'type' => __( 'Type', 'woocommerce' ),
'sku' => __( 'SKU', 'woocommerce' ),
'name' => __( 'Name', 'woocommerce' ),
'published' => __( 'Published', 'woocommerce' ),
'featured' => __( 'Is featured?', 'woocommerce' ),
'catalog_visibility' => __( 'Visibility in catalog', 'woocommerce' ),
'short_description' => __( 'Short description', 'woocommerce' ),
'description' => __( 'Description', 'woocommerce' ),
'date_on_sale_from' => __( 'Date sale price starts', 'woocommerce' ),
'date_on_sale_to' => __( 'Date sale price ends', 'woocommerce' ),
'tax_status' => __( 'Tax status', 'woocommerce' ),
'tax_class' => __( 'Tax class', 'woocommerce' ),
'stock_status' => __( 'In stock?', 'woocommerce' ),
'stock_quantity' => __( 'Stock', 'woocommerce' ),
'backorders' => __( 'Backorders allowed?', 'woocommerce' ),
'sold_individually' => __( 'Sold individually?', 'woocommerce' ),
'weight' => sprintf( __( 'Weight (%s)', 'woocommerce' ), $weight_unit ),
'length' => sprintf( __( 'Length (%s)', 'woocommerce' ), $dimension_unit ),
'width' => sprintf( __( 'Width (%s)', 'woocommerce' ), $dimension_unit ),
'height' => sprintf( __( 'Height (%s)', 'woocommerce' ), $dimension_unit ),
'reviews_allowed' => __( 'Allow customer reviews?', 'woocommerce' ),
'purchase_note' => __( 'Purchase note', 'woocommerce' ),
'sale_price' => __( 'Sale price', 'woocommerce' ),
'regular_price' => __( 'Regular price', 'woocommerce' ),
'category_ids' => __( 'Categories', 'woocommerce' ),
'tag_ids' => __( 'Tags', 'woocommerce' ),
'shipping_class_id' => __( 'Shipping class', 'woocommerce' ),
'images' => __( 'Images', 'woocommerce' ),
'download_limit' => __( 'Download limit', 'woocommerce' ),
'download_expiry' => __( 'Download expiry days', 'woocommerce' ),
'parent_id' => __( 'Parent', 'woocommerce' ),
'upsell_ids' => __( 'Upsells', 'woocommerce' ),
'cross_sell_ids' => __( 'Cross-sells', 'woocommerce' ),
'product_url' => __( 'External URL', 'woocommerce' ),
'button_text' => __( 'Button text', 'woocommerce' ),
) ) );
include( dirname( __FILE__ ) . '/mappings/mappings.php' );
/**
* @hooked wc_importer_generic_mappings - 10
* @hooked wc_importer_wordpress_mappings - 10
*/
$default_columns = apply_filters( 'woocommerce_csv_product_import_mapping_default_columns', array(
__( 'ID', 'woocommerce' ) => 'id',
__( 'Type', 'woocommerce' ) => 'type',
__( 'SKU', 'woocommerce' ) => 'sku',
__( 'Name', 'woocommerce' ) => 'name',
__( 'Published', 'woocommerce' ) => 'published',
__( 'Is featured?', 'woocommerce' ) => 'featured',
__( 'Visibility in catalog', 'woocommerce' ) => 'catalog_visibility',
__( 'Short description', 'woocommerce' ) => 'short_description',
__( 'Description', 'woocommerce' ) => 'description',
__( 'Date sale price starts', 'woocommerce' ) => 'date_on_sale_from',
__( 'Date sale price ends', 'woocommerce' ) => 'date_on_sale_to',
__( 'Tax status', 'woocommerce' ) => 'tax_status',
__( 'Tax class', 'woocommerce' ) => 'tax_class',
__( 'In stock?', 'woocommerce' ) => 'stock_status',
__( 'Stock', 'woocommerce' ) => 'stock_quantity',
__( 'Backorders allowed?', 'woocommerce' ) => 'backorders',
__( 'Sold individually?', 'woocommerce' ) => 'sold_individually',
sprintf( __( 'Weight (%s)', 'woocommerce' ), $weight_unit ) => 'weight',
sprintf( __( 'Length (%s)', 'woocommerce' ), $dimension_unit ) => 'length',
sprintf( __( 'Width (%s)', 'woocommerce' ), $dimension_unit ) => 'width',
sprintf( __( 'Height (%s)', 'woocommerce' ), $dimension_unit ) => 'height',
__( 'Allow customer reviews?', 'woocommerce' ) => 'reviews_allowed',
__( 'Purchase note', 'woocommerce' ) => 'purchase_note',
__( 'Sale price', 'woocommerce' ) => 'sale_price',
__( 'Regular price', 'woocommerce' ) => 'regular_price',
__( 'Categories', 'woocommerce' ) => 'category_ids',
__( 'Tags', 'woocommerce' ) => 'tag_ids',
__( 'Shipping class', 'woocommerce' ) => 'shipping_class_id',
__( 'Images', 'woocommerce' ) => 'images',
__( 'Download limit', 'woocommerce' ) => 'download_limit',
__( 'Download expiry days', 'woocommerce' ) => 'download_expiry',
__( 'Parent', 'woocommerce' ) => 'parent_id',
__( 'Grouped products', 'woocommerce' ) => 'grouped_products',
__( 'Upsells', 'woocommerce' ) => 'upsell_ids',
__( 'Cross-sells', 'woocommerce' ) => 'cross_sell_ids',
__( 'External URL', 'woocommerce' ) => 'product_url',
__( 'Button text', 'woocommerce' ) => 'button_text',
) );
$special_columns = array_map(
array( $this, 'sanitize_special_column_name_regex' ),
@ -538,6 +547,7 @@ class WC_Product_CSV_Importer_Controller {
'shipping_class_id' => __( 'Shipping class', 'woocommerce' ),
'images' => __( 'Images', 'woocommerce' ),
'parent_id' => __( 'Parent', 'woocommerce' ),
'grouped_products' => __( 'Grouped products', 'woocommerce' ),
'upsell_ids' => __( 'Upsells', 'woocommerce' ),
'cross_sell_ids' => __( 'Cross-sells', 'woocommerce' ),
'external' => array(

View File

@ -0,0 +1,25 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Add generic mappings.
*
* @since 3.1.0
* @param array $mappings
* @return array
*/
function wc_importer_generic_mappings( $mappings ) {
$generic_mappings = array(
__( 'Title', 'woocommerce' ) => 'name',
__( 'Product Title', 'woocommerce' ) => 'name',
__( 'Price', 'woocommerce' ) => 'regular_price',
__( 'Parent SKU', 'woocommerce' ) => 'parent_id',
__( 'Quantity', 'woocommerce' ) => 'stock',
);
return array_merge( $mappings, $generic_mappings );
}
add_filter( 'woocommerce_csv_product_import_mapping_default_columns', 'wc_importer_generic_mappings' );

View File

@ -0,0 +1,11 @@
<?php
/**
* Load up extra automatic mappings for the CSV importer.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
include( dirname( __FILE__ ) . '/generic.php' );
include( dirname( __FILE__ ) . '/wordpress.php' );

View File

@ -0,0 +1,26 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Add mappings for WordPress tables.
*
* @since 3.1.0
* @param array $mappings
* @return array
*/
function wc_importer_wordpress_mappings( $mappings ) {
$wp_mappings = array(
'post_id' => 'id',
'post_title' => 'name',
'post_content' => 'description',
'post_excerpt' => 'short_description',
'post_parent' => 'parent_id',
);
return array_merge( $mappings, $wp_mappings );
}
add_filter( 'woocommerce_csv_product_import_mapping_default_columns', 'wc_importer_wordpress_mappings' );

View File

@ -108,6 +108,7 @@ class WC_Product_CSV_Exporter extends WC_CSV_Batch_Exporter {
'download_limit' => __( 'Download limit', 'woocommerce' ),
'download_expiry' => __( 'Download expiry days', 'woocommerce' ),
'parent_id' => __( 'Parent', 'woocommerce' ),
'grouped_products' => __( 'Grouped products', 'woocommerce' ),
'upsell_ids' => __( 'Upsells', 'woocommerce' ),
'cross_sell_ids' => __( 'Cross-sells', 'woocommerce' ),
'product_url' => __( 'External URL', 'woocommerce' ),
@ -293,16 +294,40 @@ class WC_Product_CSV_Exporter extends WC_CSV_Batch_Exporter {
protected function get_column_value_parent_id( $product ) {
if ( $product->get_parent_id( 'edit' ) ) {
$parent = wc_get_product( $product->get_parent_id( 'edit' ) );
if ( $parent && $parent->get_sku() ) {
return $parent->get_sku();
} else {
return 'id:' . $parent->get_id();
if ( ! $parent ) {
return '';
}
return $parent->get_sku( 'edit' ) ? $parent->get_sku( 'edit' ) : 'id:' . $parent->get_id();
}
return '';
}
/**
* Get grouped_products value.
*
* @since 3.1.0
* @param WC_Product $product
* @return string
*/
protected function get_column_value_grouped_products( $product ) {
if ( 'grouped' !== $product->get_type() ) {
return '';
}
$grouped_products = array();
$child_ids = $product->get_children( 'edit' );
foreach ( $child_ids as $child_id ) {
$child = wc_get_product( $child_id );
if ( ! $child ) {
continue;
}
$grouped_products[] = $child->get_sku( 'edit' ) ? $child->get_sku( 'edit' ) : 'id:' . $child_id;
}
return implode( ',', $grouped_products );
}
/**
* Get download_limit value.
*

View File

@ -384,6 +384,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
'shipping_class_id' => array( $this, 'parse_shipping_class_field' ),
'images' => array( $this, 'parse_images_field' ),
'parent_id' => array( $this, 'parse_relative_field' ),
'grouped_products' => array( $this, 'parse_relative_comma_field' ),
'upsell_ids' => array( $this, 'parse_relative_comma_field' ),
'cross_sell_ids' => array( $this, 'parse_relative_comma_field' ),
'download_limit' => 'absint',
@ -480,6 +481,12 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
$data['stock_status'] = $data['stock_status'] ? 'instock' : 'outofstock';
}
// Prepare grouped products.
if ( isset( $data['grouped_products'] ) ) {
$data['children'] = $data['grouped_products'];
unset( $data['grouped_products'] );
}
// Handle special column names which span multiple columns.
$attributes = array();
$downloads = array();