javaalgorithmfor-loope-commercesap-commerce-cloud

How can I add products on backend from a Wishlist to cart


I have been working on developing am API which basically adds/updates all the products from a wishlist to the user's cart.

The requirements of the API are as follows:

  1. the API should take input of the ID of the wishlist and add all its products to cart
  2. If the products stored in the wishlist already exists on cart then the cart quantity is to be updated with the sum of both the quantity present on wishlist and the cart.
  3. the wishlist is to be retained after adding the products.

The logic that I had implemented deals with a 2 nested for loop, one loops the wishlist entries and the other loops the cart entries if the cart isn't empty. This logic works well for adding a product but I am having trouble updating the products.

When I hit the API on an empty cart I see no issue all the products were added perfectly, but when I try to hit the API again the only one item of the cart is getting updated while the others stay the same, this is happening cause when the index of the wishlist is incrementing to check for the next product the previously updated products which were updated to the sum of the both quantities are now overridden by the addToCart() method.

like if there are 2 same products on the wishlist and the cart namely Product A and Product B on the two for loops

There will be four conditions

sl cart   wishlist   operation
1   A         A       update
2   A         B        add
3   B         A        add
4   B         B       update

For case 1 the product gets updated at first but the is getting overridden by the addToCart method in the second iteration.

How do I solve this?

Suggestions to other type of implementation is also welcome but I can't change most of the pre-existing methods such as AddToCart() or UpdateCart().

The implementation

public void addAllToCart(String wishListId) 
{
    //code for finding the wishlist by ID saves the wishlist on wishlist of type WishListModel
    //code for getting session cart saves on cart of type CartModel
    if (CollectionUtils.isNotEmpty(wishlist.getEntries())) 
    {
        for (WishListEntryModel wishListEntry : wishlist.getEntries()) 
        {
            checkInCartEntry(wishListEntry, cart.getEntries());
        }
    }
}

private void checkInCartEntry(WishListEntryModel wishListEntry, List<AbstractOrderEntryModel> cartEntries)
{
    if (CollectionUtils.isEmpty(cartEntries))
    {
        addToCart(wishListEntry);
    }
    else
    {
        for (AbstractOrderEntryModel cartEntry : cartEntries) 
        {
            if (wishListEntry.getProduct().getCode().equalsIgnoreCase(cartEntry.getProduct().getCode())) 
            {
                updateCartEntry(cartEntry.getEntryNumber, wishlist.getQuantity+cartEntry.getQuantity);
            } 
            else 
            {
                addToCart(wishListEntry);
            }
        }
    }
}

Solution

  • Thanks for all the comments and answers.
    I solved this issue by separating adding, and updating tasks in the methods.
    Nested loops which I was using earlier as correctly pointed out by @Mak in their answer never break thus causing the addToCart() method to override the manipulations made earlier.
    but instead of using a map, I used a more simple solution and did the following:

    1. Called the session cart
    2. Copied the cart in another variable
    3. Called the addToCart() method and overwrite all the cart entries with the wishlist entry
    4. After overwriting the cart with the shopping list entries, I loop through the copy of the cart and the cart and update the product which are same.

    The code is below

    public void addAllToCart(String wishListId) {
        WishListModel wishList = findByWishlistId(wishListId)
        CartModel cart = cartService.getSessionCart();
        CartModel tempCart = copyOf(cart);
        if(CollectionUtils.isNotEmpty(wishList.getEntries())) {
            for (WishListEntryModel wishListEntry : wishlist.getEntries()) {
                cartFacade.addToCart(wishListEntry);
            }
            if(Objects.nonNull(tempCart)){
                for (AbstractOrderEntryModel tempCartEntry : tempCart.getEntries()) {
                    checkInCartEntry(tempCartEntry, cart.getEntries());
                }
            }
        }
    }
    private void checkInCartEntry(AbstractOrderEntryModel tempCartEntry, List<AbstractOrderEntryModel> cartEntries) throws CommerceCartModificationException {
        for (AbstractOrderEntryModel cartEntry : cartEntries) {
            if(!tempCartEntry.getProduct().getCode().isEmpty() &&  !cartEntry.getProduct().getCode().isEmpty() && (tempCartEntry.getProduct().getCode().equalsIgnoreCase(cartEntry.getProduct().getCode()))) {
                    cartFacade.updateCartEntry(cartEntry.getEntryNumber(), cartEntry.getQuantity());
    
            }
        }
    }