FreeShippingPromotionHandler.java
3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.collections.Predicate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.ValueMap
*/
package com.adobe.cq.commerce.impl.promotion;
import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.CommerceSession;
import com.adobe.cq.commerce.api.PriceInfo;
import com.adobe.cq.commerce.api.promotion.Promotion;
import com.adobe.cq.commerce.api.promotion.PromotionHandler;
import com.adobe.cq.commerce.common.PriceFilter;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.Currency;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.Predicate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ValueMap;
@Component(metatype=0)
@Service
@Properties(value={@Property(name="service.description", value={"Promotion handler which removes shipping costs from the cart"}), @Property(name="commerce.promotion.type", value={"/libs/commerce/components/promotion/freeshipping"})})
public class FreeShippingPromotionHandler
implements PromotionHandler {
@Override
public PriceInfo applyCartEntryPromotion(CommerceSession commerceSession, Promotion promotion, CommerceSession.CartEntry cartEntry) throws CommerceException {
return null;
}
@Override
public PriceInfo applyOrderPromotion(CommerceSession commerceSession, Promotion promotion) throws CommerceException {
return null;
}
@Override
public PriceInfo applyShippingPromotion(CommerceSession commerceSession, Promotion promotion) throws CommerceException {
List<PriceInfo> shipping = commerceSession.getCartPriceInfo(new PriceFilter("SHIPPING"));
if (!shipping.isEmpty()) {
return new PriceInfo(shipping.get(0).getAmount(), shipping.get(0).getCurrency());
}
return null;
}
@Deprecated
@Override
public String getMessage(SlingHttpServletRequest request, CommerceSession commerceSession, Promotion promotion) throws CommerceException {
return null;
}
@Override
public String getDescription(SlingHttpServletRequest request, CommerceSession commerceSession, Promotion promotion) throws CommerceException {
return promotion.getDescription();
}
@Override
public Map<Integer, String> getMessages(SlingHttpServletRequest request, CommerceSession commerceSession, Promotion promotion) throws CommerceException {
String message;
HashMap<Integer, String> messages = new HashMap<Integer, String>();
ValueMap config = promotion.getConfig();
List<PriceInfo> shipping = commerceSession.getCartPriceInfo(new PriceFilter("SHIPPING", "PRE_PROMO"));
message = shipping.isEmpty() || shipping.get(0).getAmount().equals(BigDecimal.ZERO) ? (String)config.get("defaultMessage", String.class) : ((message = (String)config.get("amountMessage", String.class)) != null ? MessageFormat.format(message, "", shipping.get(0).getFormattedString()) : (String)config.get("defaultMessage", String.class));
if (message != null) {
messages.put(-1, message);
}
return messages;
}
@Override
public void invalidateCaches() {
}
}