WC_Plugin_Api_Updater::override_products_api_response
Override the products API to fetch data from the Helper API if it's a Woo product.
Method of the class: WC_Plugin_Api_Updater{}
No Hooks.
Returns
null. Nothing (null).
Usage
$result = WC_Plugin_Api_Updater::override_products_api_response( $response, $action, $args );
- $response(object) (required)
- The response core needs to display the modal.
- $action(string) (required)
- The requested action.
- $args(object) (required)
- Arguments passed to the API.
WC_Plugin_Api_Updater::override_products_api_response() WC Plugin Api Updater::override products api response code WC 10.3.5
public static function override_products_api_response( $response, $action, $args ) {
if ( empty( $args->slug ) ) {
return $response;
}
// Only for slugs that start with woocommerce-com-.
if ( 0 !== strpos( $args->slug, 'woocommerce-com-' ) ) {
return $response;
}
$clean_slug = str_replace( 'woocommerce-com-', '', $args->slug );
// Look through update data by slug.
$update_data = WC_Helper_Updater::get_update_data();
$products = wp_list_filter( $update_data, array( 'slug' => $clean_slug ) );
if ( empty( $products ) ) {
return $response;
}
$product_id = array_keys( $products );
$product_id = array_shift( $product_id );
$is_site_connected = WC_Helper::is_site_connected();
$endpoint = add_query_arg(
array( 'product_id' => absint( $product_id ) ),
'info'
);
// Fetch the product information from the Helper API.
$request = WC_Helper_API::get(
$endpoint,
array( 'authenticated' => $is_site_connected )
);
// If we tried to authenticate and failed, try again without authentication.
if ( is_wp_error( $request ) && $is_site_connected ) {
$request = WC_Helper_API::get( $endpoint );
}
$results = json_decode( wp_remote_retrieve_body( $request ), true );
if ( ! empty( $results ) ) {
$response = (object) $results;
}
return $response;
}