Developer forum

Forum » Development » Custom PriceProvider on DW8

Custom PriceProvider on DW8

Dmitrij Jazel
Reply
Hej Guys,

Have a question here, :-)
I am trying to implement a custom price provider here
But something is wrong with "product.price.price" method.
Every single time I am trying to use it - it just brakes. :-?
I am using DW8 Pro 8.1.0.0

Here is the implementation...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Dynamicweb.eCommerce.Prices;
using Dynamicweb.Extensibility;
using Dynamicweb.Frontend;
using System.Data.SqlClient;


public class CustomDiscountPriceProvider : PriceProvider
{
    public override PriceRaw FindPrice(Dynamicweb.eCommerce.Products.Product Product, double Quantity, string VariantID, Dynamicweb.eCommerce.International.Currency Currency, string UnitID, Dynamicweb.Frontend.Extranet User)
    {
        PriceRaw priceRaw = null;
        if (User.LoggedIn && Product.Name.StartsWith("Ø"))
        {
            priceRaw = new PriceRaw(Product.Price.Price*0.75, Currency);
        }
        return priceRaw;
    }


    // ERROR MESSAGE
    private void errorMessage(string status)
    {
        // Error message code...
    }
}


Anything I am doing wrong here / suggestions?
Thanks in advance!

Regards,
Dmitrij

Replies

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
This post has been marked as an answer

Hi Dmitrij

You don't explicitly say what error you experience, but I think you're getting a StackOverflow. The reason is you're getting this is because you get a circular reference. Product.Price calls your PriceProvider that in turn calls Product.Price and so on. You need to break the circle.

Hope this helps :)

- Jeppe

Votes for this answer: 0
 
Dmitrij Jazel
Reply
Hej Jeppe,

Well this is a very simple example, I don't think there can be easyer one :))) it is pretty much straight forward.
But yea, I was asking myself similar question, and I think that does it, I will come back with my findings...

Thanks for help so far.

Dmitrij

 
Dmitrij Jazel
Reply
Hej Jeppe,
Well it looks like that is the case (there with product referencing itself), so thanks for that :-)

But, if you continue from that code I wrote above, the system can check whether user loggedin or not, but If I would need a special value stored in UserJobTitle of that user, it seams I can't access not this field nor any other field of that nature...

I need to get the value stored as "JobTitle" of the current logged in user.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Dynamicweb.eCommerce.Prices;
using Dynamicweb.Extensibility;
using Dynamicweb.Frontend;
using System.Data.SqlClient;


public class CustomDiscountPriceProvider : PriceProvider
{

	public override PriceRaw FindPrice(Dynamicweb.eCommerce.Products.Product Product, double Quantity, string VariantID, Dynamicweb.eCommerce.International.Currency Currency, string UnitID, Dynamicweb.Frontend.Extranet User)
    {
		
		double price = getProductPrice(Product.ID);
	
		string discountKey = User.JobTitle;
		errorMessages(discountLey); // print out temp value
	
        PriceRaw priceRaw = null;
        if (User.LoggedIn)
        {
			double discountPrice = calculateDiscountPrice(discountKey, price);
            priceRaw = new PriceRaw(discountPrice, Currency);
        }
        return priceRaw;
    }

    
}

Is that correct way to do it?
Or I am doing something wrong... That value is Always "".

Please help :-(

Regards,
Dmitrij

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

That should work, if the User is in fact logged in. However if you add the JobTitle value after the ExtranetLogin has occured, then you won't get the value here. You could disregard the Extranet object (the one called User) and get the current User from User Management this way:

            PriceRaw priceRaw = null;

            var user = Dynamicweb.Modules.UserManagement.User.GetCurrentUser();
            if (user != null)
            {
                //User is logged in
                var discountKey = user.JobTitle;

                double discountPrice = calculateDiscountPrice(discountKey, price);
                priceRaw = new PriceRaw(discountPrice, Currency);
            }

            return priceRaw;


I have a question though. Why do you use JobTitle to store a key to a Price instead of using a Custom Field?
 
Dmitrij Jazel
Reply
Hej Jeppe,
Yeej, thanks for lightning fast response :-) and that did the trick!
Yet again, I must thank you for help!

Well this is just a simple string value we need to store along with each client here, I am importing users from the external service, and we just put it into one dataset that corresponds to the AccessUser table. And If I am not mistaken when you are using custom fields, you would have to insert this discount value into one more additional table that relates to this custom field. When with this Jobtitle approach I would have to make an insert only once when I am importing new user.

I totally agree that it is very convenient to use custom fields when you are working inside DW itself, and user management is done there. But if the situation is that the users are not using Dynamicweb admin panel at all, than I must rely on something Dynamicweb has built in. Like interaction with JobTitle :-)

Regards,
Dmitrij
 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
Actually, when working with CustomFields on AccessUser, the values are stored on AccessUser. Only the definition of the CustomField is stored in the CustomField table.

In your case you would have to only create the CustomField once so the definition is present, and the values could easily come from your external system.
 
Dmitrij Jazel
Reply
Well again, I think you would agree that the best best idea always comes the last...
We just sticked to that JobTitle idea from the start as one field that we are "sending" but not really using... so found a use for it.
But I will keep this in mind... :-)


 

You must be logged in to post in the forum