Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Add isWithinQuantityLimits function
Browse files Browse the repository at this point in the history
This is because we shouldn't show a notice if the quantity limits change, but the item's quantity is still OK.
  • Loading branch information
opr committed Dec 13, 2022
1 parent ebdb4d4 commit 05527e5
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions assets/js/data/cart/notify-quantity-changes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
/**
* External dependencies
*/
import { Cart } from '@woocommerce/types';
import { Cart, CartItem } from '@woocommerce/types';
import { dispatch } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';

const isWithinQuantityLimits = ( cartItem: CartItem ) => {
return (
cartItem.quantity >= cartItem.quantity_limits.minimum &&
cartItem.quantity <= cartItem.quantity_limits.maximum &&
cartItem.quantity % cartItem.quantity_limits.multiple_of === 0
);
};

const notifyIfQuantityLimitsChanged = ( oldCart: Cart, newCart: Cart ) => {
newCart.items.forEach( ( cartItem ) => {
const oldCartItem = oldCart.items.find( ( item ) => {
Expand All @@ -19,6 +27,10 @@ const notifyIfQuantityLimitsChanged = ( oldCart: Cart, newCart: Cart ) => {
return;
}

if ( isWithinQuantityLimits( cartItem ) ) {
return;
}

const quantityAboveMax =
cartItem.quantity > cartItem.quantity_limits.maximum;
const quantityBelowMin =
Expand Down Expand Up @@ -87,13 +99,17 @@ const notifyIfQuantityChanged = (
if ( ! oldCartItem ) {
return;
}

if ( cartItem.key === oldCartItem.key ) {
if ( cartItem.quantity !== oldCartItem.quantity ) {
if (
cartItem.quantity !== oldCartItem.quantity &&
isWithinQuantityLimits( cartItem )
) {
dispatch( 'core/notices' ).createInfoNotice(
sprintf(
/* translators: %1$s is the name of the item, %2$d is the quantity of the item. */
__(
'The quantity of %1$s has been updated to %2$d.',
'The quantity of "%1$s" has been changed to %2$d.',
'woo-gutenberg-products-block'
),
cartItem.name,
Expand Down

0 comments on commit 05527e5

Please sign in to comment.