-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6089 from reactioncommerce/fix-aldeed-6077-cart-o…
…rder-catalog-lookup fix: find catalog product regardless of visibility
- Loading branch information
Showing
40 changed files
with
496 additions
and
469 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/core-services/cart/mutations/removeMissingItemsFromCart.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @method removeMissingItemsFromCart | ||
* @summary Checks a cart to see if any of the items in it correspond with | ||
* product variants that are now gone (hidden or deleted). Updates the | ||
* cart to move them to `missingItems` array and remove them from | ||
* `items`. Mutates `cart` but doesn't save to database. | ||
* @param {Object} context - App context | ||
* @param {Object} cart The Cart object to check | ||
* @returns {Promise<undefined>} Nothing. Mutates `cart` object | ||
*/ | ||
export default async function removeMissingItemsFromCart(context, cart) { | ||
const catalogItemIds = cart.items.map((item) => item.productId); | ||
const catalogItems = await context.collections.Catalog.find({ | ||
"product.productId": { $in: catalogItemIds }, | ||
"product.isDeleted": { $ne: true }, | ||
"product.isVisible": true | ||
}).toArray(); | ||
|
||
// If any items were missing from the catalog, deleted, or hidden, move them into | ||
// a missingItems array. A product variant that has been hidden or deleted since | ||
// being added to a cart is no longer valid as a cart item. | ||
const items = []; | ||
const missingItems = []; | ||
for (const item of cart.items) { | ||
const catalogItem = catalogItems.find((cItem) => cItem.product.productId === item.productId); | ||
if (!catalogItem) { | ||
missingItems.push(item); | ||
continue; | ||
} | ||
const { variant: catalogVariant } = context.queries.findVariantInCatalogProduct(catalogItem.product, item.variantId); | ||
if (!catalogVariant) { | ||
missingItems.push(item); | ||
continue; | ||
} | ||
items.push(item); | ||
} | ||
|
||
if (missingItems.length === 0) return; | ||
|
||
cart.items = items; | ||
cart.missingItems = missingItems; | ||
cart.updatedAt = new Date(); | ||
|
||
// Usually `mutations.transformAndValidateCart` removes missing items from groups | ||
// whenever we save a cart, but sometimes this mutation will need to be called | ||
// when initially reading a cart, before attempting to transform it to a CommonOrder. | ||
// So we'll also update the groups here. | ||
cart.shipping.forEach((group) => { | ||
group.itemIds = (group.itemIds || []).filter((itemId) => !!items.find((item) => item._id === itemId)); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.