There are three steps to setting up the various shipping methods: configuring the specific calculator instances, adding these calculator instances to our ShippingPricingEngine
, and finally, displaying these shipping methods to the customer via AvailableShippingMethods
.
Configuring Shipping Calculators
In the Pioneer store, we used the atg.commerce.pricing.FixedPriceShippingCalculator
for the Ground and Two Day shipping methods and atg.commerce.pricing.PriceRangeShippingCalculator
for Next Day shipping. We created properties files to generate instances of these calculators. For example, for the Ground shipping method, we created the Ground.properties
file with the following configuration:
# Define a Ground shipping method $class=atg.commerce.pricing.FixedPriceShippingCalculator # name of shipping method shippingMethod=Ground # cost of shipping method amount=5.0
This properties file creates an instance of a shipping calculator and sets the properties of that new shipping calculator. It gives it a method name (which is exposed to the user when they are selecting their shipping method) and the fixed cost of shipping. In this case, when the calculator is exposed to the user as a shipping method it has the name “Ground” associated with it and the cost is $5.
We also chose to use another instance of the FixedPriceShippingCalculator
for the Pioneer store to set the cost of all Two Day shipments to $20. This is what the properties file looks like:
# Define a Two day shipping method $class=atg.commerce.pricing.FixedPriceShippingCalculator # name of this shipping method shippingMethod=Two Day # cost of shipping method amount=20.0
Finally, we created a Next Day shipping method using an instance of the PriceRangeShippingCalculator
. The PriceRangeShippingCalculator
sets the cost of shipping based upon a range of prices. Here is the properties file that we created:
# Define a Next Day shipping method $class=atg.commerce.pricing.PriceRangeShippingCalculator # name of shipping method shippingMethod=Next Day # range of costs ranges=00.00:10.00:10.00,\ 10.01:30.00:15.00,\ 30.01:50.00:20.00,\ 50.01:70.00:25.00,\ 70.01:100.00:30.00,\ 100.01:MAX_VALUE:40.00
The first line of the ranges property states, “if the cost is between $0 and $10, set the price to $10.” The next line states, “if the price is between $10.01 and $30 set the price to $15.” This allows the cost of shipping to vary based upon the amount of the order.
We created these properties files at the Nucleus namespace at:
/atg/commerce/pricing/shipping/Ground.properties
/atg/commerce/pricing/shipping/TwoDay.properties
/atg/commerce/pricing/shipping/NextDay.properties
Adding Calculators to Pricing Engine
The ShippingPricingEngine
is a structural component that does not actually calculate shipping prices itself. The actual price calculations are performed by the Pricing Calculator components that are configured as the preCalculators
and postCalculators
properties of the ShippingPricingEngine
. This component is located at /atg/commerce/pricing/ShippingPricingEngine
.
# Calculators that ShippingPricingEngine will use to determine shipping preCalculators=shipping/Ground,\ shipping/NextDay,\ shipping/TwoDay
The values shipping/Ground
and shipping/NextDay
and shipping/TwoDay
are Nucleus references to the calculators that were defined above.
Enabling Customers to Choose Shipping Methods
During the checkout process, the customer must choose from a list of available shipping methods. Then, the selected method is associated with the shipping group selected in the customer’s order.
To display the various shipping methods that a customer can choose in the Pioneer Cycling store, we used the servlet bean located at /atg/commerce/pricing/AvailableShippingMethods
. It renders an array of the availableShippingMethods
.
We used the AvailableShippingMethods
servlet bean instead of accessing the availableMethods
property of the ShippingPricingEngine
for several reasons. Most importantly, the AvailableShippingMethods
servlet bean determines which shipping methods are available for a particular shipping group. For example, an order going to Alaska might not be able to be shipped overnight.
<droplet bean="AvailableShippingMethods"> <param name="shippingGroup" value="bean:ShoppingCartModifier.shippingGroup"> <param name="pricingModels" value="bean:UserPricingModels.shippingPricingModels"> <param name="profile" value="bean:Profile"> <oparam name="output"> <select bean="ShoppingCartModifier.shippingGroup.shippingMethod"> <droplet bean="ForEach"> <param name="array" value="param:availableShippingMethods"> <param name="elementName" value="method"> <oparam name="output"> <option value="param:method"><valueof param="method"></valueof> </oparam> </droplet> </select> </oparam> </droplet>
This JSP does several things. First, it exposes the list of available shipping methods via the parameter availableShippingMethods
of the AvailableShippingMethods
servlet bean. Second, it iterates through this array using a ForEach
component. Third, it sets the name of the shipping method that the user selects in the shippingGroup
. In this case, it is the single shippingGroup
that is created in an Order. If there was more than one shipping group, like on some of the Pioneer Cycling store checkout pages (shipping_info_multiple.jsp
) then the correct shippingGroup
would need to be referenced by iterating through the list of shipping groups in the order, and calling the AvailableShippingMethods
servlet bean for each of them.