@Test public void testExecute() { MutableShoppingCart shoppingCart = new ShoppingCartImpl(); shoppingCart.initialise(ctx().getBean("amountCalculationStrategy", AmountCalculationStrategy.class)); final ShoppingCartCommandFactory commands = ctx().getBean("shoppingCartCommandFactory", ShoppingCartCommandFactory.class); commands.execute(shoppingCart, (Map) singletonMap(ShoppingCartCommand.CMD_SETSHOP, 10)); commands.execute(shoppingCart, (Map) singletonMap(ShoppingCartCommand.CMD_CHANGECURRENCY, "EUR")); assertEquals(MoneyUtils.ZERO, shoppingCart.getTotal().getSubTotal()); Map<String, String> params = new HashMap<>(); params.put(ShoppingCartCommand.CMD_ADDTOCART, "CC_TEST1"); commands.execute(shoppingCart, (Map) params); assertEquals("19.99", shoppingCart.getTotal().getSubTotal().toPlainString()); commands.execute(shoppingCart, (Map) params); assertEquals("39.98", shoppingCart.getTotal().getSubTotal().toPlainString()); commands.execute(shoppingCart, (Map) params); assertEquals("57.00", shoppingCart.getTotal().getSubTotal().toPlainString()); params.put(ShoppingCartCommand.CMD_P_QTY, "7"); commands.execute(shoppingCart, (Map) params); assertEquals("180.00", shoppingCart.getTotal().getSubTotal().toPlainString()); params.put(ShoppingCartCommand.CMD_P_QTY, "zzzz"); commands.execute(shoppingCart, (Map) params); assertEquals("198.00", shoppingCart.getTotal().getSubTotal().toPlainString()); }
@Override protected void execute(final MutableShoppingCart shoppingCart, final ProductSku productSku, final String skuCode, final String supplier, final String itemGroup, final BigDecimal qty, final Map<String, Object> parameters) { if (determineSkuPrice(shoppingCart, supplier, skuCode, BigDecimal.ONE) == null) { LOG.debug("[{}] Unable to add item {} for supplier {} because could not resolve price", shoppingCart.getGuid(), skuCode, supplier); return; } final long shopId = shoppingCart.getShoppingContext().getCustomerShopId(); final BigDecimal cartQty = shoppingCart.getProductSkuQuantity(supplier, skuCode); final BigDecimal toAdd = getQuantityValue(shopId, supplier, skuCode, qty, cartQty); String skuName = skuCode; boolean configurable = false; Map<String, Pair<String, BigDecimal>> selectedOptions = Collections.emptyMap(); if (productSku != null) { skuName = new FailoverStringI18NModel( productSku.getDisplayName(), productSku.getName() ).getValue(shoppingCart.getCurrentLocale()); final Product product = getProductService().getProductById(productSku.getProduct().getProductId()); if (product.getOptions().isConfigurable()) { configurable = true; selectedOptions = new LinkedHashMap<>(); final List<ProductOption> options = product.getOptions().getConfigurationOptionForSKU(productSku); for (final ProductOption option : options) { final String skuOption = (String) parameters.get(option.getAttributeCode()); if ((option.isMandatory() && StringUtils.isBlank(skuOption)) || (StringUtils.isNotBlank(skuOption) && !option.getOptionSkuCodes().contains(skuOption))) { LOG.debug("[{}] Unable to add item {} for supplier {} because option {} has invalid value {}", shoppingCart.getGuid(), skuCode, supplier, option.getAttributeCode(), skuOption); return; } if (StringUtils.isBlank(skuOption)) { continue; } final ProductSku productSkuOption = getProductService().getProductSkuByCode(skuOption); if (determineSkuPrice(shoppingCart, supplier, skuOption, BigDecimal.ONE) == null) { LOG.debug("[{}] Unable to add item {} for supplier {} because could not resolve price for option {} value {}", shoppingCart.getGuid(), skuCode, supplier, option.getAttributeCode(), skuOption); return; } final BigDecimal toAddOption = toAdd.multiply(option.getQuantity()).stripTrailingZeros(); final String skuNameOption = new FailoverStringI18NModel( productSkuOption.getDisplayName(), productSkuOption.getName() ).getValue(shoppingCart.getCurrentLocale()); selectedOptions.put(skuOption, new Pair<>(skuNameOption, toAddOption)); } } } final String enforceGroup = configurable && StringUtils.isBlank(itemGroup) ? UUID.randomUUID().toString() : itemGroup; final boolean optionalGroupExists = configurable && shoppingCart.getCartItemList().stream().anyMatch(cartItem -> ShoppingCartUtils.isCartItem(cartItem, supplier, skuCode, enforceGroup)); if (optionalGroupExists) { LOG.debug("[{}] Unable to add item {} for supplier {} because item with this group already exists {}", shoppingCart.getGuid(), skuCode, supplier, enforceGroup); return; } shoppingCart.addProductSkuToCart(supplier, skuCode, skuName, toAdd, enforceGroup, configurable, false); if (configurable) { for (final Map.Entry<String, Pair<String, BigDecimal>> optItem : selectedOptions.entrySet()) { shoppingCart.addProductSkuToCart(supplier, optItem.getKey(), optItem.getValue().getFirst(), optItem.getValue().getSecond(), enforceGroup, false, true); } } recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); LOG.debug("[{}] Added {} item(s) of sku code {}", shoppingCart.getGuid(), toAdd, skuCode); }
AddSkuToCartEventCommandImpl extends AbstractSkuCartCommandImpl { @Override protected void execute(final MutableShoppingCart shoppingCart, final ProductSku productSku, final String skuCode, final String supplier, final String itemGroup, final BigDecimal qty, final Map<String, Object> parameters) { if (determineSkuPrice(shoppingCart, supplier, skuCode, BigDecimal.ONE) == null) { LOG.debug("[{}] Unable to add item {} for supplier {} because could not resolve price", shoppingCart.getGuid(), skuCode, supplier); return; } final long shopId = shoppingCart.getShoppingContext().getCustomerShopId(); final BigDecimal cartQty = shoppingCart.getProductSkuQuantity(supplier, skuCode); final BigDecimal toAdd = getQuantityValue(shopId, supplier, skuCode, qty, cartQty); String skuName = skuCode; boolean configurable = false; Map<String, Pair<String, BigDecimal>> selectedOptions = Collections.emptyMap(); if (productSku != null) { skuName = new FailoverStringI18NModel( productSku.getDisplayName(), productSku.getName() ).getValue(shoppingCart.getCurrentLocale()); final Product product = getProductService().getProductById(productSku.getProduct().getProductId()); if (product.getOptions().isConfigurable()) { configurable = true; selectedOptions = new LinkedHashMap<>(); final List<ProductOption> options = product.getOptions().getConfigurationOptionForSKU(productSku); for (final ProductOption option : options) { final String skuOption = (String) parameters.get(option.getAttributeCode()); if ((option.isMandatory() && StringUtils.isBlank(skuOption)) || (StringUtils.isNotBlank(skuOption) && !option.getOptionSkuCodes().contains(skuOption))) { LOG.debug("[{}] Unable to add item {} for supplier {} because option {} has invalid value {}", shoppingCart.getGuid(), skuCode, supplier, option.getAttributeCode(), skuOption); return; } if (StringUtils.isBlank(skuOption)) { continue; } final ProductSku productSkuOption = getProductService().getProductSkuByCode(skuOption); if (determineSkuPrice(shoppingCart, supplier, skuOption, BigDecimal.ONE) == null) { LOG.debug("[{}] Unable to add item {} for supplier {} because could not resolve price for option {} value {}", shoppingCart.getGuid(), skuCode, supplier, option.getAttributeCode(), skuOption); return; } final BigDecimal toAddOption = toAdd.multiply(option.getQuantity()).stripTrailingZeros(); final String skuNameOption = new FailoverStringI18NModel( productSkuOption.getDisplayName(), productSkuOption.getName() ).getValue(shoppingCart.getCurrentLocale()); selectedOptions.put(skuOption, new Pair<>(skuNameOption, toAddOption)); } } } final String enforceGroup = configurable && StringUtils.isBlank(itemGroup) ? UUID.randomUUID().toString() : itemGroup; final boolean optionalGroupExists = configurable && shoppingCart.getCartItemList().stream().anyMatch(cartItem -> ShoppingCartUtils.isCartItem(cartItem, supplier, skuCode, enforceGroup)); if (optionalGroupExists) { LOG.debug("[{}] Unable to add item {} for supplier {} because item with this group already exists {}", shoppingCart.getGuid(), skuCode, supplier, enforceGroup); return; } shoppingCart.addProductSkuToCart(supplier, skuCode, skuName, toAdd, enforceGroup, configurable, false); if (configurable) { for (final Map.Entry<String, Pair<String, BigDecimal>> optItem : selectedOptions.entrySet()) { shoppingCart.addProductSkuToCart(supplier, optItem.getKey(), optItem.getValue().getFirst(), optItem.getValue().getSecond(), enforceGroup, false, true); } } recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); LOG.debug("[{}] Added {} item(s) of sku code {}", shoppingCart.getGuid(), toAdd, skuCode); } }
AddSkuToCartEventCommandImpl extends AbstractSkuCartCommandImpl { @Override protected void execute(final MutableShoppingCart shoppingCart, final ProductSku productSku, final String skuCode, final String supplier, final String itemGroup, final BigDecimal qty, final Map<String, Object> parameters) { if (determineSkuPrice(shoppingCart, supplier, skuCode, BigDecimal.ONE) == null) { LOG.debug("[{}] Unable to add item {} for supplier {} because could not resolve price", shoppingCart.getGuid(), skuCode, supplier); return; } final long shopId = shoppingCart.getShoppingContext().getCustomerShopId(); final BigDecimal cartQty = shoppingCart.getProductSkuQuantity(supplier, skuCode); final BigDecimal toAdd = getQuantityValue(shopId, supplier, skuCode, qty, cartQty); String skuName = skuCode; boolean configurable = false; Map<String, Pair<String, BigDecimal>> selectedOptions = Collections.emptyMap(); if (productSku != null) { skuName = new FailoverStringI18NModel( productSku.getDisplayName(), productSku.getName() ).getValue(shoppingCart.getCurrentLocale()); final Product product = getProductService().getProductById(productSku.getProduct().getProductId()); if (product.getOptions().isConfigurable()) { configurable = true; selectedOptions = new LinkedHashMap<>(); final List<ProductOption> options = product.getOptions().getConfigurationOptionForSKU(productSku); for (final ProductOption option : options) { final String skuOption = (String) parameters.get(option.getAttributeCode()); if ((option.isMandatory() && StringUtils.isBlank(skuOption)) || (StringUtils.isNotBlank(skuOption) && !option.getOptionSkuCodes().contains(skuOption))) { LOG.debug("[{}] Unable to add item {} for supplier {} because option {} has invalid value {}", shoppingCart.getGuid(), skuCode, supplier, option.getAttributeCode(), skuOption); return; } if (StringUtils.isBlank(skuOption)) { continue; } final ProductSku productSkuOption = getProductService().getProductSkuByCode(skuOption); if (determineSkuPrice(shoppingCart, supplier, skuOption, BigDecimal.ONE) == null) { LOG.debug("[{}] Unable to add item {} for supplier {} because could not resolve price for option {} value {}", shoppingCart.getGuid(), skuCode, supplier, option.getAttributeCode(), skuOption); return; } final BigDecimal toAddOption = toAdd.multiply(option.getQuantity()).stripTrailingZeros(); final String skuNameOption = new FailoverStringI18NModel( productSkuOption.getDisplayName(), productSkuOption.getName() ).getValue(shoppingCart.getCurrentLocale()); selectedOptions.put(skuOption, new Pair<>(skuNameOption, toAddOption)); } } } final String enforceGroup = configurable && StringUtils.isBlank(itemGroup) ? UUID.randomUUID().toString() : itemGroup; final boolean optionalGroupExists = configurable && shoppingCart.getCartItemList().stream().anyMatch(cartItem -> ShoppingCartUtils.isCartItem(cartItem, supplier, skuCode, enforceGroup)); if (optionalGroupExists) { LOG.debug("[{}] Unable to add item {} for supplier {} because item with this group already exists {}", shoppingCart.getGuid(), skuCode, supplier, enforceGroup); return; } shoppingCart.addProductSkuToCart(supplier, skuCode, skuName, toAdd, enforceGroup, configurable, false); if (configurable) { for (final Map.Entry<String, Pair<String, BigDecimal>> optItem : selectedOptions.entrySet()) { shoppingCart.addProductSkuToCart(supplier, optItem.getKey(), optItem.getValue().getFirst(), optItem.getValue().getSecond(), enforceGroup, false, true); } } recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); LOG.debug("[{}] Added {} item(s) of sku code {}", shoppingCart.getGuid(), toAdd, skuCode); } AddSkuToCartEventCommandImpl(final ShoppingCartCommandRegistry registry,
                                        final PriceResolver priceResolver,
                                        final PricingPolicyProvider pricingPolicyProvider,
                                        final ProductService productService,
                                        final ShopService shopService,
                                        final ProductQuantityStrategy productQuantityStrategy); }
AddSkuToCartEventCommandImpl extends AbstractSkuCartCommandImpl { @Override protected void execute(final MutableShoppingCart shoppingCart, final ProductSku productSku, final String skuCode, final String supplier, final String itemGroup, final BigDecimal qty, final Map<String, Object> parameters) { if (determineSkuPrice(shoppingCart, supplier, skuCode, BigDecimal.ONE) == null) { LOG.debug("[{}] Unable to add item {} for supplier {} because could not resolve price", shoppingCart.getGuid(), skuCode, supplier); return; } final long shopId = shoppingCart.getShoppingContext().getCustomerShopId(); final BigDecimal cartQty = shoppingCart.getProductSkuQuantity(supplier, skuCode); final BigDecimal toAdd = getQuantityValue(shopId, supplier, skuCode, qty, cartQty); String skuName = skuCode; boolean configurable = false; Map<String, Pair<String, BigDecimal>> selectedOptions = Collections.emptyMap(); if (productSku != null) { skuName = new FailoverStringI18NModel( productSku.getDisplayName(), productSku.getName() ).getValue(shoppingCart.getCurrentLocale()); final Product product = getProductService().getProductById(productSku.getProduct().getProductId()); if (product.getOptions().isConfigurable()) { configurable = true; selectedOptions = new LinkedHashMap<>(); final List<ProductOption> options = product.getOptions().getConfigurationOptionForSKU(productSku); for (final ProductOption option : options) { final String skuOption = (String) parameters.get(option.getAttributeCode()); if ((option.isMandatory() && StringUtils.isBlank(skuOption)) || (StringUtils.isNotBlank(skuOption) && !option.getOptionSkuCodes().contains(skuOption))) { LOG.debug("[{}] Unable to add item {} for supplier {} because option {} has invalid value {}", shoppingCart.getGuid(), skuCode, supplier, option.getAttributeCode(), skuOption); return; } if (StringUtils.isBlank(skuOption)) { continue; } final ProductSku productSkuOption = getProductService().getProductSkuByCode(skuOption); if (determineSkuPrice(shoppingCart, supplier, skuOption, BigDecimal.ONE) == null) { LOG.debug("[{}] Unable to add item {} for supplier {} because could not resolve price for option {} value {}", shoppingCart.getGuid(), skuCode, supplier, option.getAttributeCode(), skuOption); return; } final BigDecimal toAddOption = toAdd.multiply(option.getQuantity()).stripTrailingZeros(); final String skuNameOption = new FailoverStringI18NModel( productSkuOption.getDisplayName(), productSkuOption.getName() ).getValue(shoppingCart.getCurrentLocale()); selectedOptions.put(skuOption, new Pair<>(skuNameOption, toAddOption)); } } } final String enforceGroup = configurable && StringUtils.isBlank(itemGroup) ? UUID.randomUUID().toString() : itemGroup; final boolean optionalGroupExists = configurable && shoppingCart.getCartItemList().stream().anyMatch(cartItem -> ShoppingCartUtils.isCartItem(cartItem, supplier, skuCode, enforceGroup)); if (optionalGroupExists) { LOG.debug("[{}] Unable to add item {} for supplier {} because item with this group already exists {}", shoppingCart.getGuid(), skuCode, supplier, enforceGroup); return; } shoppingCart.addProductSkuToCart(supplier, skuCode, skuName, toAdd, enforceGroup, configurable, false); if (configurable) { for (final Map.Entry<String, Pair<String, BigDecimal>> optItem : selectedOptions.entrySet()) { shoppingCart.addProductSkuToCart(supplier, optItem.getKey(), optItem.getValue().getFirst(), optItem.getValue().getSecond(), enforceGroup, false, true); } } recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); LOG.debug("[{}] Added {} item(s) of sku code {}", shoppingCart.getGuid(), toAdd, skuCode); } AddSkuToCartEventCommandImpl(final ShoppingCartCommandRegistry registry,
                                        final PriceResolver priceResolver,
                                        final PricingPolicyProvider pricingPolicyProvider,
                                        final ProductService productService,
                                        final ShopService shopService,
                                        final ProductQuantityStrategy productQuantityStrategy); @Override String getCmdKey(); }
