Skip to content

Commit

Permalink
Improved: Allow multiple contactMech with same purpose on ShoppingCar…
Browse files Browse the repository at this point in the history
…t (OFBIZ-13199)

During the shopping cart process, you can add some contactMech to your order but only one by purpose.

For someone, it would be logical like SHIPPING_LOCATION (although we could sequence it) but for some case like ORDER_EMAIL it's seem more complicated.

No reason to block multiple email address that will receive order notification.

For that we authorize the shopping cart to load a unique list of contact mech id by purpose instead only one
  • Loading branch information
nmalin committed Jan 23, 2025
1 parent ffaa7ad commit e67062a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ public static Map<String, Object> getExpressCheckout(DispatchContext dctx, Map<S
return ServiceUtil.returnError(e.getMessage());
}
}
cart.addContactMech("ORDER_EMAIL", emailContactMechId);
cart.addContactMechId("ORDER_EMAIL", emailContactMechId);

// Phone number
String phoneNumber = decoder.get("PHONENUM");
Expand All @@ -508,7 +508,7 @@ public static Map<String, Object> getExpressCheckout(DispatchContext dctx, Map<S
try {
outMap = dispatcher.runSync("createUpdatePartyTelecomNumber", inMap);
phoneContactId = (String) outMap.get("contactMechId");
cart.addContactMech("PHONE_BILLING", phoneContactId);
cart.addContactMechId("PHONE_BILLING", phoneContactId);
} catch (GenericServiceException e) {
Debug.logError(e, MODULE);
}
Expand All @@ -517,7 +517,7 @@ public static Map<String, Object> getExpressCheckout(DispatchContext dctx, Map<S
String postalContactId = null;
boolean needsShippingPurpose = true;
// if the cart for some reason already has a billing address, we'll leave it be
boolean needsBillingPurpose = (cart.getContactMech("BILLING_LOCATION") == null);
boolean needsBillingPurpose = (cart.getContactMechId("BILLING_LOCATION") == null);
Map<String, Object> postalMap = new HashMap<>();
postalMap.put("toName", decoder.get("SHIPTONAME"));
postalMap.put("address1", decoder.get("SHIPTOSTREET"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Map createUpdateCustomerAndShippingAddress() {
result.shipToPhoneContactMechId = serviceResultCUPTN.contactMechId

if (shipToPhoneContactMechId) {
shoppingCart.addContactMech('PHONE_SHIPPING', shipToPhoneContactMechId)
shoppingCart.addContactMechId('PHONE_SHIPPING', shipToPhoneContactMechId)
}
// Create Update email address
Map createUpdatePartyEmailCtx = emailAddressCtx
Expand All @@ -98,10 +98,10 @@ Map createUpdateCustomerAndShippingAddress() {
result.emailContactMechId = serviceResultCUPEM.contactMechId
result.partyId = partyId
if (parameters.emailContactMechId) {
shoppingCart.addContactMech('ORDER_EMAIL', parameters.emailContactMechId)
shoppingCart.addContactMechId('ORDER_EMAIL', parameters.emailContactMechId)
}
shoppingCart.setUserLogin(userLogin, dispatcher)
shoppingCart.addContactMech('SHIPPING_LOCATION', parameters.shipToContactMechId)
shoppingCart.addContactMechId('SHIPPING_LOCATION', parameters.shipToContactMechId)
shoppingCart.setAllShippingContactMechId(parameters.shipToContactMechId)
shoppingCart.setOrderPartyId(partyId)
return result
Expand Down Expand Up @@ -149,7 +149,7 @@ Map createUpdateBillingAddressAndPaymentMethod() {
parameters.billToContactMechId = serviceResultCUBA.contactMechId
result.contactMechId = serviceResultCUBA.contactMechId
if (parameters.billToContactMechId) {
shoppingCart.addContactMech('BILLING_LOCATION', parameters.billToContactMechId)
shoppingCart.addContactMechId('BILLING_LOCATION', parameters.billToContactMechId)
}
// Create Update Billing Telecom Number
Map createUpdatePartyTelecomNumberCtx = billToPhoneContext
Expand All @@ -165,7 +165,7 @@ Map createUpdateBillingAddressAndPaymentMethod() {
String billToPhoneContactMechId = serviceResultCUPTN.contactMechId
result.billToPhoneContactMechId = serviceResultCUPTN.contactMechId
if (billToPhoneContactMechId) {
shoppingCart.addContactMech('PHONE_BILLING', billToPhoneContactMechId)
shoppingCart.addContactMechId('PHONE_BILLING', billToPhoneContactMechId)
}
// Create Update credit card
Map creditCartCtx = parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public class ShoppingCart implements Iterable<ShoppingCartItem>, Serializable {
private long nextGroupNumber = 1;
private List<CartPaymentInfo> paymentInfo = new LinkedList<>();
private List<CartShipInfo> shipInfo = new LinkedList<>();
private Map<String, String> contactMechIdsMap = new HashMap<>();
private Map<String, Set<String>> contactMechIdsMap = new HashMap<>();
private Map<String, String> orderAttributes = new HashMap<>();
private Map<String, Object> attributes = new HashMap<>(); // user defined attributes
// Lists of internal/public notes: when the order is stored they are transformed into OrderHeaderNotes
Expand Down Expand Up @@ -3465,28 +3465,41 @@ public BigDecimal getGiftCardPaymentPreferenceTotal() {
}

/** Add a contact mech to this purpose; the contactMechPurposeTypeId is required */
public void addContactMech(String contactMechPurposeTypeId, String contactMechId) {
public void addContactMechId(String contactMechPurposeTypeId, String contactMechId) {
if (contactMechPurposeTypeId == null) {
throw new IllegalArgumentException("You must specify a contactMechPurposeTypeId to add a ContactMech");
}
contactMechIdsMap.put(contactMechPurposeTypeId, contactMechId);
UtilMisc.addToSetInMap(contactMechId, contactMechIdsMap, contactMechPurposeTypeId);
}

/** Get the contactMechId for this cart given the contactMechPurposeTypeId */
public String getContactMech(String contactMechPurposeTypeId) {
return contactMechIdsMap.get(contactMechPurposeTypeId);
public String getContactMechId(String contactMechPurposeTypeId) {
return UtilValidate.isNotEmpty(getContactMechIds(contactMechPurposeTypeId))
? getContactMechIds(contactMechPurposeTypeId).get(0)
: null;
}

/** Remove the contactMechId from this cart given the contactMechPurposeTypeId */
public String removeContactMech(String contactMechPurposeTypeId) {
return contactMechIdsMap.remove(contactMechPurposeTypeId);
/** Get the contactMechIds list for this cart given the contactMechPurposeTypeId */
public List<String> getContactMechIds(String contactMechPurposeTypeId) {
Set<String> contactMechIds = contactMechIdsMap.get(contactMechPurposeTypeId);
return contactMechIds != null
? new ArrayList<>(contactMechIds)
: List.of();
}

/** Remove the contactMechIds list from this cart given the contactMechPurposeTypeId */
public List<String> removeContactMechId(String contactMechPurposeTypeId) {
Set<String> contactMechIds = contactMechIdsMap.remove(contactMechPurposeTypeId);
return contactMechIds != null
? new ArrayList<>(contactMechIds)
: List.of();
}

/**
* Gets order contact mech ids.
* @return the order contact mech ids
*/
public Map<String, String> getOrderContactMechIds() {
public Map<String, Set<String>> getOrderContactMechIds() {
return this.contactMechIdsMap;
}

Expand Down Expand Up @@ -4616,17 +4629,15 @@ public List<GenericValue> makeAllOrderItemSurveyResponses() {
public List<GenericValue> makeAllOrderContactMechs() {
List<GenericValue> allOrderContactMechs = new LinkedList<>();

Map<String, String> contactMechIds = this.getOrderContactMechIds();

Map<String, Set<String>> contactMechIds = this.getOrderContactMechIds();
if (contactMechIds != null) {
for (Map.Entry<String, String> entry : contactMechIds.entrySet()) {
GenericValue orderContactMech = getDelegator().makeValue("OrderContactMech");
orderContactMech.set("contactMechPurposeTypeId", entry.getKey());
orderContactMech.set("contactMechId", entry.getValue());
allOrderContactMechs.add(orderContactMech);
for (Map.Entry<String, Set<String>> entry : contactMechIds.entrySet()) {
entry.getValue().forEach(contactMechId ->
allOrderContactMechs.add(getDelegator().makeValue("OrderContactMech",
Map.of("contactMechPurposeTypeId", entry.getKey(),
"contactMechId", contactMechId))));
}
}

return allOrderContactMechs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public static Map<String, Object> loadCartFromOrder(DispatchContext dctx, Map<St
}
if (UtilValidate.isNotEmpty(orderContactMechs)) {
for (GenericValue orderContactMech : orderContactMechs) {
cart.addContactMech(orderContactMech.getString("contactMechPurposeTypeId"), orderContactMech.getString("contactMechId"));
cart.addContactMechId(orderContactMech.getString("contactMechPurposeTypeId"), orderContactMech.getString("contactMechId"));
}
}
List<GenericValue> orderItemShipGroupList = orh.getOrderItemShipGroups();
Expand Down

0 comments on commit e67062a

Please sign in to comment.