Commit 56b05cab128 for woocommerce
commit 56b05cab128dd29b7bac22768d73c2b40f17d891
Author: Mike Jolley <mike.jolley@me.com>
Date: Thu Nov 26 14:41:58 2026 +0000
Speed up Cart and Checkout loading feedback: shorter skeletons that end together (#70884)
* End Cart and Checkout skeletons together after 300ms
Skeletons in one update share a start time and show for at least
300ms from it, instead of 2s each. The Cart express skeleton starts
with the totals, and the order summary items and summary bar total
change when the skeletons end.
* Update cart line totals while a quantity saves
Show unit price x quantity at once when the item's pricing is linear.
Other pricing, such as bulk discounts, waits for the server.
* Hold express payment skeletons for the shared minimum
The express payment skeleton used the loading flags directly, so it could
end before the totals skeletons and flash. It now uses useDelayedSkeleton,
and an active express payment method still keeps its buttons.
* End held skeletons when reduced motion turns on
Reduced motion has no minimum, so a skeleton held after loading ends
now ends at once instead of at the original deadline.
* Show a skeleton for a cart line total while its quantity saves
Only the server knows the price at a new quantity: bulk pricing plugins
change the unit price, so unit price x quantity can't be checked on the
client. The line total now shows a skeleton from the change until the
save ends, and ends with the totals skeletons. useStoreCartItemQuantity
exposes isPendingQuantity for it. getOptimisticLineTotal is removed.
* Keep a cart item pending until its latest quantity request ends
A request aborted by a newer one for the same item cleared the item's
pending flag while the newer request was still in flight.
diff --git a/plugins/woocommerce/changelog/update-cart-checkout-skeleton-timing b/plugins/woocommerce/changelog/update-cart-checkout-skeleton-timing
new file mode 100644
index 00000000000..8945876fd45
--- /dev/null
+++ b/plugins/woocommerce/changelog/update-cart-checkout-skeleton-timing
@@ -0,0 +1,4 @@
+Significance: patch
+Type: tweak
+
+Show loading placeholders in the Cart and Checkout blocks for less time, and on cart line totals while a quantity change saves.
diff --git a/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/cart-line-items-table/cart-line-item-row.tsx b/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/cart-line-items-table/cart-line-item-row.tsx
index 0c026d06329..61465320479 100644
--- a/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/cart-line-items-table/cart-line-item-row.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/cart-line-items-table/cart-line-item-row.tsx
@@ -7,6 +7,8 @@ import { decodeEntities } from '@wordpress/html-entities';
import { speak } from '@wordpress/a11y';
import QuantitySelector from '@woocommerce/base-components/quantity-selector';
import ProductPrice from '@woocommerce/base-components/product-price';
+import { Skeleton } from '@woocommerce/base-components/skeleton';
+import { DelayedContentWithSkeleton } from '@woocommerce/base-components/delayed-content-with-skeleton';
import ProductName from '@woocommerce/base-components/product-name';
import {
useStoreCartItemQuantity,
@@ -117,8 +119,13 @@ const CartLineItemRow: React.ForwardRefExoticComponent<
extensions,
} = lineItem;
- const { quantity, setItemQuantity, removeItem, isPendingDelete } =
- useStoreCartItemQuantity( lineItem );
+ const {
+ quantity,
+ setItemQuantity,
+ removeItem,
+ isPendingDelete,
+ isPendingQuantity,
+ } = useStoreCartItemQuantity( lineItem );
const { saveForLater, isSaving: isSavingForLater } = useSaveForLater();
const { dispatchStoreEvent } = useStoreEvents();
const isUserLoggedIn = !! getSetting< number >( 'currentUserId', 0 );
@@ -425,11 +432,34 @@ const CartLineItemRow: React.ForwardRefExoticComponent<
</td>
<td className="wc-block-cart-item__total">
<div className="wc-block-cart-item__total-price-and-sale-badge-wrapper">
- <ProductPrice
- currency={ totalsCurrency }
- format={ productPriceFormat }
- price={ toSnapshot( subtotalPrice ).amount }
- />
+ { /* Only the server knows the price at a new quantity, for example with bulk pricing. */ }
+ <DelayedContentWithSkeleton
+ isLoading={ isPendingQuantity }
+ skeleton={
+ // The span keeps the line height of the price, so the row does not move.
+ <span>
+ <Skeleton
+ tag="span"
+ className="wc-block-cart-item__total-skeleton"
+ width="45px"
+ height="1em"
+ ariaMessage={
+ // eslint-disable-next-line @wordpress/i18n-no-flanking-whitespace
+ __(
+ 'Loading price… ',
+ 'woocommerce'
+ )
+ }
+ />
+ </span>
+ }
+ >
+ <ProductPrice
+ currency={ totalsCurrency }
+ format={ productPriceFormat }
+ price={ toSnapshot( subtotalPrice ).amount }
+ />
+ </DelayedContentWithSkeleton>
<ProductSaleBadge
currency={ priceCurrency }
diff --git a/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/cart-line-items-table/style.scss b/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/cart-line-items-table/style.scss
index fa8a87b6815..4d0f5f7a0c3 100644
--- a/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/cart-line-items-table/style.scss
+++ b/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/cart-line-items-table/style.scss
@@ -123,6 +123,11 @@ table.wc-block-cart-items {
@include font-small-locked;
text-align: right;
line-height: 1.4;
+
+ .wc-block-cart-item__total-skeleton {
+ display: inline-block;
+ vertical-align: middle;
+ }
}
@include motion-dim(".is-disabled") {
diff --git a/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/cart-line-items-table/test/cart-line-item-row.tsx b/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/cart-line-items-table/test/cart-line-item-row.tsx
index ae787afc243..f50e1e52c28 100644
--- a/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/cart-line-items-table/test/cart-line-item-row.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/cart-line-items-table/test/cart-line-item-row.tsx
@@ -4,6 +4,7 @@
import { render, screen } from '@testing-library/react';
import type { CartItem } from '@woocommerce/types';
import { getSetting, getSettingWithCoercion } from '@woocommerce/settings';
+import { useStoreCartItemQuantity } from '@woocommerce/base-context/hooks';
/**
* Internal dependencies
@@ -245,3 +246,38 @@ describe( 'CartLineItemRow — Save for later visibility', () => {
).not.toBeInTheDocument();
} );
} );
+
+describe( 'CartLineItemRow — line total', () => {
+ const mockQuantity = ( isPendingQuantity: boolean ) =>
+ jest.mocked( useStoreCartItemQuantity ).mockReturnValue( {
+ quantity: 1,
+ setItemQuantity: jest.fn(),
+ removeItem: jest.fn(),
+ isPendingDelete: false,
+ isPendingQuantity,
+ cartItemQuantityErrors: [],
+ } );
+
+ beforeEach( () => {
+ setSettings( {} );
+ } );
+
+ it( 'shows the saved line total', () => {
+ mockQuantity( false );
+ const { container } = renderRow();
+
+ expect(
+ container.querySelector( '.wc-block-cart-item__total' )
+ ).toHaveTextContent( '$10.00' );
+ } );
+
+ it( 'shows a skeleton instead of the line total while a new quantity saves', () => {
+ mockQuantity( true );
+ const { container } = renderRow();
+
+ expect(
+ container.querySelector( '.wc-block-cart-item__total' )
+ ).not.toHaveTextContent( '$10.00' );
+ expect( screen.getByLabelText( /loading price/i ) ).toBeInTheDocument();
+ } );
+} );
diff --git a/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/order-summary/index.tsx b/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/order-summary/index.tsx
index 75d8fdc0553..d6a3765ab0d 100644
--- a/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/order-summary/index.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/order-summary/index.tsx
@@ -3,6 +3,7 @@
*/
import {
useContainerWidthContext,
+ useSettledOrderSummaryValue,
useStoreCart,
} from '@woocommerce/base-context';
import type { CartItem } from '@woocommerce/types';
@@ -31,14 +32,14 @@ const OrderSummary = ( {
const showSkeleton = cartIsLoading || hasPendingItemsOperations;
const contentRef = useRef< HTMLDivElement | null >( null );
const onItemsShow = useListEntrance( contentRef );
+ // The item prices change when the totals skeletons end, not before.
+ const items = useSettledOrderSummaryValue( cartItems );
return (
<DelayedContentWithSkeleton
isLoading={ showSkeleton }
skeleton={
- <CartLineItemsCheckoutSkeleton
- rows={ cartItems?.length || 2 }
- />
+ <CartLineItemsCheckoutSkeleton rows={ items?.length || 2 } />
}
onShow={ onItemsShow }
>
@@ -51,7 +52,7 @@ const OrderSummary = ( {
className="wc-block-components-order-summary__content"
ref={ contentRef }
>
- { cartItems.map( ( cartItem ) => {
+ { items.map( ( cartItem ) => {
return (
<OrderSummaryItem
disableProductDescriptions={
diff --git a/plugins/woocommerce/client/blocks/assets/js/base/components/delayed-content-with-skeleton/index.tsx b/plugins/woocommerce/client/blocks/assets/js/base/components/delayed-content-with-skeleton/index.tsx
index 4654dc4648e..7f39f0231c0 100644
--- a/plugins/woocommerce/client/blocks/assets/js/base/components/delayed-content-with-skeleton/index.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/base/components/delayed-content-with-skeleton/index.tsx
@@ -1,10 +1,18 @@
/**
* External dependencies
*/
-import { useState, useEffect, useLayoutEffect } from '@wordpress/element';
+import { useLayoutEffect, useState } from '@wordpress/element';
import { useReducedMotion } from '@wordpress/compose';
import type { ReactNode } from 'react';
+// Long enough that a skeleton does not flash, short enough that it does not hold the page.
+const MIN_DISPLAY_TIME = 300;
+
+declare global {
+ // eslint-disable-next-line no-var
+ var wcBlocksSkeletonPhaseStart: number | undefined;
+}
+
export interface DelayedContentWithSkeletonProps {
/**
* The component to render when loading is complete
@@ -28,51 +36,41 @@ export interface DelayedContentWithSkeletonProps {
}
/**
- * Whether to show a skeleton: while `isLoading`, and for a minimum duration
- * to prevent jarring quick flashes when content loads fast.
+ * Whether to show a skeleton: while `isLoading`, and until `MIN_DISPLAY_TIME` after the loading phase started. The
+ * start is shared, so skeletons that start during a phase end with the others. It is on
+ * `globalThis` because each script bundle has its own copy of this module. With reduced motion there is no minimum.
*/
export const useDelayedSkeleton = ( isLoading: boolean ): boolean => {
- const disableMotion = useReducedMotion();
- const [ showSkeleton, setShowSkeleton ] = useState( isLoading );
- const [ startTime, setStartTime ] = useState< number | null >( null );
+ const isReducedMotion = useReducedMotion();
+ const [ isHeld, setIsHeld ] = useState( false );
- useEffect( () => {
- // If motion is disabled, just sync with isLoading state
- if ( disableMotion ) {
- setShowSkeleton( isLoading );
+ // A layout effect, so a skeleton whose minimum has passed ends in the same frame as its loading.
+ useLayoutEffect( () => {
+ const now = Date.now();
+ const phaseStart = globalThis.wcBlocksSkeletonPhaseStart ?? 0;
+ if ( isLoading ) {
+ if ( now - phaseStart >= MIN_DISPLAY_TIME ) {
+ globalThis.wcBlocksSkeletonPhaseStart = now;
+ }
+ setIsHeld( ! isReducedMotion );
return;
}
-
- // For motion-enabled, use the delay logic
- const MIN_DISPLAY_TIME = 2000;
- let timer: ReturnType< typeof setTimeout >;
-
- if ( isLoading ) {
- setShowSkeleton( true );
- setStartTime( Date.now() );
- } else if ( startTime ) {
- const elapsed = Date.now() - startTime;
- const remainingTime = Math.max( 0, MIN_DISPLAY_TIME - elapsed );
- timer = setTimeout( () => {
- setShowSkeleton( false );
- setStartTime( null );
- }, remainingTime );
+ if ( isReducedMotion ) {
+ setIsHeld( false );
+ return;
}
+ const timer = setTimeout(
+ () => setIsHeld( false ),
+ Math.max( 0, phaseStart + MIN_DISPLAY_TIME - now )
+ );
+ return () => clearTimeout( timer );
+ }, [ isLoading, isReducedMotion ] );
- return () => {
- if ( timer ) {
- clearTimeout( timer );
- }
- };
- }, [ isLoading, startTime, disableMotion ] );
-
- // `isLoading` too, so the skeleton shows in the same render as loading starts, not one effect later.
- return isLoading || showSkeleton;
+ return isLoading || isHeld;
};
/**
- * Component that ensures skeleton children are displayed for a minimum duration
- * to prevent jarring quick flashes when content loads fast
+ * Shows a skeleton while content loads, and for a short minimum time so that it does not flash.
*/
export const DelayedContentWithSkeleton = ( {
children,
diff --git a/plugins/woocommerce/client/blocks/assets/js/base/components/delayed-content-with-skeleton/test/index.tsx b/plugins/woocommerce/client/blocks/assets/js/base/components/delayed-content-with-skeleton/test/index.tsx
new file mode 100644
index 00000000000..6a483a8a05c
--- /dev/null
+++ b/plugins/woocommerce/client/blocks/assets/js/base/components/delayed-content-with-skeleton/test/index.tsx
@@ -0,0 +1,72 @@
+/**
+ * External dependencies
+ */
+import { act, renderHook } from '@testing-library/react';
+import { useReducedMotion } from '@wordpress/compose';
+
+/**
+ * Internal dependencies
+ */
+import { useDelayedSkeleton } from '../index';
+
+jest.mock( '@wordpress/compose', () => ( {
+ ...jest.requireActual( '@wordpress/compose' ),
+ useReducedMotion: jest.fn(),
+} ) );
+
+const renderSkeleton = ( isLoading: boolean ) =>
+ renderHook( ( loading: boolean ) => useDelayedSkeleton( loading ), {
+ initialProps: isLoading,
+ } );
+
+describe( 'useDelayedSkeleton', () => {
+ beforeEach( () => {
+ jest.useFakeTimers();
+ jest.mocked( useReducedMotion ).mockReturnValue( false );
+ globalThis.wcBlocksSkeletonPhaseStart = undefined;
+ } );
+
+ afterEach( () => {
+ jest.useRealTimers();
+ } );
+
+ it( 'shows a skeleton for at least 300ms', () => {
+ const skeleton = renderSkeleton( true );
+ act( () => jest.advanceTimersByTime( 100 ) );
+ skeleton.rerender( false );
+
+ expect( skeleton.result.current ).toBe( true );
+ act( () => jest.advanceTimersByTime( 200 ) );
+ expect( skeleton.result.current ).toBe( false );
+ } );
+
+ it( 'ends a skeleton that starts later in the phase with the first one', () => {
+ const first = renderSkeleton( true );
+ act( () => jest.advanceTimersByTime( 200 ) );
+ const second = renderSkeleton( true );
+ first.rerender( false );
+ second.rerender( false );
+
+ act( () => jest.advanceTimersByTime( 100 ) );
+ expect( first.result.current ).toBe( false );
+ expect( second.result.current ).toBe( false );
+ } );
+
+ it( 'ends as soon as loading ends with reduced motion', () => {
+ jest.mocked( useReducedMotion ).mockReturnValue( true );
+ const skeleton = renderSkeleton( true );
+ skeleton.rerender( false );
+
+ expect( skeleton.result.current ).toBe( false );
+ } );
+
+ it( 'ends a held skeleton when reduced motion turns on', () => {
+ const skeleton = renderSkeleton( true );
+ skeleton.rerender( false );
+ expect( skeleton.result.current ).toBe( true );
+
+ jest.mocked( useReducedMotion ).mockReturnValue( true );
+ skeleton.rerender( false );
+ expect( skeleton.result.current ).toBe( false );
+ } );
+} );
diff --git a/plugins/woocommerce/client/blocks/assets/js/base/context/hooks/cart/test/use-store-cart-item-quantity.js b/plugins/woocommerce/client/blocks/assets/js/base/context/hooks/cart/test/use-store-cart-item-quantity.js
index b5af0ecfc6c..4845aef6cd4 100644
--- a/plugins/woocommerce/client/blocks/assets/js/base/context/hooks/cart/test/use-store-cart-item-quantity.js
+++ b/plugins/woocommerce/client/blocks/assets/js/base/context/hooks/cart/test/use-store-cart-item-quantity.js
@@ -145,6 +145,22 @@ describe( 'useStoreCartItemQuantity', () => {
[ '123', 2 ],
] );
} );
+
+ it( 'isPendingQuantity is true from a quantity change until the server saves it', () => {
+ const { result, rerender } = renderHook(
+ ( cartItem ) => useStoreCartItemQuantity( cartItem ),
+ { wrapper, initialProps: { key: '123', quantity: 1 } }
+ );
+ expect( result.current.isPendingQuantity ).toBe( false );
+
+ act( () => {
+ result.current.setItemQuantity( 2 );
+ } );
+ expect( result.current.isPendingQuantity ).toBe( true );
+
+ rerender( { key: '123', quantity: 2 } );
+ expect( result.current.isPendingQuantity ).toBe( false );
+ } );
} );
it( 'should expose store errors', () => {
diff --git a/plugins/woocommerce/client/blocks/assets/js/base/context/hooks/cart/use-store-cart-item-quantity.ts b/plugins/woocommerce/client/blocks/assets/js/base/context/hooks/cart/use-store-cart-item-quantity.ts
index 47965883296..e9542fdc316 100644
--- a/plugins/woocommerce/client/blocks/assets/js/base/context/hooks/cart/use-store-cart-item-quantity.ts
+++ b/plugins/woocommerce/client/blocks/assets/js/base/context/hooks/cart/use-store-cart-item-quantity.ts
@@ -178,6 +178,8 @@ export const useStoreCartItemQuantity = (
return {
isPendingDelete: isPending.delete,
+ // From the first change to the quantity until the server has saved it.
+ isPendingQuantity: quantity !== cartItemQuantity || isPending.quantity,
quantity,
setItemQuantity: setQuantity,
removeItem,
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/payment-methods/express-payment/cart-express-payment.js b/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/payment-methods/express-payment/cart-express-payment.js
index f922cc96434..e754235842c 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/payment-methods/express-payment/cart-express-payment.js
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/payment-methods/express-payment/cart-express-payment.js
@@ -5,8 +5,9 @@ import { __ } from '@wordpress/i18n';
import { noticeContexts, useStoreCart } from '@woocommerce/base-context';
import { StoreNoticesContainer } from '@woocommerce/blocks-components';
import { useSelect } from '@wordpress/data';
-import { paymentStore } from '@woocommerce/block-data';
+import { checkoutStore, paymentStore } from '@woocommerce/block-data';
import { Skeleton } from '@woocommerce/base-components/skeleton';
+import { useDelayedSkeleton } from '@woocommerce/base-components/delayed-content-with-skeleton';
import clsx from 'clsx';
/**
@@ -20,6 +21,7 @@ const CartExpressPayment = () => {
const {
availableExpressPaymentMethods = {},
expressPaymentMethodsInitialized,
+ isCalculating,
isExpressPaymentMethodActive,
registeredExpressPaymentMethods = {},
} = useSelect( ( select ) => {
@@ -29,6 +31,7 @@ const CartExpressPayment = () => {
payment.getAvailableExpressPaymentMethods(),
expressPaymentMethodsInitialized:
payment.expressPaymentMethodsInitialized(),
+ isCalculating: select( checkoutStore ).isCalculating(),
isExpressPaymentMethodActive:
payment.isExpressPaymentMethodActive(),
registeredExpressPaymentMethods:
@@ -51,11 +54,16 @@ const CartExpressPayment = () => {
// We show the skeleton when
// the express payment method is not active (because they trigger recalculations) and
// cart items are being added, updated, or deleted, because it can result in different express payment methods
- // or when the express payment methods are not initialized
- const showSkeleton =
- ! isExpressPaymentMethodActive &&
- ( hasPendingItemsOperations ||
- hasRegisteredNotInitializedExpressPaymentMethods );
+ // or when the express payment methods are not initialized.
+ // `isCalculating` starts the skeleton when the quantity changes, with the totals skeletons, instead of when the
+ // request starts after the quantity input's delay. The skeleton ends with the totals skeletons.
+ const isLoading = useDelayedSkeleton(
+ isCalculating ||
+ hasPendingItemsOperations ||
+ hasRegisteredNotInitializedExpressPaymentMethods
+ );
+ // An active express payment method keeps its buttons, even while a held skeleton would still show.
+ const showSkeleton = ! isExpressPaymentMethodActive && isLoading;
if (
! hasRegisteredExpressPaymentMethods ||
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/payment-methods/express-payment/checkout-express-payment.tsx b/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/payment-methods/express-payment/checkout-express-payment.tsx
index 9a06cb30670..3a1806530a3 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/payment-methods/express-payment/checkout-express-payment.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/payment-methods/express-payment/checkout-express-payment.tsx
@@ -8,6 +8,7 @@ import { CURRENT_USER_IS_ADMIN } from '@woocommerce/settings';
import { checkoutStore, paymentStore } from '@woocommerce/block-data';
import { useSelect } from '@wordpress/data';
import { Skeleton } from '@woocommerce/base-components/skeleton';
+import { useDelayedSkeleton } from '@woocommerce/base-components/delayed-content-with-skeleton';
import clsx from 'clsx';
/**
@@ -62,6 +63,11 @@ const CheckoutExpressPayment = () => {
registeredExpressPaymentMethods,
} );
+ // The skeleton ends with the totals skeletons, not as soon as the calculation ends.
+ const isLoading = useDelayedSkeleton(
+ isCalculating || hasRegisteredNotInitializedExpressPaymentMethods
+ );
+
if (
! hasRegisteredExpressPaymentMethods ||
hasNoValidRegisteredExpressPaymentMethods
@@ -91,9 +97,7 @@ const CheckoutExpressPayment = () => {
// the express payment method is not active (because they trigger recalculations) and
// the checkout is calculating, because it can result in different express payment methods
// or when the express payment methods are not initialized
- const showSkeleton =
- ! isExpressPaymentMethodActive &&
- ( isCalculating || hasRegisteredNotInitializedExpressPaymentMethods );
+ const showSkeleton = ! isExpressPaymentMethodActive && isLoading;
return (
<>
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-order-summary-block/frontend.tsx b/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-order-summary-block/frontend.tsx
index b937d85b947..201faf6ab61 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-order-summary-block/frontend.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-order-summary-block/frontend.tsx
@@ -4,7 +4,10 @@
import { exitingProps } from '@woocommerce/base-hooks';
import { TotalsFooterItem } from '@woocommerce/base-components/cart-checkout';
import { getCurrencyFromPriceResponse } from '@woocommerce/price-format';
-import { useStoreCart } from '@woocommerce/base-context/hooks';
+import {
+ useSettledOrderSummaryValue,
+ useStoreCart,
+} from '@woocommerce/base-context/hooks';
import { animateCollapse, whenAnimationsEnd } from '@woocommerce/base-utils';
import { __ } from '@wordpress/i18n';
import { Icon, chevronDown, chevronUp } from '@wordpress/icons';
@@ -63,7 +66,10 @@ const FrontendBlock = ( {
} );
const totalsCurrency = getCurrencyFromPriceResponse( cartTotals );
- const totalPrice = parseInt( cartTotals.total_price, 10 );
+ // The total changes when the totals skeletons end, not before.
+ const totalPrice = useSettledOrderSummaryValue(
+ parseInt( cartTotals.total_price, 10 )
+ );
const isToggleShown = hasContainerWidth && ! isLarge;
// After a tap, the content collapses inline before it moves to the checkout actions. Closing because the shopper
diff --git a/plugins/woocommerce/client/blocks/packages/public-api/block-data/cart/test/thunks.ts b/plugins/woocommerce/client/blocks/packages/public-api/block-data/cart/test/thunks.ts
index 67368d2b7fd..d5d9c943a07 100644
--- a/plugins/woocommerce/client/blocks/packages/public-api/block-data/cart/test/thunks.ts
+++ b/plugins/woocommerce/client/blocks/packages/public-api/block-data/cart/test/thunks.ts
@@ -199,7 +199,17 @@ describe( 'changeCartItemQuantity', () => {
10
)( { dispatch: mockDispatch, select: mockSelect } as never );
- await Promise.all( [ promise1, promise2 ] );
+ await promise1;
+ // The aborted request leaves the item pending for the newer request.
+ expect( mockDispatch.itemIsPendingQuantity ).not.toHaveBeenCalledWith(
+ 'item-1',
+ false
+ );
+ await promise2;
+ expect( mockDispatch.itemIsPendingQuantity ).toHaveBeenCalledWith(
+ 'item-1',
+ false
+ );
// First signal should be aborted
expect( abortSignals[ 0 ].aborted ).toBe( true );
diff --git a/plugins/woocommerce/client/blocks/packages/public-api/block-data/cart/thunks.ts b/plugins/woocommerce/client/blocks/packages/public-api/block-data/cart/thunks.ts
index 24e047cd3ad..16ad94f8ceb 100644
--- a/plugins/woocommerce/client/blocks/packages/public-api/block-data/cart/thunks.ts
+++ b/plugins/woocommerce/client/blocks/packages/public-api/block-data/cart/thunks.ts
@@ -674,13 +674,15 @@ export const changeCartItemQuantity =
dispatch.receiveError( isApiErrorResponse( error ) ? error : null );
return Promise.reject( error );
} finally {
- // Clean up controller if it's still the current one for this item.
- if (
- quantityAbortControllers.get( cartItemKey ) === abortController
- ) {
+ const isLatestRequest =
+ quantityAbortControllers.get( cartItemKey ) === abortController;
+ if ( isLatestRequest ) {
quantityAbortControllers.delete( cartItemKey );
}
- dispatch.itemIsPendingQuantity( cartItemKey, false );
+ // A request that a newer one aborted leaves the item pending until the newer one ends.
+ if ( isLatestRequest || abortController === null ) {
+ dispatch.itemIsPendingQuantity( cartItemKey, false );
+ }
}
};
diff --git a/plugins/woocommerce/client/blocks/packages/public-api/types/type-defs/hooks.ts b/plugins/woocommerce/client/blocks/packages/public-api/types/type-defs/hooks.ts
index 6f859402bc5..ba276b05d35 100644
--- a/plugins/woocommerce/client/blocks/packages/public-api/types/type-defs/hooks.ts
+++ b/plugins/woocommerce/client/blocks/packages/public-api/types/type-defs/hooks.ts
@@ -21,6 +21,7 @@ import type {
import type { ApiErrorResponse } from './api-error-response';
export interface StoreCartItemQuantity {
isPendingDelete: boolean;
+ isPendingQuantity: boolean;
quantity: number;
setItemQuantity: React.Dispatch< React.SetStateAction< number > >;
removeItem: () => Promise< boolean >;