Copied to clipboard

Flag this post as spam?

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


  • Bo Jacobsen 597 posts 2395 karma points
    Oct 27, 2021 @ 11:37
    Bo Jacobsen
    0

    Best practice for custom price on an item set by the customer

    Hi Matt.

    We are using VEDNR version 1.8.6.

    What would be the best practice for a customer to be able to set a price on an item by them self?

    Here is the scenario:

    We sell a flower bucket in 3 different sizes like: small (15gp), medium (30gp) and large (60gp). But we would like a 4th option "Custom price", where the customer can set a price by them self. The price has to be minimum, lets say 10gp. And in this scenario the customer want to spend 100gp on a flower bucket.

    How would we be able to achive that?

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Oct 27, 2021 @ 11:44
    Matt Brailsford
    0

    Hi Bo,

    The best way to handle this would be when the adding the product to the cart, capture the amount the customer wishes to pay and store it as an order line property on the given order line. Then extend the default OrderLineCalculator and override the CalculateOrderLineUnitPrice method, checking to see if the order line has this property and if so, return that value as the order line unit price. If the property isn't there, then have it fallback to the default implementation.

    Hope this helps

    Matt

  • Bo Jacobsen 597 posts 2395 karma points
    Oct 27, 2021 @ 12:11
    Bo Jacobsen
    0

    Hi Matt.

    Is it possible to give an example?

    // In a Controller
    using (var uow = _vendrApi.Uow.Create(autoComplete: true))
    {
        var order = _vendrApi.GetOrCreateCurrentOrder(storeId)
            .AsWritable(uow)
            // Where do i set the custom price?
            .AddProduct(productReference, variantReference, Convert.ToDecimal(quantity))
            // Maybe like this?
            .SetProperty("customprice", "100.00");
    
        _vendrApi.SaveOrder(order);
    }
    
    // Do we need to add this somewhere?
    public class MyOrderLineCalculator : OrderLineCalculator
    {
        public MyOrderLineCalculator(ITaxService taxService, IStoreService storeService, IProductPriceFreezerService productPriceFreezerService) : base(taxService, storeService, productPriceFreezerService) { }
    
        public override Price CalculateOrderLineUnitPrice(OrderReadOnly order, OrderLineReadOnly orderLine, Guid currencyId, TaxRate taxRate)
        {
            // What to do here?
            return base.CalculateOrderLineUnitPrice(order, orderLine, currencyId, taxRate);
        }
    }
    
  • Bo Jacobsen 597 posts 2395 karma points
    Oct 27, 2021 @ 12:21
    Bo Jacobsen
    0

    Maybe like this?

    public class MyOrderLineCalculator : OrderLineCalculator
    {
        public MyOrderLineCalculator(ITaxService taxService, IStoreService storeService, IProductPriceFreezerService productPriceFreezerService) : base(taxService, storeService, productPriceFreezerService) { }
    
        public override Price CalculateOrderLineUnitPrice(OrderReadOnly order, OrderLineReadOnly orderLine, Guid currencyId, TaxRate taxRate)
        {
            if(orderLine.Properties["customprice"] != null)
            {
                return Price.Calculate(Convert.ToDecimal(orderLine.Properties["customprice"].Value), taxRate, currencyId);
            }
    
            return base.CalculateOrderLineUnitPrice(order, orderLine, currencyId, taxRate);
        }
    }
    
  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Oct 27, 2021 @ 13:13
    Matt Brailsford
    100

    Yup, exactly that 👍

    When adding the product, you'd do something like

    order.AddProduct(ref, qty, new Dictionary<string, string> {
        { "customprice", customPriceValue }
    });
    
  • Bo Jacobsen 597 posts 2395 karma points
    Oct 28, 2021 @ 07:22
    Bo Jacobsen
    1

    Great.

    I think we would still add it as variant.

    using (var uow = _vendrApi.Uow.Create(autoComplete: true))
    {
        var order = _vendrApi.GetOrCreateCurrentOrder(storeId)
            .AsWritable(uow)
            .AddProduct(productReference, variantReference, Convert.ToDecimal(quantity), 
                new Dictionary<string, string> {
                    { "customprice", customPrice }
                });
        _vendrApi.SaveOrder(order);
    }
    
    public class OrderLineCustomPriceCalculator : OrderLineCalculator
    {
        public OrderLineCustomPriceCalculator(ITaxService taxService, IStoreService storeService, IProductPriceFreezerService productPriceFreezerService) : base(taxService, storeService, productPriceFreezerService) { }
    
        public override Price CalculateOrderLineUnitPrice(OrderReadOnly order, OrderLineReadOnly orderLine, Guid currencyId, TaxRate taxRate)
        {
            if (orderLine.Properties["customprice"] != null && decimal.TryParse(orderLine.Properties["customprice"].Value, out decimal customPrice))
            {
                if (customPrice >= 10)
                {
                    return Price.Calculate(customPrice, taxRate, currencyId);
                }
            }
            return base.CalculateOrderLineUnitPrice(order, orderLine, currencyId, taxRate);
        }
    }
    
    [ComposeAfter(typeof(VendrWebComposer))]
    public class Composer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.RegisterUnique<IOrderLineCalculator, OrderLineCustomPriceCalculator>();
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft