Ability to parse tags with space separators

This commit is contained in:
Mike Jolley 2019-04-15 15:55:42 +01:00
parent 558e96fdb9
commit 93535047fd
3 changed files with 45 additions and 5 deletions

View File

@ -696,7 +696,8 @@ class WC_Product_CSV_Importer_Controller {
),
),
'category_ids' => __( 'Categories', 'woocommerce' ),
'tag_ids' => __( 'Tags', 'woocommerce' ),
'tag_ids' => __( 'Tags (comma separated)', 'woocommerce' ),
'tag_ids_spaces' => __( 'Tags (space separated)', 'woocommerce' ),
'shipping_class_id' => __( 'Shipping class', 'woocommerce' ),
'images' => __( 'Images', 'woocommerce' ),
'parent_id' => __( 'Parent', 'woocommerce' ),
@ -731,7 +732,7 @@ class WC_Product_CSV_Importer_Controller {
),
'reviews_allowed' => __( 'Allow customer reviews?', 'woocommerce' ),
'purchase_note' => __( 'Purchase note', 'woocommerce' ),
'meta:' . $meta => __( 'Import as meta', 'woocommerce' ),
'meta:' . $meta => __( 'Custom', 'woocommerce' ),
'menu_order' => __( 'Position', 'woocommerce' ),
);

View File

@ -753,12 +753,13 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
* separators.
*
* @since 3.2.0
* @param string $value Value to explode.
* @param string $value Value to explode.
* @param string $separator Separator separating each value. Defaults to comma.
* @return array
*/
protected function explode_values( $value ) {
protected function explode_values( $value, $separator = ',' ) {
$value = str_replace( '\\,', '::separator::', $value );
$values = explode( ',', $value );
$values = explode( $separator, $value );
$values = array_map( array( $this, 'explode_values_formatter' ), $values );
return $values;

View File

@ -459,6 +459,37 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
return $tags;
}
/**
* Parse a tag field from a CSV with space separators.
*
* @param string $value Field value.
*
* @return array
*/
public function parse_tags_spaces_field( $value ) {
if ( empty( $value ) ) {
return array();
}
$value = $this->unescape_data( $value );
$names = $this->explode_values( $value, ' ' );
$tags = array();
foreach ( $names as $name ) {
$term = get_term_by( 'name', $name, 'product_tag' );
if ( ! $term || is_wp_error( $term ) ) {
$term = (object) wp_insert_term( $name, 'product_tag' );
}
if ( ! is_wp_error( $term ) ) {
$tags[] = $term->term_id;
}
}
return $tags;
}
/**
* Parse a shipping class field from a CSV.
*
@ -652,6 +683,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
'stock_quantity' => array( $this, 'parse_stock_quantity_field' ),
'category_ids' => array( $this, 'parse_categories_field' ),
'tag_ids' => array( $this, 'parse_tags_field' ),
'tag_ids_spaces' => array( $this, 'parse_tags_spaces_field' ),
'shipping_class_id' => array( $this, 'parse_shipping_class_field' ),
'images' => array( $this, 'parse_images_field' ),
'parent_id' => array( $this, 'parse_relative_field' ),
@ -778,6 +810,12 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
unset( $data['grouped_products'] );
}
// Tag ids.
if ( isset( $data['tag_ids_spaces'] ) ) {
$data['tag_ids'] = $data['tag_ids_spaces'];
unset( $data['tag_ids_spaces'] );
}
// Handle special column names which span multiple columns.
$attributes = array();
$downloads = array();