Skip to main content

ShippingLineAssignmentStrategy

ShippingLineAssignmentStrategy

This strategy is used to assign a given ShippingLine to one or more OrderLines of the Order. This allows you to set multiple shipping methods for a single order, each assigned a different subset of OrderLines.

The DefaultShippingLineAssignmentStrategy simply assigns all OrderLines, so is suitable for the most common scenario of a single shipping method per Order.

info

This is configured via the shippingOptions.shippingLineAssignmentStrategy property of your VendureConfig.

Here's an example of a custom ShippingLineAssignmentStrategy which assigns digital products to a different ShippingLine to physical products:

import {
Order,
OrderLine,
RequestContext,
ShippingLine,
ShippingLineAssignmentStrategy,
} from '@vendure/core';

export class DigitalShippingLineAssignmentStrategy implements ShippingLineAssignmentStrategy {
assignShippingLineToOrderLines(
ctx: RequestContext,
shippingLine: ShippingLine,
order: Order,
): OrderLine[] | Promise<OrderLine[]> {
if (shippingLine.shippingMethod.customFields.isDigital) {
return order.lines.filter(l => l.productVariant.customFields.isDigital);
} else {
return order.lines.filter(l => !l.productVariant.customFields.isDigital);
}
}
}
Signature
interface ShippingLineAssignmentStrategy extends InjectableStrategy {
assignShippingLineToOrderLines(
ctx: RequestContext,
shippingLine: ShippingLine,
order: Order,
): OrderLine[] | Promise<OrderLine[]>;
}

assignShippingLineToOrderLines

method
(ctx: RequestContext, shippingLine: ShippingLine, order: Order) => OrderLine[] | Promise<OrderLine[]>