From 9182efaaf6a7004c2bd5e7f9247c1151919fbc92 Mon Sep 17 00:00:00 2001 From: louwie17 Date: Thu, 21 Dec 2023 14:09:00 -0400 Subject: [PATCH] Fix product task redirect to support grouped and external products (#43051) * Rearrange product redirection logic to better accept grouped and external produc types * Add changelog --- .../products/use-create-product-by-type.ts | 75 ++++++++++++------- .../changelog/fix-product-task-redirect | 4 + 2 files changed, 53 insertions(+), 26 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-product-task-redirect diff --git a/plugins/woocommerce-admin/client/task-lists/fills/products/use-create-product-by-type.ts b/plugins/woocommerce-admin/client/task-lists/fills/products/use-create-product-by-type.ts index 82b46924f7a..47745d6f5d6 100644 --- a/plugins/woocommerce-admin/client/task-lists/fills/products/use-create-product-by-type.ts +++ b/plugins/woocommerce-admin/client/task-lists/fills/products/use-create-product-by-type.ts @@ -24,32 +24,19 @@ export const useCreateProductByType = () => { const isNewExperienceEnabled = window.wcAdminFeatures[ 'new-product-management-experience' ]; - const createProductByType = async ( type: ProductTypeKey ) => { - setIsRequesting( true ); - + const getProductEditPageLink = async ( + type: ProductTypeKey, + classicEditor: boolean + ) => { if ( type === 'physical' || type === 'variable' || - type === 'digital' || - type === 'grouped' || - type === 'external' + type === 'digital' ) { - if ( isNewExperienceEnabled ) { - navigateTo( { url: getNewPath( {}, '/add-product', {} ) } ); - return; - } - const assignment = await loadExperimentAssignment( - EXPERIMENT_NAME - ); - if ( assignment.variationName === 'treatment' ) { - const _feature_nonce = getAdminSetting( '_feature_nonce' ); - window.location.href = getAdminLink( - `post-new.php?post_type=product&product_block_editor=1&_feature_nonce=${ _feature_nonce }` - ); - return; - } + return classicEditor + ? getAdminLink( 'post-new.php?post_type=product' ) + : getNewPath( {}, '/add-product', {} ); } - try { const data: { id?: number; @@ -61,16 +48,52 @@ export const useCreateProductByType = () => { { _fields: [ 'id' ] } ); if ( data && data.id ) { - const link = getAdminLink( - `post.php?post=${ data.id }&action=edit&wc_onboarding_active_task=products&tutorial=true` - ); - window.location.href = link; - return; + return classicEditor + ? getAdminLink( + `post.php?post=${ data.id }&action=edit&wc_onboarding_active_task=products&tutorial=true` + ) + : getNewPath( {}, '/product/' + data.id, {} ); } throw new Error( 'Unexpected empty data response from server' ); } catch ( error ) { createNoticesFromResponse( error ); } + }; + + const createProductByType = async ( type: ProductTypeKey ) => { + setIsRequesting( true ); + + if ( + type === 'physical' || + type === 'variable' || + type === 'digital' || + type === 'grouped' || + type === 'external' + ) { + if ( isNewExperienceEnabled ) { + const url = await getProductEditPageLink( type, false ); + if ( url ) { + navigateTo( { url } ); + } + return; + } + const assignment = await loadExperimentAssignment( + EXPERIMENT_NAME + ); + if ( assignment.variationName === 'treatment' ) { + const url = await getProductEditPageLink( type, true ); + const _feature_nonce = getAdminSetting( '_feature_nonce' ); + window.location.href = + url + + `&product_block_editor=1&_feature_nonce=${ _feature_nonce }`; + return; + } + } + + const url = await getProductEditPageLink( type, true ); + if ( url ) { + navigateTo( { url } ); + } setIsRequesting( false ); }; diff --git a/plugins/woocommerce/changelog/fix-product-task-redirect b/plugins/woocommerce/changelog/fix-product-task-redirect new file mode 100644 index 00000000000..e461726935e --- /dev/null +++ b/plugins/woocommerce/changelog/fix-product-task-redirect @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Fix product task redirection to allow use for external and grouped products with the new product editor.