Example #1
0
export const getCurrentlyOpenShippingZoneMethod = (
	state,
	siteId = getSelectedSiteId( state )
) => {
	if ( ! areShippingZonesLoaded( state, siteId ) ) {
		return null;
	}
	const zone = getCurrentlyEditingShippingZone( state, siteId );
	if ( ! zone || ! zone.methods || ! zone.methods.currentlyEditingId ) {
		return null;
	}

	const { methodType } = zone.methods.currentlyEditingChanges;
	const defaultValues = startsWith( methodType, 'wc_services' )
		? getDefaultSettingsValues( getShippingMethodSchema( state, methodType, siteId ).formSchema )
		: {};

	if ( zone.methods.currentlyEditingNew ) {
		return merge( {}, defaultValues, zone.methods.currentlyEditingChanges, {
			enabled: false !== zone.methods.currentlyEditingChanges.enabled,
		} );
	}

	const methods = getCurrentlyEditingShippingZoneMethods( state );
	const openMethod = find( methods, { id: zone.methods.currentlyEditingId } );
	if ( ! openMethod ) {
		return null;
	}

	const enabled = isNil( zone.methods.currentlyEditingChanges.enabled )
		? false !== openMethod.enabled
		: false !== zone.methods.currentlyEditingChanges.enabled;

	return merge( {}, defaultValues, openMethod, zone.methods.currentlyEditingChanges, { enabled } );
};
Example #2
0
	const allMethods = [ ...methods, ...creates ].map( method => {
		let enabled = method.enabled;
		if ( isNil( enabled ) && 'number' === typeof method._originalId ) {
			// If the "enabled" prop hasn't been modified, use the value from the original method
			enabled = getShippingZoneMethod( state, method._originalId, siteId ).enabled;
		}

		const defaultValues = startsWith( method.methodType, 'wc_services' )
			? getDefaultSettingsValues(
					getShippingMethodSchema( state, method.methodType, siteId ).formSchema
				)
			: {};
		return merge( {}, defaultValues, method, { enabled: false !== enabled } );
	} );