Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • David Brendel 786 posts 2952 karma points MVP c-trib
    Mar 02, 2021 @ 13:56
    David Brendel

    Dynamic price from API for products


    we are currently evaluating Vendr for a shop implementation. Our more or less single "pain-point" is that Vendr is calculating prices based on the property on the actual product. In our use-case the product price is calculated based on values the customer provides. These values will get send to an API which then returns the price.

    In our case these are two dates, the selected product and some additional information.

    Is it possible to set a fixed price when a product is added to the cart and disable all calculation based on the product node?

    Regards David

  • Nik 1480 posts 6517 karma points MVP 4x c-trib
    Mar 02, 2021 @ 16:12

    Hi David,

    I'm pretty sure you can. I think you can create your own ProductCalculator which is responsible for calculating the prices of an order line.

    You might need a "OrderLineCalculator" though



  • Matt Brailsford 3342 posts 17700 karma points MVP 8x c-trib
    Mar 02, 2021 @ 18:53
    Matt Brailsford

    Nik is pretty close but the IProductCalculator is only used when you ask a ProductSnapshot to calculate a price, which usually only occurs when you want to display the product price on a product page.

    What you would want to use is an IOrderLineCalculator as this is used for calculating the price on an order line during order calculation, ie, after the item is added to the order.

    There are two approaches you could take here:

    1. Which I think might be what you are thinking, is to do the price calculation externally in your own code, then store the product price on the order line as a property. You could then implement a custom IOrderLineCalculator that in it's CalculateOrderLineUnitPrice you check to see if the order line property is defined, and if it is, return that price for the order line. If not, and assuming you inherit from our base calculator, then you could fallback to the original calculation logic (for order lines where there isn't a defined price).
    2. Rather than storing the price on the order line manually, you store the details that price is based upon and then in the IOrderLineCalculator you put your logic here for getting the price. You could then cache the result (possibly using our PriceFreezer to store the returned unit price).

    The two are essentially the same, it just depends where you want to put your calculation logic.

    Hope this helps


  • David Brendel 786 posts 2952 karma points MVP c-trib
    Mar 10, 2021 @ 08:17
    David Brendel

    Hi Matt,

    sorry for the late response, needed some time to test it all out. :)

    We decided to go with the property and custom OrderLineCalculator which works like a charm.

    Setting the property: order.WithOrderLine(orderLine.Id).SetProperty("apiPrice", "15");


    var apiPriceReturn = orderLine.Properties["apiPrice"].Value;
    if (string.IsNullOrEmpty(apiPriceReturn))
        return base.CalculateOrderLineUnitPrice(order, orderLine, currencyId, taxRate);
    var priceDecimal = Convert.ToDecimal(apiPriceReturn);
    return Price.Calculate(priceDecimal, taxRate, currencyId);

    Thanks for the help!

  • Matt Brailsford 3342 posts 17700 karma points MVP 8x c-trib
    Mar 10, 2021 @ 08:27
    Matt Brailsford


    And thanks for sharing your working code. I’m sure that’ll come in real handy for others wishing to do similar 🙌🏻

Please Sign in or register to post replies

Write your reply to: