From 93535047fd101391faeb82accacea3d0c680ddfb Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 15 Apr 2019 15:55:42 +0100 Subject: [PATCH] Ability to parse tags with space separators --- ...ass-wc-product-csv-importer-controller.php | 5 ++- .../import/abstract-wc-product-importer.php | 7 ++-- .../import/class-wc-product-csv-importer.php | 38 +++++++++++++++++++ 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/includes/admin/importers/class-wc-product-csv-importer-controller.php b/includes/admin/importers/class-wc-product-csv-importer-controller.php index 9f279ebfd2a..325719a9560 100644 --- a/includes/admin/importers/class-wc-product-csv-importer-controller.php +++ b/includes/admin/importers/class-wc-product-csv-importer-controller.php @@ -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' ), ); diff --git a/includes/import/abstract-wc-product-importer.php b/includes/import/abstract-wc-product-importer.php index 4a5fb01073d..1a0103a195c 100644 --- a/includes/import/abstract-wc-product-importer.php +++ b/includes/import/abstract-wc-product-importer.php @@ -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; diff --git a/includes/import/class-wc-product-csv-importer.php b/includes/import/class-wc-product-csv-importer.php index 1f4c317bf69..445da2e6f53 100644 --- a/includes/import/class-wc-product-csv-importer.php +++ b/includes/import/class-wc-product-csv-importer.php @@ -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();