final Locale locale = requestData.getLocale();
final String catalogCategoryCode = request.getParameter(RequestConstants.REQUEST_PARAMETER_CATALOG_CATEGORY_CODE);
final String productSkuCode = request.getParameter(RequestConstants.REQUEST_PARAMETER_PRODUCT_SKU_CODE);
final String quantity = request.getParameter(RequestConstants.REQUEST_PARAMETER_MULTIPLE_ADD_TO_CART_QUANTITY);
final FoAddToCartPojo addToCart = new FoAddToCartPojo();
// INJECT PRODUCT SKU
final ProductSku productSku = productService.getProductSkuByCode(productSkuCode);
addToCart.setProductSku(catalogPojoService.buildProductSku(productSku));
addToCart.setCheckoutShoppingCartUrl(urlService.generateUrl(FoUrls.CART_DETAILS, requestData));
try {
int quantityValue = 1;
if(StringUtils.isNotEmpty(quantity)){
quantityValue = Integer.parseInt(quantity);
}
webManagementService.addToCart(requestData, catalogCategoryCode, productSkuCode, quantityValue);
CartPojo cart = checkoutPojoService.handleCartMapping(requestData.getCart(), requestData.getVirtualCatalogCode(), requestData.getMasterCatalogCode());
for (Iterator<CartItemPojo> iterator = cart.getCartItems().iterator(); iterator.hasNext();) {
CartItemPojo cartItem = (CartItemPojo) iterator.next();
if(cartItem.getProductSku().getCode().equals(productSkuCode)){
addToCart.setQuantity(cartItem.getQuantity());
}
}
if(cart != null && cart.getCartItems() != null){
if (cart.getCartItems().size() == 1) {
addToCart.setCheckoutShoppingCartHeaderLabel(getSpecificMessage(ScopeWebMessage.COMMON, "cart_total_summary_label_one_item", locale));
} else if (cart.getCartItems().size() > 1) {
Object[] cartTotalSummaryLabelParams = { cart.getCartItems().size() };
addToCart.setCheckoutShoppingCartHeaderLabel(getSpecificMessage(ScopeWebMessage.COMMON, "cart_total_summary_label_many_items", cartTotalSummaryLabelParams, locale));
} else {
addToCart.setCheckoutShoppingCartHeaderLabel(getSpecificMessage(ScopeWebMessage.COMMON, "cart_total_summary_label_no_item", locale));
}
} else {
addToCart.setCheckoutShoppingCartHeaderLabel(getSpecificMessage(ScopeWebMessage.COMMON, "cart_total_summary_label_no_item", locale));
}
addToCart.setCheckoutShoppingCartUrl(urlService.generateUrl(FoUrls.CART_DETAILS, requestData));
FoMessagePojo successMessage = new FoMessagePojo();
successMessage.setId("success-add-to-cart-product-sku");
Object[] messageParams = { productSku.getI18nName(localization.getCode()) };
successMessage.setMessage(getSpecificMessage(ScopeWebMessage.CHECKOUT_SHOPPING_CART, "add_to_cart_success_message", messageParams, locale));
addToCart.getSuccessMessages().add(successMessage);
return addToCart;
} catch (Exception e) {
logger.error("", e);
FoMessagePojo errorMessage = new FoMessagePojo();
errorMessage.setId("error-add-to-cart-product-sku");
Object[] messageParams = { productSku.getI18nName(localization.getCode()) };
errorMessage.setMessage(getSpecificMessage(ScopeWebMessage.CHECKOUT_SHOPPING_CART, "add_to_cart_error_message", messageParams, locale));
addToCart.getErrorMessages().add(errorMessage);
addToCart.setStatuts(false);
return addToCart;
}
}