Announcement: You can find the guides for Commerce 7.5 and later on the new Elastic Path Documentation site. This Developer Center contains the guides for Commerce 6.13.0 through 7.4.1.Visit new site

This version of Elastic Path Commerce is no longer supported or maintained. To upgrade to the latest version, contact your Elastic Path representative.

Creating an Implementation of Shipping Calculation API

Creating an Implementation of Shipping Calculation API

Elastic Path provides new plugins, such as shipping-calculation-connectivity-api and shipping-calculation-plugin-epcommerce, to support shipping calculation API framework. The shipping-calculation-connectivity-api bundle defines the interfaces that a new plugin needs to implement. The custom plugin replaces the shipping-calculation-plugin-epcommerce bundle.

To implement a new plugin, you must replace the following dependency with the new dependencies in the cortex and cm-libs module's POM.xml:
<dependency>
 <groupId>com.elasticpath</groupId>
 <artifactId>shipping-calculation-plugin-epcommerce</artifactId>
 </dependency>
 <dependency>
 <groupId>com.elasticpath</groupId>
 <artifactId>shipping-calculation-epcommerce</artifactId>
 </dependency>
  1. Create a new module in the extensions module for your shipping calculation API called Shipping Calculation Plugins:
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <parent>
          <groupId>com.elasticpath.extensions</groupId>
          <artifactId>ext-commerce-engine-parent</artifactId>
          <version>0-SNAPSHOT</version>
       </parent>
       <modelVersion>4.0.0</modelVersion>
     
       <name>Shipping Calculation Plugins</name>
       <artifactId>ext-shipping-calculation-plugins-parent</artifactId>
       <packaging>pom</packaging>
     
       <modules>
          <module>Name of the custom plugin folder</module>
       </modules>
    </project>
  2. Add Shipping Calculation Plugins as a module in the extensions/pom.xml file.
  3. Create a directory within the Shipping Calculation Plugins directory for your specific plugin, such as custom_shipping_calculation_plugin.
  4. Add the following POM file to the plugin:
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<parent>
    		<artifactId>core</artifactId>
    		<groupId>com.elasticpath</groupId>
    		<version>0-SNAPSHOT</version>
    	</parent>
    
    
    	<name>EP Shipping Calculation Plugin</name>
    
    	<dependencies>
    		<dependency>
    			<groupId>com.elasticpath</groupId>
    			<artifactId>ep-money</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>com.elasticpath</groupId>
    			<artifactId>shipping-calculation-connectivity-api</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>com.elasticpath</groupId>
    			<artifactId>shipping-calculation-epcommerce</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>commons-collections</groupId>
    			<artifactId>commons-collections</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>log4j</groupId>
    			<artifactId>log4j</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>com.elasticpath</groupId>
    			<artifactId>ep-test-utils</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.assertj</groupId>
    			<artifactId>assertj-core</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.mockito</groupId>
    			<artifactId>mockito-core</artifactId>
    		</dependency>
    	</dependencies>
    
    	<build>
    		<plugins>
    			<plugin>
    				<artifactId>maven-checkstyle-plugin</artifactId>
    			</plugin>
    			<plugin>
    				<artifactId>maven-pmd-plugin</artifactId>
    			</plugin>
    			<plugin>
    				<groupId>org.apache.felix</groupId>
    				<artifactId>maven-bundle-plugin</artifactId>
    				<extensions>true</extensions>
    			</plugin>
    		</plugins>
    	</build>
    </project>
    
  5. Create standard Maven directories in the following structure in this module:
    src/
      main/
        java/
        resources/
      test/
        java/
        resources/
  6. Create a class to extend AbstractShippingCalculationPlugin.
    /*
    package com.elasticpath.shipping.connectivity.spi;
    
    import java.io.Serializable;
    
    import com.elasticpath.shipping.connectivity.spi.capability.ShippingCalculationCapability;
    
    /**
     * Service Provider Interface for extension classes implementing shipping calculation plugins.
     */
    public abstract class AbstractShippingCalculationPlugin implements ShippingCalculationPlugin, Serializable {
    	/**
    	 * Serial Version UID.
    	 */
    	private static final long serialVersionUID = 1L;
    
    	@Override
    	public <T extends ShippingCalculationCapability> T getCapability(final Class<T> capability) {
    		if (hasCapability(capability)) {
    			return capability.cast(this);
    		}
    		return null;
    	}
    
    	@Override
    	public <T extends ShippingCalculationCapability> boolean hasCapability(final Class<T> capability) {
    		return capability.isAssignableFrom(this.getClass());
    	}
    
    }
    
  7. Implement ShippingCostCalculationCapability, ShippingOptionListCapability, ShippingOptionListAllCapability, and ShippingOptionListPerDestinationCapability.
    Implement shipping calculation capability as in the following example:
    public Money calculateShippingCost(final ShippingServiceLevel shippingServiceLevel,
    										  final Collection<? extends ShippableItem> shippableItems,
    										  final Money shippableItemsSubtotal,
    										  final Currency currency) {
    		return shippingServiceLevel.getShippingCostCalculationMethod()
    				.calculateShippingCost(shippableItems, shippableItemsSubtotal, currency, productSkuLookup);
    	}
  8. Add the plugin into the Cortex and Commerce Manager modules.