use of com.salesmanager.core.model.shoppingcart.ShoppingCartItem in project shopizer by shopizer-ecommerce.
the class ShoppingCartServiceImpl method populateShoppingCartItem.
@Override
public ShoppingCartItem populateShoppingCartItem(final Product product) throws ServiceException {
Validate.notNull(product, "Product should not be null");
Validate.notNull(product.getMerchantStore(), "Product.merchantStore should not be null");
ShoppingCartItem item = new ShoppingCartItem(product);
// set item price
FinalPrice price = pricingService.calculateProductPrice(product);
item.setItemPrice(price.getFinalPrice());
return item;
}
use of com.salesmanager.core.model.shoppingcart.ShoppingCartItem in project shopizer by shopizer-ecommerce.
the class ShoppingCartServiceImpl method createShippingProduct.
@Override
public List<ShippingProduct> createShippingProduct(final ShoppingCart cart) throws ServiceException {
/**
* Determines if products are virtual
*/
Set<ShoppingCartItem> items = cart.getLineItems();
List<ShippingProduct> shippingProducts = null;
for (ShoppingCartItem item : items) {
Product product = item.getProduct();
if (!product.isProductVirtual() && product.isProductShipeable()) {
if (shippingProducts == null) {
shippingProducts = new ArrayList<ShippingProduct>();
}
ShippingProduct shippingProduct = new ShippingProduct(product);
shippingProduct.setQuantity(item.getQuantity());
shippingProduct.setFinalPrice(item.getFinalPrice());
shippingProducts.add(shippingProduct);
}
}
return shippingProducts;
}
use of com.salesmanager.core.model.shoppingcart.ShoppingCartItem in project shopizer by shopizer-ecommerce.
the class ShoppingCartServiceImpl method mergeShoppingCarts.
@Override
public ShoppingCart mergeShoppingCarts(final ShoppingCart userShoppingModel, final ShoppingCart sessionCart, final MerchantStore store) throws Exception {
if (sessionCart.getCustomerId() != null && sessionCart.getCustomerId().equals(userShoppingModel.getCustomerId())) {
LOGGER.info("Session Shopping cart belongs to same logged in user");
if (CollectionUtils.isNotEmpty(userShoppingModel.getLineItems()) && CollectionUtils.isNotEmpty(sessionCart.getLineItems())) {
return userShoppingModel;
}
}
LOGGER.info("Starting merging shopping carts");
if (CollectionUtils.isNotEmpty(sessionCart.getLineItems())) {
Set<ShoppingCartItem> shoppingCartItemsSet = getShoppingCartItems(sessionCart, store, userShoppingModel);
boolean duplicateFound = false;
if (CollectionUtils.isNotEmpty(shoppingCartItemsSet)) {
for (ShoppingCartItem sessionShoppingCartItem : shoppingCartItemsSet) {
if (CollectionUtils.isNotEmpty(userShoppingModel.getLineItems())) {
for (ShoppingCartItem cartItem : userShoppingModel.getLineItems()) {
if (cartItem.getProduct().getId().longValue() == sessionShoppingCartItem.getProduct().getId().longValue()) {
if (CollectionUtils.isNotEmpty(cartItem.getAttributes())) {
if (!duplicateFound) {
LOGGER.info("Dupliate item found..updating exisitng product quantity");
cartItem.setQuantity(cartItem.getQuantity() + sessionShoppingCartItem.getQuantity());
duplicateFound = true;
break;
}
}
}
}
}
if (!duplicateFound) {
LOGGER.info("New item found..adding item to Shopping cart");
userShoppingModel.getLineItems().add(sessionShoppingCartItem);
}
}
}
}
LOGGER.info("Shopping Cart merged successfully.....");
saveOrUpdate(userShoppingModel);
removeShoppingCart(sessionCart);
return userShoppingModel;
}
use of com.salesmanager.core.model.shoppingcart.ShoppingCartItem in project shopizer by shopizer-ecommerce.
the class ShoppingCartServiceImpl method getShoppingCartItems.
private Set<ShoppingCartItem> getShoppingCartItems(final ShoppingCart sessionCart, final MerchantStore store, final ShoppingCart cartModel) throws Exception {
Set<ShoppingCartItem> shoppingCartItemsSet = null;
if (CollectionUtils.isNotEmpty(sessionCart.getLineItems())) {
shoppingCartItemsSet = new HashSet<ShoppingCartItem>();
for (ShoppingCartItem shoppingCartItem : sessionCart.getLineItems()) {
Product product = productService.getById(shoppingCartItem.getProductId());
if (product == null) {
throw new Exception("Item with id " + shoppingCartItem.getProductId() + " does not exist");
}
if (product.getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new Exception("Item with id " + shoppingCartItem.getProductId() + " does not belong to merchant " + store.getId());
}
ShoppingCartItem item = populateShoppingCartItem(product);
item.setQuantity(shoppingCartItem.getQuantity());
item.setShoppingCart(cartModel);
List<ShoppingCartAttributeItem> cartAttributes = new ArrayList<ShoppingCartAttributeItem>();
if (shoppingCartItem != null && !CollectionUtils.isEmpty(shoppingCartItem.getAttributes())) {
cartAttributes.addAll(shoppingCartItem.getAttributes());
if (CollectionUtils.isNotEmpty(cartAttributes)) {
for (ShoppingCartAttributeItem shoppingCartAttributeItem : cartAttributes) {
ProductAttribute productAttribute = productAttributeService.getById(shoppingCartAttributeItem.getId());
if (productAttribute != null && productAttribute.getProduct().getId().longValue() == product.getId().longValue()) {
ShoppingCartAttributeItem attributeItem = new ShoppingCartAttributeItem(item, productAttribute);
if (shoppingCartAttributeItem.getId() > 0) {
attributeItem.setId(shoppingCartAttributeItem.getId());
}
item.addAttributes(attributeItem);
}
}
}
}
shoppingCartItemsSet.add(item);
}
}
return shoppingCartItemsSet;
}
use of com.salesmanager.core.model.shoppingcart.ShoppingCartItem in project shopizer by shopizer-ecommerce.
the class ShoppingCartTest method createShoppingCart.
@Test
public void createShoppingCart() throws Exception {
MerchantStore store = merchantService.getByCode(MerchantStore.DEFAULT_STORE);
Language en = languageService.getByCode("en");
/**
* CATALOG CREATION *
*/
ProductType generalType = productTypeService.getProductType(ProductType.GENERAL_TYPE);
/**
* Create the category
*/
Category shirts = new Category();
shirts.setMerchantStore(store);
shirts.setCode("shirts");
CategoryDescription shirtsEnglishDescription = new CategoryDescription();
shirtsEnglishDescription.setName("Shirts");
shirtsEnglishDescription.setCategory(shirts);
shirtsEnglishDescription.setLanguage(en);
Set<CategoryDescription> descriptions = new HashSet<CategoryDescription>();
descriptions.add(shirtsEnglishDescription);
shirts.setDescriptions(descriptions);
categoryService.create(shirts);
/**
* Create a manufacturer
*/
Manufacturer addidas = new Manufacturer();
addidas.setMerchantStore(store);
addidas.setCode("addidas");
ManufacturerDescription addidasDesc = new ManufacturerDescription();
addidasDesc.setLanguage(en);
addidasDesc.setManufacturer(addidas);
addidasDesc.setName("Addidas");
addidas.getDescriptions().add(addidasDesc);
manufacturerService.create(addidas);
/**
* Create an option
*/
ProductOption option = new ProductOption();
option.setMerchantStore(store);
option.setCode("color");
option.setProductOptionType(ProductOptionType.Radio.name());
ProductOptionDescription optionDescription = new ProductOptionDescription();
optionDescription.setLanguage(en);
optionDescription.setName("Color");
optionDescription.setDescription("Item color");
optionDescription.setProductOption(option);
option.getDescriptions().add(optionDescription);
productOptionService.saveOrUpdate(option);
/**
* first option value *
*/
ProductOptionValue white = new ProductOptionValue();
white.setMerchantStore(store);
white.setCode("white");
ProductOptionValueDescription whiteDescription = new ProductOptionValueDescription();
whiteDescription.setLanguage(en);
whiteDescription.setName("White");
whiteDescription.setDescription("White color");
whiteDescription.setProductOptionValue(white);
white.getDescriptions().add(whiteDescription);
productOptionValueService.saveOrUpdate(white);
ProductOptionValue black = new ProductOptionValue();
black.setMerchantStore(store);
black.setCode("black");
/**
* second option value *
*/
ProductOptionValueDescription blackDesc = new ProductOptionValueDescription();
blackDesc.setLanguage(en);
blackDesc.setName("Black");
blackDesc.setDescription("Black color");
blackDesc.setProductOptionValue(black);
black.getDescriptions().add(blackDesc);
productOptionValueService.saveOrUpdate(black);
/**
* Create a complex product
*/
Product product = new Product();
product.setProductHeight(new BigDecimal(4));
product.setProductLength(new BigDecimal(3));
product.setProductWidth(new BigDecimal(1));
product.setSku("XABC12");
product.setManufacturer(addidas);
product.setType(generalType);
product.setMerchantStore(store);
// Product description
ProductDescription description = new ProductDescription();
description.setName("Short sleeves shirt");
description.setLanguage(en);
description.setProduct(product);
product.getDescriptions().add(description);
product.getCategories().add(shirts);
// availability
ProductAvailability availability = new ProductAvailability();
availability.setProductDateAvailable(new Date());
availability.setProductQuantity(100);
availability.setRegion("*");
// associate with product
availability.setProduct(product);
// price
ProductPrice dprice = new ProductPrice();
dprice.setDefaultPrice(true);
dprice.setProductPriceAmount(new BigDecimal(29.99));
dprice.setProductAvailability(availability);
ProductPriceDescription dpd = new ProductPriceDescription();
dpd.setName("Base price");
dpd.setProductPrice(dprice);
dpd.setLanguage(en);
dprice.getDescriptions().add(dpd);
availability.getPrices().add(dprice);
product.getAvailabilities().add(availability);
// attributes
// white
ProductAttribute whiteAttribute = new ProductAttribute();
whiteAttribute.setProduct(product);
whiteAttribute.setProductOption(option);
whiteAttribute.setAttributeDefault(true);
// no price variation
whiteAttribute.setProductAttributePrice(new BigDecimal(0));
// no weight variation
whiteAttribute.setProductAttributeWeight(new BigDecimal(0));
whiteAttribute.setProductOption(option);
whiteAttribute.setProductOptionValue(white);
product.getAttributes().add(whiteAttribute);
// black
ProductAttribute blackAttribute = new ProductAttribute();
blackAttribute.setProduct(product);
blackAttribute.setProductOption(option);
// 5 + dollars
blackAttribute.setProductAttributePrice(new BigDecimal(5));
// no weight variation
blackAttribute.setProductAttributeWeight(new BigDecimal(0));
blackAttribute.setProductOption(option);
blackAttribute.setProductOptionValue(black);
product.getAttributes().add(blackAttribute);
productService.create(product);
/**
* Create Shopping cart *
*/
ShoppingCart shoppingCart = new ShoppingCart();
shoppingCart.setMerchantStore(store);
UUID cartCode = UUID.randomUUID();
shoppingCart.setShoppingCartCode(cartCode.toString());
ShoppingCartItem item = new ShoppingCartItem(shoppingCart, product);
item.setShoppingCart(shoppingCart);
FinalPrice price = pricingService.calculateProductPrice(product);
item.setItemPrice(price.getFinalPrice());
item.setQuantity(1);
/**
* user selects black *
*/
ShoppingCartAttributeItem attributeItem = new ShoppingCartAttributeItem(item, blackAttribute);
item.getAttributes().add(attributeItem);
shoppingCart.getLineItems().add(item);
// create cart
shoppingCartService.create(shoppingCart);
/**
* Retrieve cart *
*/
ShoppingCart retrievedCart = shoppingCartService.getByCode(cartCode.toString(), store);
Assert.assertNotNull(retrievedCart);
/**
* Delete cart *
*/
shoppingCartService.delete(retrievedCart);
/**
* Check if cart has been deleted *
*/
retrievedCart = shoppingCartService.getByCode(cartCode.toString(), store);
Assert.assertNull(retrievedCart);
// Clean up for other tests
categoryService.delete(shirts);
}
Aggregations