2022-12-14 13:00:07 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { select } from '@wordpress/data';
|
2023-02-03 16:00:24 +00:00
|
|
|
import { hasCollectableRate } from '@woocommerce/base-utils';
|
|
|
|
import { isString, objectHasProp } from '@woocommerce/types';
|
2022-12-14 13:00:07 +00:00
|
|
|
|
2022-06-10 16:33:15 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { STATUS } from './constants';
|
|
|
|
import { CheckoutState } from './default-state';
|
2022-12-14 13:00:07 +00:00
|
|
|
import { STORE_KEY as cartStoreKey } from '../cart/constants';
|
2022-06-10 16:33:15 +00:00
|
|
|
|
|
|
|
export const getCustomerId = ( state: CheckoutState ) => {
|
|
|
|
return state.customerId;
|
|
|
|
};
|
|
|
|
|
2022-11-24 14:19:59 +00:00
|
|
|
export const getOrderId = ( state: CheckoutState ) => {
|
|
|
|
return state.orderId;
|
|
|
|
};
|
|
|
|
|
2022-06-10 16:33:15 +00:00
|
|
|
export const getOrderNotes = ( state: CheckoutState ) => {
|
|
|
|
return state.orderNotes;
|
|
|
|
};
|
|
|
|
|
2022-11-24 14:19:59 +00:00
|
|
|
export const getRedirectUrl = ( state: CheckoutState ) => {
|
|
|
|
return state.redirectUrl;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getUseShippingAsBilling = ( state: CheckoutState ) => {
|
|
|
|
return state.useShippingAsBilling;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getExtensionData = ( state: CheckoutState ) => {
|
|
|
|
return state.extensionData;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getShouldCreateAccount = ( state: CheckoutState ) => {
|
|
|
|
return state.shouldCreateAccount;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getCheckoutStatus = ( state: CheckoutState ) => {
|
|
|
|
return state.status;
|
|
|
|
};
|
|
|
|
|
2022-06-10 16:33:15 +00:00
|
|
|
export const hasError = ( state: CheckoutState ) => {
|
|
|
|
return state.hasError;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const hasOrder = ( state: CheckoutState ) => {
|
|
|
|
return !! state.orderId;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isComplete = ( state: CheckoutState ) => {
|
|
|
|
return state.status === STATUS.COMPLETE;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isIdle = ( state: CheckoutState ) => {
|
|
|
|
return state.status === STATUS.IDLE;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isBeforeProcessing = ( state: CheckoutState ) => {
|
|
|
|
return state.status === STATUS.BEFORE_PROCESSING;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isAfterProcessing = ( state: CheckoutState ) => {
|
|
|
|
return state.status === STATUS.AFTER_PROCESSING;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isProcessing = ( state: CheckoutState ) => {
|
|
|
|
return state.status === STATUS.PROCESSING;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isCalculating = ( state: CheckoutState ) => {
|
|
|
|
return state.calculatingCount > 0;
|
|
|
|
};
|
2022-10-04 12:02:28 +00:00
|
|
|
|
|
|
|
export const prefersCollection = ( state: CheckoutState ) => {
|
2023-04-06 11:56:47 +00:00
|
|
|
if ( typeof state.prefersCollection === 'undefined' ) {
|
2022-12-14 13:00:07 +00:00
|
|
|
const shippingRates = select( cartStoreKey ).getShippingRates();
|
2022-12-15 15:26:14 +00:00
|
|
|
if ( ! shippingRates || ! shippingRates.length ) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-12-14 13:00:07 +00:00
|
|
|
const selectedRate = shippingRates[ 0 ].shipping_rates.find(
|
|
|
|
( rate ) => rate.selected
|
|
|
|
);
|
2023-04-06 11:56:47 +00:00
|
|
|
|
2023-02-03 16:00:24 +00:00
|
|
|
if (
|
|
|
|
objectHasProp( selectedRate, 'method_id' ) &&
|
|
|
|
isString( selectedRate.method_id )
|
|
|
|
) {
|
|
|
|
return hasCollectableRate( selectedRate?.method_id );
|
|
|
|
}
|
2022-12-14 13:00:07 +00:00
|
|
|
}
|
2022-10-04 12:02:28 +00:00
|
|
|
return state.prefersCollection;
|
|
|
|
};
|