is there a way to set the price in an orderline when it is added to the order?
Before I add the product to the order I check whether there is enough stock
if (_productService.GetProductStock(postModel.ProductReference) > 0)
else there is no product left.
If there is no product left I would still add the product to the order but set the price to 0. That way the customer can place the order without having to pay for it.
Is there a way or am I completely on the wrong track?
These are what control the order line unit price so you could override this and see if there is no stock, setting it to 0, or falling back to the default value as fetched from the product.
Now I'd like to know how to use the new calculator in one specific step. Like so
@foreach (var orderLine in currentOrder.OrderLines)
(...)
//How to replace this default calculation with my Calculator?
// or how to use myCalculator to recalculate the line before?
<div class="pl-4 font-medium">@(orderLine.TotalPrice.Value.Formatted().WithoutTax)</div>
(...)
The calculator is executed every time the order is re-calculated so it's within that context that you'll be providing the price. The calculator is responsible for returning the individual unit price of the given order line item so that's what you should be doing.
You can use whatever dependencies you need to determine what that price should be.
I can now calculate the price in all views but whenever a product is added to an order it fetches the original price from the product, because I pass in the ProductReference.
var order = _sessionManager.GetOrCreateCurrentOrder(store.Id)
.AsWritable(uow)
.AddProduct(postModel.ProductReference, postModel.ProductVariantReference,
postModel.Quantity);
_orderService.SaveOrder(order);
uow.Complete();
}
Or do I have to add it to the order and than change the price afterwards?
Hmm, I guess it depends if the Value<int>("stock") triggers a value converter? There is a stock value converter that reads the stock level from a custom DB table as the value on the node gets reset to -1.
Change price on orderline when added to order
Hi Matt,
is there a way to set the price in an orderline when it is added to the order?
Before I add the product to the order I check whether there is enough stock
If there is no product left I would still add the product to the order but set the price to 0. That way the customer can place the order without having to pay for it.
Is there a way or am I completely on the wrong track?
You'll probably want to implement an order line calculator https://vendr.net/docs/core/3.0.0/key-concepts/calculators/
These are what control the order line unit price so you could override this and see if there is no stock, setting it to 0, or falling back to the default value as fetched from the product.
Hi Matt,
I now have created a
and have overridden the method
After I have registred the calculator using:
Now I'd like to know how to use the new calculator in one specific step. Like so
Thanks
Hi Edgar,
The calculator is executed every time the order is re-calculated so it's within that context that you'll be providing the price. The calculator is responsible for returning the individual unit price of the given order line item so that's what you should be doing.
You can use whatever dependencies you need to determine what that price should be.
Hi Matt,
I can now calculate the price in all views but whenever a product is added to an order it fetches the original price from the product, because I pass in the ProductReference.
Or do I have to add it to the order and than change the price afterwards?
It's hard to say without seeing the code of your calculator
Hmm, I guess it depends if the
Value<int>("stock")
triggers a value converter? There is a stock value converter that reads the stock level from a custom DB table as the value on the node gets reset to-1
.is working on a reply...