StocklevelProvider
  • 26 Nov 2024
  • 1 Minute to read
  • Dark
    Light

StocklevelProvider

  • Dark
    Light

Article summary

IStockLevelProvider Method signature

GetStockLevelResponse getStockLevels(GetStockLevelRequest request);

GetStockLevelRequest properties

Property name

Type

Description

productIds

Set(Id)

Product2 ids for which the stock level should be retrieved

stockItems

List<GetStockLevelRequest.StockItem>

A list of StockItem objects containing product (same as in the productIds property) and pricebook IDs.

GetStockLevelRequest.StockItem properties

Name

Type

Description

productId

Id

The Product2 Id of a product to retrieve the stock for

pricebookId

Id

The Pricebook2 Id which is used to determine the price for the account of the buyer.


GetStockLevelResponse properties

Property name

Type

Description

stockLevelItems

List(StockLevelItem)

Stock levels related to the specified product ids in the GetStockLevelRequest

StockLevelItem properties

Property name

Type

Description

productId

Id

The Product2 id to which the stock level is associated

remainingQuantity

Decimal

The remaining stock level amount

minimalQuantity

Decimal

Specify to override the default minimal stock level entered on a Web Product or in the global settings

Example implementation

public class CustomStockLevelProvider implements welisacommerce.IStockLevelProvider {
    public welisacommerce.GetStockLevelResponse getStockLevels(welisacommerce.GetStockLevelRequest request) {
        // Create a response object
        welisacommerce.GetStockLevelResponse response = new welisacommerce.GetStockLevelResponse();
        response.stockLevelItems = new List<welisacommerce.StockLevelItem>();

        // Iterate over the request's stock items and populate stock levels
        for (welisacommerce.GetStockLevelRequest.StockItem item : request.stockItems) {
            welisacommerce.StockLevelItem stockLevelItem = new welisacommerce.StockLevelItem();
            stockLevelItem.productId = item.productId;
            stockLevelItem.stockLevel = getStockLevelForProduct(item.productId, item.pricebookId);
            response.stockLevelItems.add(stockLevelItem);
        }

        return response;
    }

    private Integer getStockLevelForProduct(Id productId, Id pricebookId) {
        // Custom logic to fetch stock level for the given product and pricebook
        return 100; // Example stock level
    }
}