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

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1493512 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jacques Le Roux
2013-06-16 14:01:56 +00:00
parent 85f3137230
commit 7bfc413625
6 changed files with 88 additions and 0 deletions
@@ -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>
@@ -3150,6 +3150,21 @@ public class ShoppingCart implements Iterable<ShoppingCartItem>, Serializable {
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();
@@ -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);
@@ -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>
@@ -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>
@@ -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">