Posted on 28/04/2021 22:21:33
For anyone else interested in this, this is what I landed on:
[Subscribe(Ecommerce.Cart.BeforeShippingCalculation)]
public class FilterShippingProviders : NotificationSubscriber
{
public override void OnNotify(string notification, NotificationArgs args)
{
try
{
var localArgs = (Ecommerce.Cart.BeforeShippingCalculationArgs)args;
if (someCondition)
{
localArgs.Cancel = true; // Skip this provider as it cannot be used.
}
}
catch (Exception ex)
{
var logger = LogManager.Current.GetLogger(nameof(FilterShippingProviders));
logger.Error($"Error determining correct shipping provider: {ex.Message}", ex);
}
}
}
someCondition could use localArgs.Order and localArgs.Shipping to determine if the current shipper can be selected. Note that you may also have to change the current order's shipping if it's incompatible. I used code like this for that but ymmv:
var fullShippingMethod = Dynamicweb.Ecommerce.Services.Shippings.GetShipping(shipper.Id, order.LanguageId);
order.ShippingMethodId = shipper.Id;
order.ShippingMethod = fullShippingMethod.Name;
order.ShippingMethodDescription = fullShippingMethod.Description;
Dynamicweb.Ecommerce.Services.Orders.ClearCachedPrices(order);
Imar