diff --git a/plugins/woocommerce/changelog/fix-38069-fix-product-task-imports b/plugins/woocommerce/changelog/fix-38069-fix-product-task-imports new file mode 100644 index 00000000000..4e3ac85ae4c --- /dev/null +++ b/plugins/woocommerce/changelog/fix-38069-fix-product-task-imports @@ -0,0 +1,4 @@ +Significance: minor +Type: fix + +Fix product task import for cases when user locale is en_US diff --git a/plugins/woocommerce/includes/admin/importers/mappings/default.php b/plugins/woocommerce/includes/admin/importers/mappings/default.php index 0d9599da04c..f6315de18fa 100644 --- a/plugins/woocommerce/includes/admin/importers/mappings/default.php +++ b/plugins/woocommerce/includes/admin/importers/mappings/default.php @@ -32,7 +32,7 @@ function wc_importer_current_locale() { * @return array */ function wc_importer_default_english_mappings( $mappings ) { - if ( 'en_US' === wc_importer_current_locale() ) { + if ( 'en_US' === wc_importer_current_locale() && is_array( $mappings ) && count( $mappings ) > 0 ) { return $mappings; } @@ -92,7 +92,7 @@ add_filter( 'woocommerce_csv_product_import_mapping_default_columns', 'wc_import * @return array */ function wc_importer_default_special_english_mappings( $mappings ) { - if ( 'en_US' === wc_importer_current_locale() ) { + if ( 'en_US' === wc_importer_current_locale() && is_array( $mappings ) && count( $mappings ) > 0 ) { return $mappings; } diff --git a/plugins/woocommerce/src/Admin/API/OnboardingTasks.php b/plugins/woocommerce/src/Admin/API/OnboardingTasks.php index b77514cbf00..a7e80da382d 100644 --- a/plugins/woocommerce/src/Admin/API/OnboardingTasks.php +++ b/plugins/woocommerce/src/Admin/API/OnboardingTasks.php @@ -355,7 +355,7 @@ class OnboardingTasks extends \WC_REST_Data_Controller { * @return WP_REST_Response|WP_Error */ public static function create_product_from_template( $request ) { - $template_name = $request->get_param( 'template_name' ); + $template_name = basename( $request->get_param( 'template_name' ) ); $template_path = __DIR__ . '/Templates/' . $template_name . '_product.csv'; $template_path = apply_filters( 'woocommerce_product_template_csv_file_path', $template_path, $template_name ); diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/onboarding-tasks.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/onboarding-tasks.php index 3c32603d76b..f3c26ff6804 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/onboarding-tasks.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/onboarding-tasks.php @@ -135,6 +135,32 @@ class WC_Admin_Tests_API_Onboarding_Tasks extends WC_REST_Unit_Test_Case { $this->assertEquals( 'simple', $product->get_type() ); } + /** + * Test creating a product from a template with 'en_US' locale since this is handled differently. + */ + public function test_create_product_from_template_locale() { + wp_set_current_user( $this->user ); + + // Get the current user's locale. + $user_locale = get_user_locale(); + switch_to_locale( 'en_US', $this->user ); + + $request = new WP_REST_Request( 'POST', $this->endpoint . '/create_product_from_template' ); + $request->set_param( 'template_name', 'variable' ); + $response = $this->server->dispatch( $request ); + $data = $response->get_data(); + + $this->assertEquals( 200, $response->get_status() ); + + $this->assertArrayHasKey( 'id', $data ); + $product = wc_get_product( $data['id'] ); + $this->assertEquals( 'auto-draft', $product->get_status() ); + $this->assertEquals( 'variable', $product->get_type() ); + + // Set to initial locale. + switch_to_locale( $user_locale, $this->user ); + } + /** * Test that we get an error when template_name does not exist. */