From 1db98a78d9216ebb76fb3e633e3eaa53ad168d3b Mon Sep 17 00:00:00 2001 From: Joshua T Flowers Date: Mon, 23 Mar 2020 17:25:02 +0200 Subject: [PATCH] Onboarding: Create flat rate and free shipping methods from task list (https://github.com/woocommerce/woocommerce-admin/pull/3927) * Get flat rate and free shipping methods in Rates component * Update shipping methods by type * Disable all shipping methods except the one being updated * Update toggleEnabled to be toggleable for readability * Check if shipping method has cost before getting value --- .../task-list/tasks/shipping/index.js | 2 +- .../task-list/tasks/shipping/rates.js | 122 ++++++++++-------- 2 files changed, 69 insertions(+), 55 deletions(-) diff --git a/plugins/woocommerce-admin/client/dashboard/task-list/tasks/shipping/index.js b/plugins/woocommerce-admin/client/dashboard/task-list/tasks/shipping/index.js index bf09888ef71..acdf6d0b5f0 100644 --- a/plugins/woocommerce-admin/client/dashboard/task-list/tasks/shipping/index.js +++ b/plugins/woocommerce-admin/client/dashboard/task-list/tasks/shipping/index.js @@ -70,7 +70,7 @@ class Shipping extends Component { path: `/wc/v3/shipping/zones/${ zone.id }/methods`, } ); zone.name = __( 'Rest of the world', 'woocommerce-admin' ); - zone.toggleEnabled = true; + zone.toggleable = true; shippingZones.push( zone ); return; } diff --git a/plugins/woocommerce-admin/client/dashboard/task-list/tasks/shipping/rates.js b/plugins/woocommerce-admin/client/dashboard/task-list/tasks/shipping/rates.js index 7a8a29ff902..6785de3c539 100644 --- a/plugins/woocommerce-admin/client/dashboard/task-list/tasks/shipping/rates.js +++ b/plugins/woocommerce-admin/client/dashboard/task-list/tasks/shipping/rates.js @@ -32,15 +32,41 @@ class ShippingRates extends Component { this.updateShippingZones = this.updateShippingZones.bind( this ); } + getShippingMethods( zone, type = null ) { + if ( ! type ) { + return zone.methods; + } + + return zone.methods + ? zone.methods.filter( ( method ) => method.method_id === type ) + : []; + } + + disableShippingMethods( zone, methods ) { + if ( ! methods.length ) { + return; + } + + methods.forEach( ( method ) => { + apiFetch( { + method: 'POST', + path: `/wc/v3/shipping/zones/${ zone.id }/methods/${ method.instance_id }`, + data: { + enabled: false, + }, + } ); + } ); + } + async updateShippingZones( values ) { const { createNotice, shippingZones } = this.props; let restOfTheWorld = false; let shippingCost = false; - shippingZones.forEach( zone => { + shippingZones.forEach( ( zone ) => { if ( zone.id === 0 ) { restOfTheWorld = - zone.toggleEnabled && values[ `${ zone.id }_enabled` ]; + zone.toggleable && values[ `${ zone.id }_enabled` ]; } else { shippingCost = values[ `${ zone.id }_rate` ] !== '' && @@ -48,49 +74,41 @@ class ShippingRates extends Component { parseFloat( 0 ); } - const flatRateMethods = zone.methods - ? zone.methods.filter( - ( method ) => method.method_id === 'flat_rate' - ) - : []; + const shippingMethods = this.getShippingMethods( zone ); + const methodType = + parseFloat( values[ `${ zone.id }_rate` ] ) === parseFloat( 0 ) + ? 'free_shipping' + : 'flat_rate'; + const shippingMethod = this.getShippingMethods( zone, methodType ) + .length + ? this.getShippingMethods( zone, methodType )[ 0 ] + : null; - if ( zone.toggleEnabled && ! values[ `${ zone.id }_enabled` ] ) { - // Disable any flat rate methods that exist if toggled off. - if ( flatRateMethods.length ) { - flatRateMethods.forEach( method => { - apiFetch( { - method: 'POST', - path: `/wc/v3/shipping/zones/${ zone.id }/methods/${ method.instance_id }`, - data: { - enabled: false, - }, - } ); - } ); - } + if ( zone.toggleable && ! values[ `${ zone.id }_enabled` ] ) { + // Disable any shipping methods that exist if toggled off. + this.disableShippingMethods( zone, shippingMethods ); return; + } else if ( shippingMethod ) { + // Disable all methods except the one being updated. + const methodsToDisable = shippingMethods.filter( + ( method ) => + method.instance_id !== shippingMethod.instance_id + ); + this.disableShippingMethods( zone, methodsToDisable ); } - if ( flatRateMethods.length ) { - // Update the existing method. - apiFetch( { - method: 'POST', - path: `/wc/v3/shipping/zones/${ zone.id }/methods/${ flatRateMethods[ 0 ].instance_id }`, - data: { - enabled: true, - settings: { cost: values[ `${ zone.id }_rate` ] }, - }, - } ); - } else { - // Add new method if one doesn't exist. - apiFetch( { - method: 'POST', - path: `/wc/v3/shipping/zones/${ zone.id }/methods`, - data: { - method_id: 'flat_rate', - settings: { cost: values[ `${ zone.id }_rate` ] }, - }, - } ); - } + apiFetch( { + method: 'POST', + path: shippingMethod + ? // Update the first existing method if one exists, otherwise create a new one. + `/wc/v3/shipping/zones/${ zone.id }/methods/${ shippingMethod.instance_id }` + : `/wc/v3/shipping/zones/${ zone.id }/methods`, + data: { + method_id: methodType, + enabled: true, + settings: { cost: values[ `${ zone.id }_rate` ] }, + }, + } ); } ); recordEvent( 'tasklist_shipping_set_costs', { @@ -153,20 +171,16 @@ class ShippingRates extends Component { const values = {}; this.props.shippingZones.forEach( ( zone ) => { - const flatRateMethods = - zone.methods && zone.methods.length - ? zone.methods.filter( - ( method ) => method.method_id === 'flat_rate' + const shippingMethods = this.getShippingMethods( zone ); + const rate = + shippingMethods.length && shippingMethods[ 0 ].settings.cost + ? this.getFormattedRate( + shippingMethods[ 0 ].settings.cost.value ) - : []; - const rate = flatRateMethods.length - ? this.getFormattedRate( - flatRateMethods[ 0 ].settings.cost.value - ) - : getCurrencyFormatString( 0 ); + : getCurrencyFormatString( 0 ); values[ `${ zone.id }_rate` ] = rate; - if ( flatRateMethods.length && flatRateMethods[ 0 ].enabled ) { + if ( shippingMethods.length && shippingMethods[ 0 ].enabled ) { values[ `${ zone.id }_enabled` ] = true; } else { values[ `${ zone.id }_enabled` ] = false; @@ -248,7 +262,7 @@ class ShippingRates extends Component {
{ zone.name } - { zone.toggleEnabled && ( + { zone.toggleable && ( ) }
- { ( ! zone.toggleEnabled || + { ( ! zone.toggleable || values[ `${ zone.id }_enabled` ] ) && (