Reference information: Shipment method plugins feature walkthrough

Edit on GitHub

The main concerns regarding shipping services are:

  • Availability: Is the shipping method available to deliver the order?
  • Price: How is the delivery price calculated ?
  • Delivery time: When will the order be delivered ?

For each of these concerns, an optional plugin is linked to each shipping method:

  • Availability Plugin: Returns a boolean value which implies if the active shipping method is available and should be visible to the customers in the list of available shipping services.
  • Price Calculation Plugin: Shipping services can consider different criteria in calculating the price for delivery (such as size of the package, weight, etc.). When a price plugin is paired to a shipping method, the related Zed Admin UI preconfigured prices are omitted.
  • Delivery Time Plugin: The estimated delivery time information of the purchased items is important for the customers. The delivery time can vary depending on region, shipping service type, or day of week. Delivery time is measured in seconds as integer (for example,1 day = 86400; 5 days = 5 * 86400).

Availability Plugin

For each availability plugin linked to a shipment method, a class with the same name must exist on the project side in the Shipment module (Pyz/Zed/Shipment/Communication/Plugin/Availability).

The class must implement ShipmentMethodAvailabilityPluginInterface and must extend the AbstractPlugin class, as in the example below:

<?php
namespace Pyz/Zed/Shipment/Communication/Plugin/Availability;
use Generated/Shared/Transfer/QuoteTransfer;
use Generated/Shared/Transfer/ShipmentGroupTransfer;
use Spryker/Zed/Kernel/Communication/AbstractPlugin;
use Spryker/Zed/ShipmentExtension/Dependency/Plugin/ShipmentMethodAvailabilityPluginInterface;
class DHLExpressPlugin extends AbstractPlugin implements ShipmentMethodAvailabilityPluginInterface
{
    /**
     * Specification:
     *  - Checks shipment method availability for shipment group.
     *
     * @api
     *
     * @param /Generated/Shared/Transfer/ShipmentGroupTransfer $shipmentGroupTransfer
     * @param /Generated/Shared/Transfer/QuoteTransfer $quoteTransfer
     *
     * @return bool
     */
    public function isAvailable(ShipmentGroupTransfer $shipmentGroupTransfer, QuoteTransfer $quoteTransfer): bool
    {
        return true;
    }
}

Price Calculation plugin

For each price calculation plugin linked to a shipment method, a class with the same name must exist on the project side in the Shipment module (Pyz/Zed/Shipment/Communication/Plugin/PriceCalculation). The class must implement ShipmentMethodPricePluginInterface and must extend the AbstractPlugin class, as in the example below:

<?php
namespace Pyz/Zed/Shipment/Communication/Plugin/PriceCalculation;
use Generated/Shared/Transfer/QuoteTransfer;
use Generated/Shared/Transfer/ShipmentGroupTransfer;
use Spryker/Zed/Kernel/Communication/AbstractPlugin;
use Spryker/Zed/ShipmentExtension/Dependency/Plugin/ShipmentMethodPricePluginInterface;
class DHLExpressPlugin extends AbstractPlugin implements ShipmentMethodPricePluginInterface
{
    /**
     * Specification:
     *  - Returns shipment method price for shipment group.
     *
     * @api
     *
     * @param /Generated/Shared/Transfer/ShipmentGroupTransfer $shipmentGroupTransfer
     * @param /Generated/Shared/Transfer/QuoteTransfer $quoteTransfer
     *
     * @return int
     */
    public function getPrice(ShipmentGroupTransfer $shipmentGroupTransfer, QuoteTransfer $quoteTransfer): int
    {
        return 0;
    }
}

Delivery Time plugin

For each availability plugin linked to a shipment method, a class with the same name must exist on the project side in the Shipment module (Pyz/Zed/Shipment/Communication/Plugin/DeliveryTime). The class must implement ShipmentMethodDeliveryTimePluginInterface and must extend the AbstractPlugin class, as in the example below:

<?php
namespace Pyz/Zed/Shipment/Communication/Plugin/DeliveryTime;
use Generated/Shared/Transfer/QuoteTransfer;
use Generated/Shared/Transfer/ShipmentGroupTransfer;
use Spryker/Zed/Kernel/Communication/AbstractPlugin;
use Spryker/Zed/ShipmentExtension/Dependency/Plugin/ShipmentMethodDeliveryTimePluginInterface;
class DHLExpressPlugin extends AbstractPlugin implements ShipmentMethodDeliveryTimePluginInterface
{
    /**
     * Specification:
     *  - Returns delivery time for shipment group.
     *
     * @param /Generated/Shared/Transfer/ShipmentGroupTransfer $shipmentGroupTransfer
     * @param /Generated/Shared/Transfer/QuoteTransfer $quoteTransfer
     *
     * @return int
     * @api
     *
     */
    public function getTime(ShipmentGroupTransfer $shipmentGroupTransfer, QuoteTransfer $quoteTransfer): int
    {
        return 0;
    }
}

Plugin registration

The plugins must be registered in the ShipmentDependencyProvider, by overriding the following 3 operations:

<?php
/**
 * @param Container $container
 *
 * @return array
 */
protected function getAvailabilityPlugins(Container $container)
{
    return [
        'Plugin name visible in form' => new YourAvailabilityPlugin(),
    ];
}

/**
 * @param Container $container
 *
 * @return array
 */
protected function getPricePlugins(Container $container)
{
    return [
        'Plugin name visible in form' => new YourPricePlugin(),
    ];
}

/**
 * @param Container $container
 *
 * @return array
 */
protected function getDeliveryTimePlugins(Container $container)
{
    return [
        'Plugin name visible in form' => new YourDeliveryTimePlugin(),
    ];
}