UNPKG

1.16 kBJavaScriptView Raw
1import { useContext } from 'react';
2import ProductConfigurationContext from '../components/ProductConfigurationProvider/ProductConfigurationContext';
3/** Will return either the configuration, variation, or base product price */
4export function useDynamicPrice(product, variant) {
5 const configurationContext = useContext(ProductConfigurationContext);
6 // If we have a configuration, return the price.
7 // Products with configurations cannot have variants.
8 if (configurationContext === null || configurationContext === void 0 ? void 0 : configurationContext.configurationPrice) {
9 return configurationContext.configurationPrice;
10 }
11 // If a variant is selected, return the price of the variant
12 if (variant) {
13 const { price, previousPrice } = variant;
14 return {
15 price,
16 previousPrice
17 };
18 }
19 // Otherwise, just return the base product price
20 return {
21 price: product === null || product === void 0 ? void 0 : product.price,
22 previousPrice: product === null || product === void 0 ? void 0 : product.previousPrice
23 };
24}
25//# sourceMappingURL=useDynamicPrice.js.map
\No newline at end of file