From a2cfc2f1f5d0a9e4ce1863022572f932d6537f69 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Wed, 7 Sep 2022 16:28:44 +0800 Subject: [PATCH] Update get_product_data to send request with a locale query param for translation (#34461) * Update get_product_data to send request with a locale query param for translation * Add changelog * Fix lint * Update PRODUCT_DATA_TRANSIENT format for translations --- .../changelog/update-get-product-data-i18n | 4 ++++ .../Admin/Onboarding/OnboardingProducts.php | 20 +++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 plugins/woocommerce/changelog/update-get-product-data-i18n diff --git a/plugins/woocommerce/changelog/update-get-product-data-i18n b/plugins/woocommerce/changelog/update-get-product-data-i18n new file mode 100644 index 00000000000..16ffc40bd71 --- /dev/null +++ b/plugins/woocommerce/changelog/update-get-product-data-i18n @@ -0,0 +1,4 @@ +Significance: patch +Type: update + +Update get_product_data to send request with a locale query param for translation diff --git a/plugins/woocommerce/src/Internal/Admin/Onboarding/OnboardingProducts.php b/plugins/woocommerce/src/Internal/Admin/Onboarding/OnboardingProducts.php index ec531489579..fae3f970ffd 100644 --- a/plugins/woocommerce/src/Internal/Admin/Onboarding/OnboardingProducts.php +++ b/plugins/woocommerce/src/Internal/Admin/Onboarding/OnboardingProducts.php @@ -78,19 +78,27 @@ class OnboardingProducts { * @return array */ public static function get_product_data( $product_types ) { - $woocommerce_products = get_transient( self::PRODUCT_DATA_TRANSIENT ); + $locale = get_user_locale(); + // Transient value is an array of product data keyed by locale. + $transient_value = get_transient( self::PRODUCT_DATA_TRANSIENT ); + $transient_value = is_array( $transient_value ) ? $transient_value : array(); + $woocommerce_products = $transient_value[ $locale ] ?? false; + if ( false === $woocommerce_products ) { $woocommerce_products = wp_remote_get( - 'https://woocommerce.com/wp-json/wccom-extensions/1.0/search', - array( - 'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ), + add_query_arg( + array( + 'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ), + 'locale' => $locale, + ), + 'https://woocommerce.com/wp-json/wccom-extensions/1.0/search' ) ); if ( is_wp_error( $woocommerce_products ) ) { return $product_types; } - - set_transient( self::PRODUCT_DATA_TRANSIENT, $woocommerce_products, DAY_IN_SECONDS ); + $transient_value[ $locale ] = $woocommerce_products; + set_transient( self::PRODUCT_DATA_TRANSIENT, $transient_value, DAY_IN_SECONDS ); } $data = json_decode( $woocommerce_products['body'] );