📄 shoppingcart.java
字号:
// set quantity to 0 to trigger necessary events
item.setQuantity(0.0, dispatcher, this);
}
/** Moves a line item to a differnt index. */
public void moveCartItem(int fromIndex, int toIndex) {
if (toIndex < fromIndex) {
cartLines.add(toIndex, cartLines.remove(fromIndex));
} else if (toIndex > fromIndex) {
cartLines.add(toIndex - 1, cartLines.remove(fromIndex));
}
}
/** Returns the number of items in the cart object. */
public int size() {
return cartLines.size();
}
/** Returns a Collection of items in the cart object. */
public List items() {
return cartLines;
}
/** Returns an iterator of cart items. */
public Iterator iterator() {
return cartLines.iterator();
}
/** Gets the userLogin associated with the cart; may be null */
public GenericValue getUserLogin() {
return this.userLogin;
}
public void setUserLogin(GenericValue userLogin, LocalDispatcher dispatcher) throws CartItemModifyException {
this.userLogin = userLogin;
this.handleNewUser(dispatcher);
}
public GenericValue getAutoUserLogin() {
return this.autoUserLogin;
}
public void setAutoUserLogin(GenericValue autoUserLogin, LocalDispatcher dispatcher) throws CartItemModifyException {
this.autoUserLogin = autoUserLogin;
if (getUserLogin() == null) {
this.handleNewUser(dispatcher);
}
}
public void handleNewUser(LocalDispatcher dispatcher) throws CartItemModifyException {
String partyId = this.getPartyId();
if (UtilValidate.isNotEmpty(partyId)) {
// recalculate all prices
Iterator cartItemIter = this.iterator();
while (cartItemIter.hasNext()) {
ShoppingCartItem cartItem = (ShoppingCartItem) cartItemIter.next();
cartItem.updatePrice(dispatcher, this);
}
// check all promo codes, remove on failed check
Iterator promoCodeIter = this.productPromoCodes.iterator();
while (promoCodeIter.hasNext()) {
String promoCode = (String) promoCodeIter.next();
String checkResult = ProductPromoWorker.checkCanUsePromoCode(promoCode, partyId, this.getDelegator());
if (checkResult != null) {
promoCodeIter.remove();
Debug.logWarning("On user change promo code was removed because: " + checkResult, module);
}
}
// rerun promotions
ProductPromoWorker.doPromotions(this, dispatcher);
}
}
public String getWebSiteId() {
return this.webSiteId;
}
public void setWebSiteId(String webSiteId) {
this.webSiteId = webSiteId;
}
public String getPartyId() {
String partyId = null;
if (partyId == null && getUserLogin() != null) {
partyId = getUserLogin().getString("partyId");
}
if (partyId == null && getAutoUserLogin() != null) {
partyId = getAutoUserLogin().getString("partyId");
}
return partyId;
}
public Double getPartyDaysSinceCreated(Timestamp nowTimestamp) {
String partyId = this.getPartyId();
if (UtilValidate.isEmpty(partyId)) {
return null;
}
try {
GenericValue party = this.getDelegator().findByPrimaryKeyCache("Party", UtilMisc.toMap("partyId", partyId));
if (party == null) {
return null;
}
Timestamp createdDate = party.getTimestamp("createdDate");
if (createdDate == null) {
return null;
}
double diffMillis = nowTimestamp.getTime() - createdDate.getTime();
// millis per day: 1000.0 * 60.0 * 60.0 * 24.0 = 86400000.0
return new Double((diffMillis) / 86400000.0);
} catch (GenericEntityException e) {
Debug.logError(e, "Error looking up party when getting createdDate", module);
return null;
}
}
// =======================================================================
// Methods for cart fields
// =======================================================================
/** Clears out the cart. */
public void clear() {
this.firstAttemptOrderId = null;
this.poNumber = null;
this.orderId = null;
this.orderShipmentPreference.remove("shippingInstructions");
this.orderShipmentPreference.remove("maySplit");
this.orderShipmentPreference.remove("giftMessage");
this.orderShipmentPreference.remove("isGift");
this.orderAdditionalEmails = null;
this.freeShippingProductPromoActions.clear();
this.productPromoUseInfoList.clear();
this.productPromoCodes.clear();
this.clearPaymentMethodIds();
this.clearPaymentMethodTypeIds();
this.adjustments.clear();
this.expireSingleUsePayments();
this.cartLines.clear();
}
private void expireSingleUsePayments() {
Iterator i = singleUsePaymentIds.iterator();
Timestamp now = UtilDateTime.nowTimestamp();
while (i.hasNext()) {
String paymentMethodId = (String) i.next();
GenericValue paymentMethod = null;
try {
paymentMethod = delegator.findByPrimaryKey("PaymentMethod", UtilMisc.toMap("paymentMethodId", paymentMethodId));
} catch (GenericEntityException e) {
Debug.logError(e, "ERROR: Unable to get payment method record to expire : " + paymentMethodId, module);
}
if (paymentMethod != null) {
paymentMethod.set("thruDate", now);
try {
paymentMethod.store();
} catch (GenericEntityException e) {
Debug.logError(e, "Unable to store single use PaymentMethod record : " + paymentMethod, module);
}
} else {
Debug.logError("ERROR: Received back a null payment method record for expired ID : " + paymentMethodId, module);
}
}
}
/** Sets the order type. */
public void setOrderType(String orderType) {
this.orderType = orderType;
}
/** Returns the order type. */
public String getOrderType() {
return this.orderType;
}
/** Sets the PO Number in the cart. */
public void setPoNumber(String poNumber) {
this.poNumber = poNumber;
}
/** Returns the po number. */
public String getPoNumber() {
return poNumber;
}
/** Checks to see if a Payment Method is selected. */
public boolean isPaymentMethodSelected(String paymentMethodId) {
if (paymentMethodId != null) {
return this.paymentMethodIds.contains(paymentMethodId);
} else {
return false;
}
}
/** Sets the amount of a Payment Method. */
public void setPaymentMethodAmount(String paymentMethodId, Double amount, boolean isSingleUse) {
if (paymentMethodId != null) {
if (!this.paymentMethodIds.contains(paymentMethodId)) {
this.paymentMethodIds.add(paymentMethodId);
}
this.paymentMethodAmounts.put(paymentMethodId, amount);
}
if (isSingleUse) {
this.singleUsePaymentIds.add(paymentMethodId);
}
}
public void setPaymentMethodAmount(String paymentMethodId, Double amount) {
setPaymentMethodAmount(paymentMethodId, amount, false);
}
/** Returns the Payment Method Ids. */
public List getPaymentMethodIds() {
return paymentMethodIds;
}
/** Clears the list of Payment Method Ids. */
public void clearPaymentMethodIds() {
this.paymentMethodIds.clear();
this.paymentMethodAmounts.clear();
}
/** Clears a specific Payment Method Id. */
public void clearPaymentMethodId(String paymentMethodId) {
this.paymentMethodIds.remove(paymentMethodId);
this.paymentMethodAmounts.remove(paymentMethodId);
}
/** Add the Payment Method Type Id to the cart. */
public void addPaymentMethodTypeId(String paymentMethodTypeId) {
addPaymentMethodTypeId(paymentMethodTypeId, null);
}
/** Add the Payment Method Type Id with amount to the cart. */
public void addPaymentMethodTypeId(String paymentMethodTypeId, Double amount) {
if (paymentMethodTypeId != null) {
this.paymentMethodTypeIds.add(paymentMethodTypeId);
this.paymentMethodTypeAmounts.put(paymentMethodTypeId, amount);
}
}
/** Returns set amount of the Payment Method. */
public Double getPaymentMethodAmount(String paymentMethodId) {
if (this.paymentMethodAmounts.get(paymentMethodId) != null) {
return (Double) this.paymentMethodAmounts.get(paymentMethodId);
} else {
return new Double(0.00);
}
}
/** Returns the total amount of the selected Payment Method(s). */
public double getSelectedPaymentMethodsTotal() {
double paymentMethodsTotal = 0.00;
Iterator i = this.getPaymentMethodIds().iterator();
while (i.hasNext()) {
String paymentMethodId = (String) i.next();
Double paymentTotal = (Double) this.paymentMethodAmounts.get(paymentMethodId);
if (paymentTotal != null) {
paymentMethodsTotal += paymentTotal.doubleValue();
}
}
return paymentMethodsTotal;
}
/** Returns a list of PaymentMethod value objects selected in the cart. */
public List getSelectedPaymentMethods() {
List ids = getPaymentMethodIds();
List methods = new ArrayList();
try {
Iterator i = ids.iterator();
while (i.hasNext()) {
String id = (String) i.next();
GenericValue paymentMethod = this.getDelegator().findByPrimaryKeyCache("PaymentMethod", UtilMisc.toMap("paymentMethodId", id));
methods.add(paymentMethod);
}
} catch (GenericEntityException e) {
Debug.logError(e, "Unable to get selected payment methods from the database", module);
return null;
}
return methods;
}
/** Returns the Payment Method Ids. */
public List getPaymentMethodTypeIds() {
return paymentMethodTypeIds;
}
/** Clears the list of Payment Method Type Ids. */
public void clearPaymentMethodTypeIds() {
this.paymentMethodTypeIds.clear();
this.paymentMethodTypeAmounts.clear();
}
/** Sets the billing account id string. */
public void setBillingAccount(String billingAccountId, double amount) {
this.billingAccountId = billingAccountId;
this.billingAccountAmt = amount;
}
/** Returns the billing message string. */
public String getBillingAccountId() {
return this.billingAccountId;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -