Fix partial data import with product task imports (#38089)

* Fix product task import mapping issue with en_US locale

* Sanitize template name

* Changelog
This commit is contained in:
Ilyas Foo 2023-05-04 22:54:41 +08:00 committed by GitHub
parent 81b899b55e
commit 8e6fd416e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 3 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: fix
Fix product task import for cases when user locale is en_US

View File

@ -32,7 +32,7 @@ function wc_importer_current_locale() {
* @return array * @return array
*/ */
function wc_importer_default_english_mappings( $mappings ) { 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; return $mappings;
} }
@ -92,7 +92,7 @@ add_filter( 'woocommerce_csv_product_import_mapping_default_columns', 'wc_import
* @return array * @return array
*/ */
function wc_importer_default_special_english_mappings( $mappings ) { 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; return $mappings;
} }

View File

@ -355,7 +355,7 @@ class OnboardingTasks extends \WC_REST_Data_Controller {
* @return WP_REST_Response|WP_Error * @return WP_REST_Response|WP_Error
*/ */
public static function create_product_from_template( $request ) { 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 = __DIR__ . '/Templates/' . $template_name . '_product.csv';
$template_path = apply_filters( 'woocommerce_product_template_csv_file_path', $template_path, $template_name ); $template_path = apply_filters( 'woocommerce_product_template_csv_file_path', $template_path, $template_name );

View File

@ -135,6 +135,32 @@ class WC_Admin_Tests_API_Onboarding_Tasks extends WC_REST_Unit_Test_Case {
$this->assertEquals( 'simple', $product->get_type() ); $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. * Test that we get an error when template_name does not exist.
*/ */