Copied to clipboard

Flag this post as spam?

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


  • Edgar Rasquin 326 posts 925 karma points
    Jan 30, 2023 @ 16:26
    Edgar Rasquin
    0

    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 (_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?

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jan 30, 2023 @ 16:41
    Matt Brailsford
    0

    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.

  • Edgar Rasquin 326 posts 925 karma points
    Jan 31, 2023 @ 10:22
    Edgar Rasquin
    0

    Hi Matt,

    I now have created a

    MyProductCalculator 
    

    and have overridden the method

    CalculateProductPrice
    

    After I have registred the calculator using:

    composition.RegisterUnique<IProductCalculator, MyProductCalculator>();
    

    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>
     (...)
    

    Thanks

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jan 31, 2023 @ 14:04
    Matt Brailsford
    0

    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.

  • Edgar Rasquin 326 posts 925 karma points
    Feb 03, 2023 @ 17:16
    Edgar Rasquin
    0

    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.

    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?

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Feb 04, 2023 @ 10:55
    Matt Brailsford
    0

    It's hard to say without seeing the code of your calculator

  • Edgar Rasquin 326 posts 925 karma points
    Feb 06, 2023 @ 16:49
    Edgar Rasquin
    0
    public class MyProductCalculator : ProductCalculator
    {
        public MyProductCalculator(ITaxService taxService, IStoreService storeService)
            : base(taxService, storeService)
        { }
    
        public override Price CalculateProductPrice(IProductSnapshot productSnapshot, Guid currencyId, TaxRate taxRate)
        {
            var product = Current.UmbracoContext.Content.GetById(productSnapshot.ProductReference.As<Guid>());
    
            if (product.Value<int>("stock") == 0)
            {
                return new Price(0, 0, currencyId);
            }
    
            return base.CalculateProductPrice(productSnapshot, currencyId, taxRate);
    
        }
    }
    
  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Feb 07, 2023 @ 09:21
    Matt Brailsford
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft