Author: jleroux Date: Sun Jun 16 14:01:56 2013 New Revision: 1493512 URL: http://svn.apache.org/r1493512 Log: A slightly modified patch from Ankit Jain from a previous work by Nandani Aggarwal for "No method to remove a promo code from the shopping cart" https://issues.apache.org/jira/browse/OFBIZ-4291
When working with promotions and promo codes in the shopping cart, it seems not possible to remove _one_ specific promo code from the cart neither through the ShoppingCart and its helpers and services, nor through the ProductPromoWorker. ShoppingCart has the method {{addProductPromoCode(String productPromoCodeId, LocalDispatcher dispatcher)}} but no method to remove the added PromoCode. Is there any other worker, service or event that can be called to do this? And if not, is there a specifc reason (maybe relating to promotions application rules) for not implementing this? Current provided method of dealing with removal of promotions is a so called brute force approach where removing all and adding not to be removed promotions again. jleroux: * in ecommerce no needs to be authentified to be abble to remove a promo code * I also added a French label Modified: ofbiz/trunk/applications/order/config/OrderUiLabels.xml ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java ofbiz/trunk/applications/order/webapp/ordermgr/WEB-INF/controller.xml ofbiz/trunk/applications/order/webapp/ordermgr/entry/cart/promoUseDetailsInline.ftl ofbiz/trunk/specialpurpose/ecommerce/webapp/ecommerce/WEB-INF/controller.xml Modified: ofbiz/trunk/applications/order/config/OrderUiLabels.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/config/OrderUiLabels.xml?rev=1493512&r1=1493511&r2=1493512&view=diff ============================================================================== --- ofbiz/trunk/applications/order/config/OrderUiLabels.xml (original) +++ ofbiz/trunk/applications/order/config/OrderUiLabels.xml Sun Jun 16 14:01:56 2013 @@ -3018,6 +3018,10 @@ <value xml:lang="zh">å 容</value> <value xml:lang="zh_TW">å §å®¹</value> </property> + <property key="OrderRemovePromotion"> + <value xml:lang="en">Remove Promotion</value> + <value xml:lang="fr">Supprimer la promotion</value> + </property> <property key="OrderContinueToFinalOrderReview"> <value xml:lang="cs">Dále k závÄreÄnému pÅehledu objednávky</value> <value xml:lang="de">Weiter zur abschlieÃenden Bestellüberprüfung</value> Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java?rev=1493512&r1=1493511&r2=1493512&view=diff ============================================================================== --- ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java (original) +++ ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java Sun Jun 16 14:01:56 2013 @@ -3150,6 +3150,21 @@ public class ShoppingCart implements Ite this.productPromoUseInfoList.add(new ProductPromoUseInfo(productPromoId, productPromoCodeId, totalDiscountAmount, quantityLeftInActions)); } + public void removeProductPromoUse(String productPromoId) { + if (!productPromoId.isEmpty()) { + int index = -1; + for (ProductPromoUseInfo productPromoUseInfo : this.productPromoUseInfoList) { + if (productPromoId.equals(productPromoUseInfo.productPromoId)) { + index = this.productPromoUseInfoList.indexOf(productPromoUseInfo); + break; + } + } + if (index != -1) { + this.productPromoUseInfoList.remove(index); + } + } + } + public void clearProductPromoUseInfo() { // clear out info for general promo use this.productPromoUseInfoList.clear(); Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java?rev=1493512&r1=1493511&r2=1493512&view=diff ============================================================================== --- ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java (original) +++ ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java Sun Jun 16 14:01:56 2013 @@ -23,6 +23,7 @@ import java.math.MathContext; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Enumeration; +import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -53,6 +54,7 @@ import org.ofbiz.entity.GenericValue; import org.ofbiz.entity.condition.EntityCondition; import org.ofbiz.entity.condition.EntityOperator; import org.ofbiz.entity.util.EntityUtil; +import org.ofbiz.order.shoppingcart.ShoppingCart.ProductPromoUseInfo; import org.ofbiz.order.shoppingcart.product.ProductPromoWorker; import org.ofbiz.product.catalog.CatalogWorker; import org.ofbiz.product.config.ProductConfigWorker; @@ -96,6 +98,58 @@ public class ShoppingCartEvents { return "success"; } + public static String removePromotion(HttpServletRequest request,HttpServletResponse response) { + LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); + ShoppingCart cart = getCartObject(request); + String promoCodeId = (String) request.getParameter("promoCode"); + String result = "error"; + + if (!promoCodeId.isEmpty()) { + cart.getProductPromoCodesEntered().clear(); + GenericValue productPromoCode = null; + try { + productPromoCode = dispatcher.getDelegator().findOne("ProductPromoCode", UtilMisc.toMap("productPromoCodeId", promoCodeId), false); + if (!productPromoCode.isEmpty()) { + String productPromoId = productPromoCode.getString("productPromoId"); + GenericValue productPromoAction = null; + Map<String, String> productPromoActionMap = new HashMap<String, String>(); + productPromoActionMap.put("productPromoId", productPromoId); + productPromoActionMap.put("productPromoRuleId", "01"); + productPromoActionMap.put("productPromoActionSeqId", "01"); + + productPromoAction = dispatcher.getDelegator().findOne("ProductPromoAction", productPromoActionMap, false); + if (!productPromoAction.isEmpty()) { + int index = cart.getAdjustmentPromoIndex(productPromoId); + /*Remove order adjustment*/ + if (index != -1) { + cart.removeAdjustment(index); + result = "success"; + } + + /*Remove product adjustment*/ + for(ShoppingCartItem checkItem : cart) { + List<GenericValue> itemAdjustments = checkItem.getAdjustments(); + if (!itemAdjustments.isEmpty()) { + index = 0; + for (GenericValue adjustment : itemAdjustments ) { + if(adjustment.get("productPromoId").equals(productPromoId)) { + checkItem.getAdjustments().remove(index); + result = "success"; + } + index++; + } + } + } + cart.removeProductPromoUse(productPromoId); + } + } + } catch (GenericEntityException e) { + Debug.logError(e.getMessage(), module); + } + } + return result; + } + public static String addItemGroup(HttpServletRequest request, HttpServletResponse response) { ShoppingCart cart = getCartObject(request); Map<String, Object> parameters = UtilHttp.getParameterMap(request); Modified: ofbiz/trunk/applications/order/webapp/ordermgr/WEB-INF/controller.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/webapp/ordermgr/WEB-INF/controller.xml?rev=1493512&r1=1493511&r2=1493512&view=diff ============================================================================== --- ofbiz/trunk/applications/order/webapp/ordermgr/WEB-INF/controller.xml (original) +++ ofbiz/trunk/applications/order/webapp/ordermgr/WEB-INF/controller.xml Sun Jun 16 14:01:56 2013 @@ -663,6 +663,12 @@ under the License. <security https="true" auth="true"/> <response name="success" type="view" value="showPromotionDetails"/> </request-map> + <request-map uri="removePromotion"> + <security https="true" auth="true"/> + <event type="java" path="org.ofbiz.order.shoppingcart.ShoppingCartEvents" invoke="removePromotion"/> + <response name="success" type="request" value="orderentry"/> + <response name="error" type="request" value="orderentry"/> + </request-map> <request-map uri="quickadd"> <response name="success" type="view" value="quickadd"/> </request-map> Modified: ofbiz/trunk/applications/order/webapp/ordermgr/entry/cart/promoUseDetailsInline.ftl URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/webapp/ordermgr/entry/cart/promoUseDetailsInline.ftl?rev=1493512&r1=1493511&r2=1493512&view=diff ============================================================================== --- ofbiz/trunk/applications/order/webapp/ordermgr/entry/cart/promoUseDetailsInline.ftl (original) +++ ofbiz/trunk/applications/order/webapp/ordermgr/entry/cart/promoUseDetailsInline.ftl Sun Jun 16 14:01:56 2013 @@ -27,6 +27,9 @@ under the License. ${uiLabelMap.OrderPromotion} <a href="<@ofbizUrl>showPromotionDetails?productPromoId=${productPromoUseInfo.productPromoId?if_exists}</@ofbizUrl>" class="button">${uiLabelMap.CommonDetails}</a> <#if productPromoUseInfo.productPromoCodeId?has_content> - ${uiLabelMap.OrderWithPromoCode} [${productPromoUseInfo.productPromoCodeId}]</#if> <#if (productPromoUseInfo.totalDiscountAmount != 0)> - ${uiLabelMap.CommonTotalValue} <@ofbizCurrency amount=(-1*productPromoUseInfo.totalDiscountAmount) isoCode=shoppingCart.getCurrency()/></#if> + <#if productPromoUseInfo.productPromoCodeId?has_content> + <a href="<@ofbizUrl>removePromotion?promoCode=${productPromoUseInfo.productPromoCodeId?if_exists}</@ofbizUrl>" class="button">${uiLabelMap.OrderRemovePromotion}</a> + </#if> </li> <#if (productPromoUseInfo.quantityLeftInActions > 0)> <li>- Could be used for ${productPromoUseInfo.quantityLeftInActions} more discounted item<#if (productPromoUseInfo.quantityLeftInActions > 1)>s</#if> if added to your cart.</li> Modified: ofbiz/trunk/specialpurpose/ecommerce/webapp/ecommerce/WEB-INF/controller.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/ecommerce/webapp/ecommerce/WEB-INF/controller.xml?rev=1493512&r1=1493511&r2=1493512&view=diff ============================================================================== --- ofbiz/trunk/specialpurpose/ecommerce/webapp/ecommerce/WEB-INF/controller.xml (original) +++ ofbiz/trunk/specialpurpose/ecommerce/webapp/ecommerce/WEB-INF/controller.xml Sun Jun 16 14:01:56 2013 @@ -321,6 +321,12 @@ under the License. <security https="false" auth="false"/> <response name="success" type="view" value="showPromotionDetails"/> </request-map> + <request-map uri="removePromotion"> + <security https="true" auth="false"/> + <event type="java" path="org.ofbiz.order.shoppingcart.ShoppingCartEvents" invoke="removePromotion"/> + <response name="success" type="view" value="showcart"/> + <response name="error" type="view" value="showcart"/> + </request-map> <!-- Start Anonymous checkout requests --> <request-map uri="setCustomer">