This topic describes examples of how to use the ECF API to work with multi-warehouse features, such as how to get listings of warehouses and getting inventories per warehouse.
Multi-warehouse features
Examples of listing of warehouses and inventories.
Example: Getting a list of warehouses
C#
// Get list Warehouse
public IEnumerable<IWarehouse> ListAllWarehouses()
{
var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
return warehouseRepository.List();
}
Example: Getting warehouse by ID or warehouse code
C#
// Get list Warehouse by ID
public IWarehouse GetWarehouse(int warehouseId)
{
var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
return warehouseRepository.Get(warehouseId);
}
C#
// Get list Warehouse by Code
public IWarehouse GetWarehouse(string warehouseCode)
{
var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
return warehouseRepository.Get(warehouseCode);
}
Note: Use the warehouse code, which most likely is more meaningful in a business context.
For more details on this example, see WarehouseHelper.cs in the EPiServer.Commerce.Sample project.
Example: Picking up an item from a warehouse and add to a cart
C#
//Add an entry from a warehouse to Cart
CartHelper cartHelper = new CartHelper(Cart.DefaultName);
cartHelper.AddEntry(entry, quantity, false, warehouseCode);
Example: Listing warehouse codes for an entry
C#
//List warehouse of an entry
var warehouseCodes = ServiceLocator.Current.GetInstance<IInventoryService>().QueryByEntry(new [] {"code"}.Select(w => w.WarehouseCode);
In store pick-up
Examples of how to create a shipping with pick-up in store, and make it display in the checkout process.
Example: Getting the "In store pick-up" method name
C#
// Get Pickup shipping method name
string pickupMethodName = ShippingManager.PickupShippingMethodName;
Example: Checking if the shipping method is a pick-up method or not
C#
// Check shipping method is pick-up shipment
string pickupMethodName;
ShippingManager.IsHandoffShippingMethod(pickupMethodName);
Example: Picking up an entry and adding it to a cart
C#
var cartHelper = new CartHelper(Mediachase.Commerce.Orders.Cart.DefaultName);
var cart = cartHelper.Cart;
var lineItems = cart.OrderForms[0].LineItems.ToArray().FirstOrDefault();
Shipment shipment = new Shipment();
shipment.CreatorId = SecurityContext.Current.CurrentUserId.ToString();
shipment.Created = DateTime.UtcNow;
shipment.AddLineItemIndex(lineItems.IndexOf(lineItem), lineItem.Quantity);
// Note that lineItem.WarehouseCode should have been determined ahead of time, for example,
// by calling a workflow containing GetFulfillmentWarehouseActivity, such as the CartPrepare workflow.
shipment.WarehouseCode = lineItem.WarehouseCode;
// Check warehouse is Pickup location, shipping method will be "In store pickup".
IWarehouse warehouse = WarehouseHelper.GetWarehouse(lineItem.WarehouseCode);
if (warehouse.IsPickupLocation)
{
// Add address to OrderAddress and Shipment.
if (cartHelper.FindAddressByName(warehouse.Name) == null)
{
var address = warehouse.ContactInformation.ToOrderAddress();
address.Name = warehouse.Name;
cart.OrderAddresses.Add(address);
}
shipment.ShippingAddressId = warehouse.Name;
shipment.ShippingMethodName = ShippingManager.PickupShippingMethodName;
var instorepickupShippingMethod = ShippingManager.GetShippingMethods("en").ShippingMethod.ToArray().Where(m => m.Name.Equals(ShippingManager.PickupShippingMethodName)).FirstOrDefault();
if (instorepickupShippingMethod != null)
{
shipment.ShippingMethodId = instorepickupShippingMethod.ShippingMethodId;
}
}
cart.OrderForms[0].Shipments.Add(shipment);
cart.AcceptChanges();
Doing this, the checkout process moves the entry to In store pickup shipments.
For details about this example, see MultiShipmentCheckout.ascx.cs in the EPiServer.Commerce.Sample project (install a sample site).
Do you find this information helpful? Please log in to provide feedback.