In Craft Commerce it is possible to define your own shipping methods within a plugin.
The plugin would provide shipping methods the customer can choose from. In addition, each Shipping method would need one or more shipping rule which allows the shipping method to match the order and be available for the current order, as well as provide the associated shipping costs.
You can provide one or more shipping methods with the following method in their main plugin class:
public function commerce_registerShippingMethods()
return [new CustomShipper_CourierDeliveryMethod()];
The plugin can provide one or more shipping method classes with the following interface:
# Shipping Method Interface
The shipping method interface requires a class with the following methods:
Returns the type of shipping method. This might be the name of the plugin or provider.
The core shipping methods have type:
Custom. This is shown in the control panel only.
This must return null.
Returns the name of this shipping method as displayed to the customer and in the control panel.
This is the handle added to the order for the chosen shipping method.
Returns a control panel URL to a place where you can configure this shipping method’s rules. Return an empty string if the method has no link.
Returns an array of rules that meet the
ShippingRules interface. (see below)
Is this shipping method available to the customer to select.
# Shipping Rules Interface
A shipping method returns an array of rules objects. The shipping engine goes through each rule and calls
matchOrder(). It expects a
false returned if this shipping method can be applied to the order/cart. The matched rule also returns the costs to the cart if the rule matches.
These are the methods required for the shipping rule objects:
Returns the unique handle of this shipping rule
Return a boolean. Is this rule a match on the order? If false is returned, the shipping engine tries the next rule on the current shipping method. If all rules return false, the shipping method is not available for selection by the customer on the order.
Is this shipping rule enabled, if not, the matchOrder() is not attempted.
Stores this data as JSON on the order’s shipping adjustment. For example, you might include all data used to determine the rule matched.
Returns the percentage rate that is multiplied per line item subtotal. Zero will not make any changes
Returns the flat rate that is multiplied per qty. Zero will not make any changes.
Returns the rate that is multiplied by the line item’s weight. Zero will not make any changes.
Returns a base shipping cost. This is added at the order level. Zero will not make any changes.
Returns a max cost this rule should ever apply. If the total of your rates as applied to the order are greater than this, the baseShippingCost on the order is modified to meet this max rate.
Returns a min cost this rule should have applied. If the total of your rates as applied to the order are less than this, the baseShippingCost on the order is modified to meet this min rate. Zero will not make any changes.
Returns a readable description of the rates applied by this rule.