From 0f54e570936b97df72fe50a4fd33fb3f0a579e37 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 11 Aug 2017 11:07:12 +0100 Subject: [PATCH 1/2] CSV Import; correctly set stock props. Closes #16398 --- .../import/class-wc-product-csv-importer.php | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/includes/import/class-wc-product-csv-importer.php b/includes/import/class-wc-product-csv-importer.php index a8170a5f333..cdc6cd82cec 100644 --- a/includes/import/class-wc-product-csv-importer.php +++ b/includes/import/class-wc-product-csv-importer.php @@ -283,6 +283,20 @@ class WC_Product_CSV_Importer extends WC_Product_Importer { return floatval( $value ); } + /** + * Parse the stock qty field. + * + * @param string $value Field value. + * @return float|string + */ + public function parse_stock_quantity_field( $value ) { + if ( '' === $value ) { + return $value; + } + + return wc_stock_amount( $value ); + } + /** * Parse a category field from a CSV. * Categories are separated by commas and subcategories are "parent > subcategory". @@ -488,7 +502,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer { 'purchase_note' => 'wp_filter_post_kses', 'price' => 'wc_format_decimal', 'regular_price' => 'wc_format_decimal', - 'stock_quantity' => 'wc_stock_amount', + 'stock_quantity' => array( $this, 'parse_stock_quantity_field' ), 'category_ids' => array( $this, 'parse_categories_field' ), 'tag_ids' => array( $this, 'parse_tags_field' ), 'shipping_class_id' => array( $this, 'parse_shipping_class_field' ), @@ -584,7 +598,13 @@ class WC_Product_CSV_Importer extends WC_Product_Importer { } if ( isset( $data['stock_quantity'] ) ) { - $data['manage_stock'] = 0 < $data['stock_quantity']; + if ( '' === $data['stock_quantity'] ) { + $data['manage_stock'] = false; + $data['stock_status'] = isset( $data['stock_status'] ) ? $data['stock_status'] : true; + } else { + $data['manage_stock'] = true; + $data['stock_status'] = 0 < $data['stock_quantity']; + } } // Stock is bool. From b8b9ae41f0fce83021db375d2bec4fd43ada372c Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Fri, 11 Aug 2017 21:46:51 -0300 Subject: [PATCH 2/2] Fixed importer unit tests --- tests/unit-tests/importer/product.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unit-tests/importer/product.php b/tests/unit-tests/importer/product.php index db2e4d7c9b3..45148650325 100644 --- a/tests/unit-tests/importer/product.php +++ b/tests/unit-tests/importer/product.php @@ -301,7 +301,7 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case { 'tax_status' => 'taxable', 'tax_class' => 'standard', 'stock_status' => 'instock', - 'stock_quantity' => 0, + 'stock_quantity' => '', 'backorders' => '', 'sold_individually' => '', 'weight' => '', @@ -352,7 +352,7 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case { 'tax_status' => 'taxable', 'tax_class' => 'standard', 'stock_status' => 'instock', - 'stock_quantity' => 0, + 'stock_quantity' => '', 'backorders' => '', 'sold_individually' => '', 'weight' => '', @@ -387,7 +387,7 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case { 'tax_status' => '', 'tax_class' => '', 'stock_status' => 'outofstock', - 'stock_quantity' => 0, + 'stock_quantity' => '', 'backorders' => '', 'sold_individually' => '', 'weight' => '', @@ -526,7 +526,7 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case { 'tax_status' => '', 'tax_class' => '', 'stock_status' => 'instock', - 'stock_quantity' => 0, + 'stock_quantity' => '', 'backorders' => '', 'sold_individually' => '', 'weight' => '',