Adding Custom Cart Fees To Order

Adding Custom Cart Fees To Order

This post is about how to add custom cart fees to your Salesforce B2B or D2C commerce orders

Introduction

In this post, we will discuss how to add custom cart fees to commerce orders, using the USA Minnesota Retail Delivery Fee as an example. In this scenario, a one-time custom cart fee of EUR 0.50 (in real scenario it will be USD) should be added to orders shipped to the state of Minnesota, with a minimum order total of EUR 100.

We will model this fee as a shipping/delivery charge added to the carte. While an alternative approach is to create a simple product with a price and add it to the cart, this method is not ideal. We aim to avoid having this product appear as a separate line item in the cart, which customers could potentially delete. Although custom Lightning Web Component (LWC) cart components could be built to hide this product, our goal is to leverage native functionality as much as possible.

This method ensures a seamless user experience while adhering to the required fee regulations. Let’s dive into the steps involved in implementing this custom cart fee.

The same code for this implementation is present here.

Implementation Details

To implement the custom delivery fee, we will create a simple product in our B2B or D2C store, as demonstrated in the screenshot below. This product should adhere to the following guidelines:

  1. Category Assignment: Do not assign this product to any category.
  2. Pricing: Do not set a price for this product.

Product Configuration

By following these steps, the product will not be visible to customers and cannot be independently searched for or added to the cart. This ensures the fee is applied seamlessly without customer intervention. Optionally, you can programmatically insert the product through a DML statement while applying the fee.

To add the custom fee to our orders, we will leverage the latest Commerce Extensions feature. Specifically, we will use the Cart Calculate calculator to apply the fee according to the requirements mentioned earlier.

The process involves adding the fee as part of the postShipping flow. This custom fee will be incorporated into the final shipping costs and will appear as a charge in the Order Summary record.

The sample implementation is present under CartCalculateSample.cls and CustomCartFee.cls apex classes.

Sample Class

Testing the Custom Fee Feature

After uploading the custom classes to our Salesforce organization, we are ready to test this feature. Please follow the steps below in your B2B or D2C store:

Note: Ensure you register the extensions for your store before proceeding.

  1. Add a Product to the Basket: Add any product to your basket. Ensure that the minimum order amount is $100.
  2. Proceed to Checkout: Go to the checkout page and select an address within the state of Minnesota, USA.
  3. Verify the Fee Addition: Observe that the shipping calculation is refreshed, and a new EUR 0.50 fee is added to the shipping cost and the basket total. Optionally, you can create a custom Lightning Web Component (LWC) to display the fee as a separate item in the checkout order summary.
  4. Test Fee Removal: Decrease the order total below $100 or select an address outside of Minnesota, and observe that the fee is automatically removed.

The fee will be displayed as follows in the checkout and order summary record. The base shipping cost of €10.99 is increased by €0.50.

Checkout

Order Summary

Order Items