Checkout: Fix display of coupon removal notices (#50412)

* Replace receiveCartContents with thunk which handles errors

* Avoid notice dismissal when pushing changes

* Update coupon error messaging for store API requests

* Changelog

* Undo thunks import change

* Remove unused removeAllNotices
This commit is contained in:
Mike Jolley 2024-08-09 16:28:18 +01:00 committed by GitHub
parent 062b161525
commit 1160414b14
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 28 additions and 30 deletions

View File

@ -15,7 +15,6 @@ import { BillingAddress, ShippingAddress } from '@woocommerce/settings';
import {
triggerAddedToCartEvent,
triggerAddingToCartEvent,
camelCaseKeys,
} from '@woocommerce/base-utils';
/**
@ -57,27 +56,6 @@ export const setErrorData = (
};
};
/**
* Returns an action object used in updating the store with the provided cart.
*
* This omits the customer addresses so that only updates to cart items and totals are received. This is useful when
* currently editing address information to prevent it being overwritten from the server.
*
* This is a generic response action.
*
* @param {CartResponse} response
*/
export const receiveCartContents = (
response: CartResponse
): { type: string; response: Partial< Cart > } => {
const cart = camelCaseKeys( response ) as unknown as Cart;
const { shippingAddress, billingAddress, ...cartWithoutAddress } = cart;
return {
type: types.SET_CART_DATA,
response: cartWithoutAddress,
};
};
/**
* Returns an action object used to track when a coupon is applying.
*
@ -503,7 +481,6 @@ type Actions =
| typeof itemIsPendingDelete
| typeof itemIsPendingQuantity
| typeof receiveApplyingCoupon
| typeof receiveCartContents
| typeof receiveCartItem
| typeof receiveRemovingCoupon
| typeof removeCoupon

View File

@ -25,7 +25,6 @@ export const notifyCartErrors = (
createNotice( 'error', decodeEntities( error.message ), {
id: error.code,
context: 'wc/cart',
isDismissible: false,
} );
}
} );

View File

@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { removeAllNotices, debounce, pick } from '@woocommerce/base-utils';
import { debounce, pick } from '@woocommerce/base-utils';
import {
CartBillingAddress,
CartShippingAddress,
@ -157,7 +157,6 @@ const updateCustomerData = (): void => {
localState.dirtyProps.billingAddress = [];
localState.dirtyProps.shippingAddress = [];
localState.doingPush = false;
removeAllNotices();
} )
.catch( ( response ) => {
localState.doingPush = false;

View File

@ -23,7 +23,7 @@ import { CartDispatchFromMap, CartSelectFromMap } from './index';
* @param {CartResponse} response
*/
export const receiveCart =
( response: CartResponse ) =>
( response: Partial< CartResponse > ) =>
( {
dispatch,
select,
@ -43,6 +43,22 @@ export const receiveCart =
dispatch.setCartData( newCart );
};
/**
* Updates the store with the provided cart but omits the customer addresses.
*
* This is useful when currently editing address information to prevent it being overwritten from the server.
*
* @param {CartResponse} response
*/
export const receiveCartContents =
( response: Partial< CartResponse > ) =>
( { dispatch }: { dispatch: CartDispatchFromMap } ) => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { shipping_address, billing_address, ...cartWithoutAddress } =
response;
dispatch.receiveCart( cartWithoutAddress );
};
/**
* A thunk used in updating the store with cart errors retrieved from a request.
*/
@ -58,4 +74,7 @@ export const receiveError =
}
};
export type Thunks = typeof receiveCart | typeof receiveError;
export type Thunks =
| typeof receiveCart
| typeof receiveCartContents
| typeof receiveError;

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Ensure coupon errors are visible on block checkout when invalid coupons are removed.

View File

@ -13,7 +13,7 @@ use Automattic\WooCommerce\Utilities\StringUtil;
defined( 'ABSPATH' ) || exit;
require_once dirname( __FILE__ ) . '/legacy/class-wc-legacy-coupon.php';
require_once __DIR__ . '/legacy/class-wc-legacy-coupon.php';
/**
* Coupon class.
@ -1066,7 +1066,7 @@ class WC_Coupon extends WC_Legacy_Coupon {
$err = __( 'Sorry, this coupon is not applicable to your cart contents.', 'woocommerce' );
break;
case self::E_WC_COUPON_USAGE_LIMIT_COUPON_STUCK:
if ( is_user_logged_in() && wc_get_page_id( 'myaccount' ) > 0 ) {
if ( is_user_logged_in() && wc_get_page_id( 'myaccount' ) > 0 && ! WC()->is_store_api_request() ) {
/* translators: %s: myaccount page link. */
$err = sprintf( __( 'Coupon usage limit has been reached. If you were using this coupon just now but your order was not complete, you can retry or cancel the order by going to the <a href="%s">my account page</a>.', 'woocommerce' ), wc_get_endpoint_url( 'orders', '', wc_get_page_permalink( 'myaccount' ) ) );
} else {