public Resolution addRevitalProductsAndGoToCart() { ProductReferrer productReferrer = null; User user = null; if (getPrincipal() != null) { user = getUserService().getUserById(getPrincipal().getId()); // user = UserCache.getInstance().getUserById(getPrincipal().getId()).getUser(); if (user == null) { user = userManager.createAndLoginAsGuestUser(null, null); } } else { user = userManager.createAndLoginAsGuestUser(null, null); } List<ProductVariant> productVariantList = new ArrayList<ProductVariant>(); productVariantList.add( getProductVariantService().getVariantById("NUT410-01")); // Revital Daily Health // Supplement productVariantList.add( getProductVariantService().getVariantById("NUT411-01")); // Revital For Women productVariantList.add( getProductVariantService().getVariantById("NUT412-01")); // Revital Form Seniors productVariantList.add( getProductVariantService().getVariantById("NUT598-01")); // Ranbaxy Revitalite Powder Order order = orderManager.getOrCreateOrder(user); try { if (productReferrerId != null) { productReferrer = getBaseDao().get(ProductReferrer.class, productReferrerId); } if (productVariantList != null && productVariantList.size() > 0) { orderManager.createLineItems(productVariantList, order, null, null, productReferrer); } } catch (Exception e) { // } return new RedirectResolution(CartAction.class); }
@SuppressWarnings("unchecked") @JsonHandler @DefaultHandler public Resolution buyNow() { getConfigValuesFromJson(jsonConfigValues); ProductVariant productVariant = getProductVariantService().getVariantById(variantId); User user = null; ProductReferrer productReferrer = null; if (getPrincipal() != null) { user = getUserService().getUserById(getPrincipal().getId()); // user = UserCache.getInstance().getUserById(getPrincipal().getId()).getUser(); if (user == null) { user = userManager.createAndLoginAsGuestUser(null, null); } } else { user = userManager.createAndLoginAsGuestUser(null, null); } Order order = orderManager.getOrCreateOrder(user); CartLineItemConfig lineItemConfig = new CartLineItemConfig(); boolean isLineItemCreated = false; try { for (LineItemConfigValuesDTO dto : configValues) { CartLineItemConfigValues configValue = new CartLineItemConfigValues(); configValue.setLineItemConfig(lineItemConfig); VariantConfigOption configOption = getBaseDao().get(VariantConfigOption.class, dto.getOptionId()); VariantConfigValues selectedConfigValue = getBaseDao().get(VariantConfigValues.class, dto.getValueId()); if (configOption != null && selectedConfigValue != null) { if (configOption .getAdditionalParam() .equalsIgnoreCase(VariantConfigOptionParam.ENGRAVING.param())) { configValue.setValue(getNameToBeEngraved()); } else { configValue.setValue(selectedConfigValue.getValue()); } } if (VariantConfigOptionParam.shouldPriceBeDoubledForParam( configOption.getAdditionalParam())) { configValue.setAdditionalPrice(selectedConfigValue.getAdditonalPrice() * 2); } else { configValue.setAdditionalPrice(selectedConfigValue.getAdditonalPrice()); } if (VariantConfigOptionParam.shouldPriceBeDoubledForParam( configOption.getAdditionalParam())) { configValue.setCostPrice(selectedConfigValue.getCostPrice() * 2); } else { configValue.setCostPrice(selectedConfigValue.getCostPrice()); } configValue.setVariantConfigOption(configOption); lineItemConfig.getCartLineItemConfigValues().add(configValue); } productVariant.setQty(new Long(1)); if (productReferrerId != null) { productReferrer = getBaseDao().get(ProductReferrer.class, productReferrerId); } isLineItemCreated = orderManager.createLineItems(productVariant, lineItemConfig, order, productReferrer); userProductHistoryDao.updateIsAddedToCart(productVariant.getProduct(), user, order); } catch (OutOfStockException e) { getContext().getValidationErrors().add("e2", new SimpleError(e.getMessage())); return new JsonResolution(getContext().getValidationErrors(), getContext().getLocale()); } Map dataMap = new HashMap(); String cartUrl = linkManager.getCartUrl(); dataMap.put("url", cartUrl); dataMap.put("name", productVariant.getProduct().getName()); dataMap.put("qty", productVariant.getQty()); Long itemsInCart = Long.valueOf( order.getExclusivelyProductCartLineItems().size() + order.getExclusivelyComboCartLineItems().size()); dataMap.put("itemsInCart", isLineItemCreated ? itemsInCart + 1L : itemsInCart); HealthkartResponse healthkartResponse = new HealthkartResponse( HealthkartResponse.STATUS_OK, "Product has been added to cart", dataMap); noCache(); return new JsonResolution(healthkartResponse); }