AddSkuToCartEventCommandImpl extends AbstractSkuCartCommandImpl { @Override protected void execute(final MutableShoppingCart shoppingCart, final ProductSku productSku, final String skuCode, final String supplier, final String itemGroup, final BigDecimal qty, final Map<String, Object> parameters) { if (determineSkuPrice(shoppingCart, supplier, skuCode, BigDecimal.ONE) == null) { LOG.debug("[{}] Unable to add item {} for supplier {} because could not resolve price", shoppingCart.getGuid(), skuCode, supplier); return; } final long shopId = shoppingCart.getShoppingContext().getCustomerShopId(); final BigDecimal cartQty = shoppingCart.getProductSkuQuantity(supplier, skuCode); final BigDecimal toAdd = getQuantityValue(shopId, supplier, skuCode, qty, cartQty); String skuName = skuCode; boolean configurable = false; Map<String, Pair<String, BigDecimal>> selectedOptions = Collections.emptyMap(); if (productSku != null) { skuName = new FailoverStringI18NModel( productSku.getDisplayName(), productSku.getName() ).getValue(shoppingCart.getCurrentLocale()); final Product product = getProductService().getProductById(productSku.getProduct().getProductId()); if (product.getOptions().isConfigurable()) { configurable = true; selectedOptions = new LinkedHashMap<>(); final List<ProductOption> options = product.getOptions().getConfigurationOptionForSKU(productSku); for (final ProductOption option : options) { final String skuOption = (String) parameters.get(option.getAttributeCode()); if ((option.isMandatory() && StringUtils.isBlank(skuOption)) || (StringUtils.isNotBlank(skuOption) && !option.getOptionSkuCodes().contains(skuOption))) { LOG.debug("[{}] Unable to add item {} for supplier {} because option {} has invalid value {}", shoppingCart.getGuid(), skuCode, supplier, option.getAttributeCode(), skuOption); return; } if (StringUtils.isBlank(skuOption)) { continue; } final ProductSku productSkuOption = getProductService().getProductSkuByCode(skuOption); if (determineSkuPrice(shoppingCart, supplier, skuOption, BigDecimal.ONE) == null) { LOG.debug("[{}] Unable to add item {} for supplier {} because could not resolve price for option {} value {}", shoppingCart.getGuid(), skuCode, supplier, option.getAttributeCode(), skuOption); return; } final BigDecimal toAddOption = toAdd.multiply(option.getQuantity()).stripTrailingZeros(); final String skuNameOption = new FailoverStringI18NModel( productSkuOption.getDisplayName(), productSkuOption.getName() ).getValue(shoppingCart.getCurrentLocale()); selectedOptions.put(skuOption, new Pair<>(skuNameOption, toAddOption)); } } } final String enforceGroup = configurable && StringUtils.isBlank(itemGroup) ? UUID.randomUUID().toString() : itemGroup; final boolean optionalGroupExists = configurable && shoppingCart.getCartItemList().stream().anyMatch(cartItem -> ShoppingCartUtils.isCartItem(cartItem, supplier, skuCode, enforceGroup)); if (optionalGroupExists) { LOG.debug("[{}] Unable to add item {} for supplier {} because item with this group already exists {}", shoppingCart.getGuid(), skuCode, supplier, enforceGroup); return; } shoppingCart.addProductSkuToCart(supplier, skuCode, skuName, toAdd, enforceGroup, configurable, false); if (configurable) { for (final Map.Entry<String, Pair<String, BigDecimal>> optItem : selectedOptions.entrySet()) { shoppingCart.addProductSkuToCart(supplier, optItem.getKey(), optItem.getValue().getFirst(), optItem.getValue().getSecond(), enforceGroup, false, true); } } recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); LOG.debug("[{}] Added {} item(s) of sku code {}", shoppingCart.getGuid(), toAdd, skuCode); } AddSkuToCartEventCommandImpl(final ShoppingCartCommandRegistry registry,
                                        final PriceResolver priceResolver,
                                        final PricingPolicyProvider pricingPolicyProvider,
                                        final ProductService productService,
                                        final ShopService shopService,
                                        final ProductQuantityStrategy productQuantityStrategy); @Override String getCmdKey();  }