Developer forum

Forum » Ecommerce - Standard features » Differentiate shipping options based on products

Differentiate shipping options based on products

Anders Ebdrup
Anders Ebdrup
Reply

Hello,

 

Does anyone have an idea for differentiate shipping options based on products in the cart?
Say I have a GLS parcelshop and I have some products (e.g. a couch) which is not suitable for a parcelshop and now I need to change the default shipping provider and at the same time I will have the hide GLS parcelshop as a shipping option when these specific products are in the cart.

 

Best regards, Anders


Replies

 
Nicolai Pedersen
Reply

Best bet is to either hide the unwanted shipping methods in the rendering based on i.e. weight, or do a redirect to another page with a checkout flow with different delivery options selected...

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Is this still the recommended approach? I have the same requirement in need to hide or show one of two shipping options based on the contents of the cart. Should I do this in a template or is there also a server side approach (like a subscriber) available now to determine the visibility of a shipping provider?

Thanks!

Imar

 
Nicolai Pedersen
Reply

There are a number of things added, i.e. show and hide for particular user groups.

You can on a shipping method add fee rules that targets specific products. That will only affect the price of the shipping though.
You could take the approach of using that feature - so that if a shipping method have a price of 0 or above a fallback of 1000000, you will not display the shipping method. A little hacky...

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Further to this, I have a solution with two shipping providers: "Ship on pallet" and "Ship". When the cart contains at least one product that requires shipping on a pallet, only that option can be shown / preselected. Otherwise, the other option should be shown only. There are  tons of products in each category, so selecting them in the inclusion list is not feasible.

However creating a custom Rapido block to replace parts of the standard template file (CartV2/Steps/Blocks/Shipping.cshtml) just to filter the provider seems like a lot of work. Is that my only alternative? It would be nice to have an "On shipping method selected" subscriber or so that would allow me to determine if the provider can be shown or not...

Thanks!

Imar

 
Nicolai Pedersen
Reply

You can use Ecommerce.Notifications.Ecommerce.Cart.BeforeShippingCalculation and on the arguments set cancel to True. Then the shipping provider will not be listed.

Would this do?

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

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

Votes for this answer: 1

 

You must be logged in to post in the forum