getProduct method

Future<Product> getProduct(
  1. String sku
)

Get detailed product information by SKU.

Retrieves comprehensive product details including attributes, media, options, and pricing information for a specific product.

sku the product's SKU (Stock Keeping Unit)

Returns a Product object with detailed product information. Throws an exception if the product is not found.

Implementation

Future<Product> getProduct(String sku) async {
  try {
    final response = await _client.guestRequest<Map<String, dynamic>>(
      '/rest/V1/products/$sku',
      queryParameters: {
        'fields':
            'id,sku,name,price,special_price,status,visibility,stock_status,stock_quantity,description,short_description,meta_title,meta_description,meta_keyword,categories,websites,custom_attributes,media_gallery_entries,options,product_links,tier_prices,reviews,created_at,updated_at',
      },
    );

    if (response.statusCode == 200) {
      return Product.fromJson(response.data!);
    } else {
      throw Exception('Failed to get product: ${response.statusMessage}');
    }
  } on DioException catch (e) {
    if (e.response?.statusCode == 404) {
      throw Exception('Product not found: $sku');
    }
    throw Exception('Failed to get product: ${e.message}');
  } catch (e) {
    throw Exception('Failed to get product: $e');
  }
}