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
This commit is contained in:
Chi-Hsuan Huang 2022-09-07 16:28:44 +08:00 committed by GitHub
parent 7c679dd754
commit a2cfc2f1f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 6 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: update
Update get_product_data to send request with a locale query param for translation

View File

@ -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',
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'] );