Only add product data on REST requests and task list (https://github.com/woocommerce/woocommerce-admin/pull/8235)

* Only add product data on REST requests and task list

* Add changelog entry

* Return new product data variable
This commit is contained in:
Joshua T Flowers 2022-02-04 10:03:47 -05:00 committed by GitHub
parent ec3852160d
commit e8c1c94b01
4 changed files with 25 additions and 19 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: Fix
Only add product data on REST requests and task list #8235

View File

@ -72,7 +72,7 @@ class OnboardingProductTypes extends \WC_REST_Data_Controller {
* @return \WP_Error|\WP_REST_Response
*/
public function get_product_types( $request ) {
return Onboarding::get_allowed_product_types();
return Onboarding::get_product_data( Onboarding::get_allowed_product_types() );
}
}

View File

@ -508,9 +508,8 @@ class Onboarding {
if ( ! Features::is_enabled( 'subscriptions' ) || 'US' !== $base_location['country'] ) {
$products['subscriptions']['product'] = 27147;
}
$product_types = self::append_product_data( $products );
return apply_filters( 'woocommerce_admin_onboarding_product_types', $product_types );
return apply_filters( 'woocommerce_admin_onboarding_product_types', $products );
}
/**
@ -633,12 +632,12 @@ class Onboarding {
}
/**
* Append dynamic product data from API.
* Get dynamic product data from API.
*
* @param array $product_types Array of product types.
* @return array
*/
public static function append_product_data( $product_types ) {
public static function get_product_data( $product_types ) {
$woocommerce_products = get_transient( self::PRODUCT_DATA_TRANSIENT );
if ( false === $woocommerce_products ) {
$woocommerce_products = wp_remote_get( 'https://woocommerce.com/wp-json/wccom-extensions/1.0/search' );
@ -649,12 +648,13 @@ class Onboarding {
set_transient( self::PRODUCT_DATA_TRANSIENT, $woocommerce_products, DAY_IN_SECONDS );
}
$product_data = json_decode( $woocommerce_products['body'] );
$data = json_decode( $woocommerce_products['body'] );
$products = array();
$product_data = array();
// Map product data by ID.
if ( isset( $product_data ) && isset( $product_data->products ) ) {
foreach ( $product_data->products as $product_datum ) {
if ( isset( $data ) && isset( $data->products ) ) {
foreach ( $data->products as $product_datum ) {
if ( isset( $product_datum->id ) ) {
$products[ $product_datum->id ] = $product_datum;
}
@ -663,21 +663,23 @@ class Onboarding {
// Loop over product types and append data.
foreach ( $product_types as $key => $product_type ) {
$product_data[ $key ] = $product_types[ $key ];
if ( isset( $product_type['product'] ) && isset( $products[ $product_type['product'] ] ) ) {
$price = html_entity_decode( $products[ $product_type['product'] ]->price );
$yearly_price = (float) str_replace( '$', '', $price );
$product_types[ $key ]['yearly_price'] = $yearly_price;
$product_types[ $key ]['description'] = $products[ $product_type['product'] ]->excerpt;
$product_types[ $key ]['more_url'] = $products[ $product_type['product'] ]->link;
$product_types[ $key ]['slug'] = strtolower( preg_replace( '~[^\pL\d]+~u', '-', $products[ $product_type['product'] ]->slug ) );
$product_data[ $key ]['yearly_price'] = $yearly_price;
$product_data[ $key ]['description'] = $products[ $product_type['product'] ]->excerpt;
$product_data[ $key ]['more_url'] = $products[ $product_type['product'] ]->link;
$product_data[ $key ]['slug'] = strtolower( preg_replace( '~[^\pL\d]+~u', '-', $products[ $product_type['product'] ]->slug ) );
} elseif ( isset( $product_type['product'] ) ) {
/* translators: site currency symbol (used to show that the product costs money) */
$product_types[ $key ]['label'] .= sprintf( __( ' — %s', 'woocommerce-admin' ), html_entity_decode( get_woocommerce_currency_symbol() ) );
$product_data[ $key ]['label'] .= sprintf( __( ' — %s', 'woocommerce-admin' ), html_entity_decode( get_woocommerce_currency_symbol() ) );
}
}
return $product_types;
return $product_data;
}
/**

View File

@ -154,18 +154,18 @@ class Purchase extends Task {
$profiler_data = get_option( Onboarding::PROFILE_DATA_OPTION, array() );
$installed = PluginsHelper::get_installed_plugin_slugs();
$product_types = isset( $profiler_data['product_types'] ) ? $profiler_data['product_types'] : array();
$allowed = Onboarding::get_allowed_product_types();
$product_data = Onboarding::get_product_data( Onboarding::get_allowed_product_types() );
$purchaseable = array();
$remaining = array();
foreach ( $product_types as $type ) {
if ( ! isset( $allowed[ $type ]['slug'] ) ) {
if ( ! isset( $product_data[ $type ]['slug'] ) ) {
continue;
}
$purchaseable[] = $allowed[ $type ];
$purchaseable[] = $product_data[ $type ];
if ( ! in_array( $allowed[ $type ]['slug'], $installed, true ) ) {
$remaining[] = $allowed[ $type ]['label'];
if ( ! in_array( $product_data[ $type ]['slug'], $installed, true ) ) {
$remaining[] = $product_data[ $type ]['label'];
}
}