2020-02-25 11:36:53 +00:00
|
|
|
/** @typedef { import('@woocommerce/type-defs/hooks').StoreCart } StoreCart */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
|
|
|
|
import { useSelect } from '@wordpress/data';
|
2020-03-13 13:41:59 +00:00
|
|
|
import { useEditorContext } from '@woocommerce/base-context';
|
|
|
|
import { previewCart } from '@woocommerce/resource-previews';
|
2020-02-25 11:36:53 +00:00
|
|
|
|
2020-02-18 23:06:37 +00:00
|
|
|
/**
|
2020-02-25 11:36:53 +00:00
|
|
|
* @constant
|
|
|
|
* @type {StoreCart} Object containing cart data.
|
2020-02-18 23:06:37 +00:00
|
|
|
*/
|
2020-02-25 11:36:53 +00:00
|
|
|
const defaultCartData = {
|
|
|
|
cartCoupons: [],
|
|
|
|
cartItems: [],
|
|
|
|
cartItemsCount: 0,
|
|
|
|
cartItemsWeight: 0,
|
|
|
|
cartNeedsShipping: true,
|
|
|
|
cartTotals: {},
|
|
|
|
cartIsLoading: true,
|
|
|
|
cartErrors: [],
|
2020-03-13 19:04:03 +00:00
|
|
|
shippingAddress: {
|
|
|
|
first_name: '',
|
|
|
|
last_name: '',
|
|
|
|
company: '',
|
|
|
|
address_1: '',
|
|
|
|
address_2: '',
|
|
|
|
city: '',
|
|
|
|
state: '',
|
|
|
|
postcode: '',
|
|
|
|
country: '',
|
|
|
|
},
|
|
|
|
shippingRates: [],
|
2020-02-25 11:36:53 +00:00
|
|
|
};
|
2020-02-18 23:06:37 +00:00
|
|
|
|
|
|
|
/**
|
2020-02-25 11:36:53 +00:00
|
|
|
* This is a custom hook that is wired up to the `wc/store/cart` data
|
|
|
|
* store.
|
|
|
|
*
|
|
|
|
* @param {Object} options An object declaring the various
|
|
|
|
* collection arguments.
|
|
|
|
* @param {boolean} options.shouldSelect If false, the previous results will be
|
|
|
|
* returned and internal selects will not
|
|
|
|
* fire.
|
2020-02-18 23:06:37 +00:00
|
|
|
*
|
2020-02-25 11:36:53 +00:00
|
|
|
* @return {StoreCart} Object containing cart data.
|
2020-02-18 23:06:37 +00:00
|
|
|
*/
|
2020-02-25 11:36:53 +00:00
|
|
|
export const useStoreCart = ( options = { shouldSelect: true } ) => {
|
2020-03-13 13:41:59 +00:00
|
|
|
const { isEditor } = useEditorContext();
|
2020-02-25 11:36:53 +00:00
|
|
|
const { shouldSelect } = options;
|
|
|
|
|
|
|
|
const results = useSelect(
|
|
|
|
( select ) => {
|
|
|
|
if ( ! shouldSelect ) {
|
2020-03-13 19:04:03 +00:00
|
|
|
return defaultCartData;
|
2020-02-25 11:36:53 +00:00
|
|
|
}
|
2020-03-13 13:41:59 +00:00
|
|
|
|
|
|
|
if ( isEditor ) {
|
|
|
|
return {
|
|
|
|
cartCoupons: previewCart.coupons,
|
|
|
|
cartItems: previewCart.items,
|
|
|
|
cartItemsCount: previewCart.items_count,
|
|
|
|
cartItemsWeight: previewCart.items_weight,
|
|
|
|
cartNeedsShipping: previewCart.needs_shipping,
|
|
|
|
cartTotals: previewCart.totals,
|
|
|
|
cartIsLoading: false,
|
|
|
|
cartErrors: [],
|
2020-03-13 20:05:45 +00:00
|
|
|
shippingRates: previewCart.shipping_rates,
|
|
|
|
shippingAddress: {
|
|
|
|
country: '',
|
|
|
|
state: '',
|
|
|
|
city: '',
|
|
|
|
postcode: '',
|
|
|
|
},
|
2020-03-13 13:41:59 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-25 11:36:53 +00:00
|
|
|
const store = select( storeKey );
|
|
|
|
const cartData = store.getCartData();
|
|
|
|
const cartErrors = store.getCartErrors();
|
|
|
|
const cartTotals = store.getCartTotals();
|
|
|
|
const cartIsLoading = ! store.hasFinishedResolution(
|
|
|
|
'getCartData'
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
cartCoupons: cartData.coupons,
|
2020-03-05 19:54:05 +00:00
|
|
|
shippingRates: cartData.shippingRates,
|
2020-03-13 19:04:03 +00:00
|
|
|
shippingAddress: cartData.shippingAddress,
|
2020-02-25 11:36:53 +00:00
|
|
|
cartItems: cartData.items,
|
|
|
|
cartItemsCount: cartData.itemsCount,
|
|
|
|
cartItemsWeight: cartData.itemsWeight,
|
|
|
|
cartNeedsShipping: cartData.needsShipping,
|
|
|
|
cartTotals,
|
|
|
|
cartIsLoading,
|
|
|
|
cartErrors,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
[ shouldSelect ]
|
|
|
|
);
|
|
|
|
return results;
|
2020-02-18 23:06:37 +00:00
|
|
|
};
|