1 | import get from 'lodash.get';
|
2 | import { useCountrySettings } from '../components/ChannelContext/useCountrySettings';
|
3 | import freeShippingQuery from './FreeShippingQuery.gql';
|
4 | export default function useFreeShippingCheck({ cartTotal } = { cartTotal: null }) {
|
5 | const { selectedCountrySettings, loading } = useCountrySettings({
|
6 | countrySettingsQuery: freeShippingQuery
|
7 | });
|
8 | const limit = get(selectedCountrySettings, 'shipping.freeShippingLimit');
|
9 | const shippingIsInQuery = typeof limit !== 'undefined';
|
10 | const noFreeShippingConfigured = !limit && shippingIsInQuery;
|
11 | if (noFreeShippingConfigured || !shippingIsInQuery) {
|
12 | return {
|
13 | loading,
|
14 | freeShippingLimit: limit,
|
15 | freeShippingConfigured: false,
|
16 | hasMetLimit: false,
|
17 | untilLimit: null
|
18 | };
|
19 | }
|
20 | return {
|
21 | loading,
|
22 | freeShippingLimit: limit,
|
23 | freeShippingConfigured: true,
|
24 | hasMetLimit: limit.incVat <= cartTotal.incVat,
|
25 | untilLimit: {
|
26 | incVat: limit.incVat - cartTotal.incVat,
|
27 | exVat: limit.exVat - cartTotal.exVat
|
28 | }
|
29 | };
|
30 | }
|
31 |
|
\ | No newline at end of file |