Invalidate variations upon generation and fix formatted name (#40905)

* Invalidate variations upon generation and fix formatted name

* Add aditional invalidates to keep cache up to date

* Update formatted name to always return attribute term name list
This commit is contained in:
louwie17 2023-10-25 09:45:58 -03:00 committed by GitHub
parent be46ab25ed
commit 3f7eb0ce31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 5 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: update
Update the variation name, by using the wc_get_formatted_variation.

View File

@ -167,6 +167,8 @@ export const VariationsTable = forwardRef<
batchUpdateProductVariations,
invalidateResolution,
} = useDispatch( EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME );
const { invalidateResolution: coreInvalidateResolution } =
useDispatch( 'core' );
const { generateProductVariations } = useProductVariationsHelper();
@ -265,6 +267,16 @@ export const VariationsTable = forwardRef<
invalidateResolution( 'getProductVariations', [
requestParams,
] );
coreInvalidateResolution( 'getEntityRecord', [
'postType',
'product',
productId,
] );
coreInvalidateResolution( 'getEntityRecord', [
'postType',
'product_variation',
variationId,
] );
} )
.finally( () => {
setIsUpdating( ( prevState ) => ( {
@ -333,11 +345,24 @@ export const VariationsTable = forwardRef<
delete: values.map( ( { id } ) => id ),
}
)
.then( ( response: VariationResponseProps ) =>
.then( ( response: VariationResponseProps ) => {
invalidateResolution( 'getProductVariations', [
requestParams,
] ).then( () => response )
)
] );
coreInvalidateResolution( 'getEntityRecord', [
'postType',
'product',
productId,
] );
values.forEach( ( { id: variationId } ) => {
coreInvalidateResolution( 'getEntityRecord', [
'postType',
'product_variation',
variationId,
] );
} );
return response;
} )
.then( ( response: VariationResponseProps ) => {
createSuccessNotice( getSnackbarText( response ) );
onVariationTableChange( 'delete' );

View File

@ -65,6 +65,8 @@ export function useProductVariationsHelper() {
generateProductVariations: _generateProductVariations,
invalidateResolutionForStoreSelector,
} = useDispatch( EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME );
const { invalidateResolution: coreInvalidateResolution } =
useDispatch( 'core' );
const [ isGenerating, setIsGenerating ] = useState( false );
@ -75,7 +77,7 @@ export function useProductVariationsHelper() {
) => {
setIsGenerating( true );
const { status: lastStatus } = await resolveSelect(
const { status: lastStatus, variations } = await resolveSelect(
'core'
).getEditedEntityRecord< Product >(
'postType',
@ -111,6 +113,20 @@ export function useProductVariationsHelper() {
invalidateResolutionForStoreSelector(
'getProductVariations'
);
if ( variations && variations.length > 0 ) {
for ( const variationId of variations ) {
coreInvalidateResolution( 'getEntityRecord', [
'postType',
'product_variation',
variationId,
] );
}
}
coreInvalidateResolution( 'getEntityRecord', [
'postType',
'product',
productId,
] );
return invalidateResolutionForStoreSelector(
'getProductVariationsTotalCount'
);

View File

@ -0,0 +1,4 @@
Significance: minor
Type: fix
Invalidate variation requests after new product variations are generated.

View File

@ -119,7 +119,7 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Product_Variations_V
'attributes' => $this->get_attributes( $object ),
'menu_order' => $object->get_menu_order(),
'meta_data' => $object->get_meta_data(),
'name' => preg_replace( '/' . preg_quote( $object->get_title() . ' - ', '/' ) . '/', '', $object->get_name(), 1 ),
'name' => wc_get_formatted_variation( $object, true, false, false ),
'parent_id' => $object->get_parent_id(),
);