Developer forum

Forum » Ecommerce - Standard features » Replace product price with custom field value

Replace product price with custom field value

Bjarne Rosendal
Bjarne Rosendal
Reply

Hi :)

I am trying to setup a type of discount that would replace the product price used on the orderline with the value of a custom field on the product - if the user is logged in and a member of one particular user group. How to do that? :)

Best regards Bjarne


Replies

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Using a price provider

using Dynamicweb.Ecommerce.Prices;
 
namespace Dynamicweb.Ecommerce.Examples.Prices

{

    public class PriceProviderSample : PriceProvider

 {

        public override PriceRaw FindPrice(PriceContext context, PriceProductSelection selection)

        {

 // Get the price from the DefaultPriceProvider

 DefaultPriceProvider defaultProvider = new DefaultPriceProvider();

 PriceRaw customPrice = defaultProvider.FindPrice(context, selection);


 //find your own price

 if (context.Customer != null && context.Customer.CustomerNumber.StartsWith("abc"))

 {

 //User is logged in

 //find the product field:

 var product = Services.Products.GetProductById(selection.ProductId, selection.VariantId, true);

 var price = Services.Products.GetProductFieldValue(product, "CustomPrice");

 //parse the price which is an object to a double and set the price below

 customPrice.Price *= .90;

 return customPrice;

 }

 else if (selection.Quantity > 1)

 {

 customPrice.Price *= .95;

 return customPrice;

 }


 return customPrice;

 }

 }

}
Votes for this answer: 1
 
Bjarne Rosendal
Bjarne Rosendal
Reply

Hi Nicholai.

Thanks for your reply. Was kinda expecting that to be the way to go about it :)

 

You must be logged in to post in the forum