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
This commit is contained in:
louwie17 2023-12-21 14:09:00 -04:00 committed by GitHub
parent a67a1c6efc
commit 9182efaaf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 26 deletions

View File

@ -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 );
};

View File

@ -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.