Developer forum

Forum » Ecommerce - Standard features » OrderCustomerNumber not added to order if CustomerNumber set at group level

OrderCustomerNumber not added to order if CustomerNumber set at group level

Stephen Anthony Jackson
Reply

Hello.

I am trying to fix it so that all users of a group will inherit the customernumber set in that group, and this is added to the order.

The customer number on the user can be blank (inherit from group), or filled in.

Is this possible?

We already have a DiscountHandler notification subscriber which uses some code to get the customernumber of the users group, then queries price info for this customer and creates  a session variable with the price matrix. This works  fine.

But how do I get this number into the order? Should I store it in a session variable too, and then modify the ecom order templates in an appropriate place, to check for said session variable?

Cheers

Steve


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Stephen

Default you cannot - since a user can be member of several groups.

You can use a Notifications.Standard.User.OnExtranetLogin notification which will fire when the user logs in - and then assign the customer number from one of the groups the user belongs to. This will ensure the user has the customer number and be used for pricing etc.

Another approach might be to add it on the cart using Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.Created notification.

 
Stephen Anthony Jackson
Reply

You mean something like this in the OnExtranetLogin?

// Set CustomerNumber to the derived one
var user = Dynamicweb.Security.UserManagement.User.GetUserByID(userId);
user.CustomerNumber = custNo;

I have already code to get the customernumber from one of the parent groups. It returns the first one it finds. So I will inform the customer, they must not make the users members of multiple groups if those groups have a different customer number.

 
Stephen Anthony Jackson
Reply

Unfortunately it doesnt work. And If i call the save() method on the user object, it does set the customernumber on the user, but this will not be available for an order unless the user logs in again, once its set.

Is there a session variable that is used when the order is sent? I want to set that instead

 
Stephen Anthony Jackson
Reply

I will try this instead

/// <summary>
///  Gets the customernumber for the group, when creating cart, and sets the ordercustomernumber to this
/// </summary>
[Subscribe(Ecommerce.Cart.Created)]
public class OnCartCreated : NotificationSubscriber
{
    public override void OnNotify(string notification, NotificationArgs args)
    {
        if (!(args is Ecommerce.Cart.CreatedArgs))
            return;

        Ecommerce.Cart.CreatedArgs item = (Ecommerce.Cart.CreatedArgs)args;

        var user = Dynamicweb.Security.UserManagement.User.GetCurrentFrontendUser();
        var userId = user.ID;
        var custNo = Helper.GetGroupCustomerNumber(userId);

        item.Order.CustomerNumber = custNo;

    }

 
Stephen Anthony Jackson
Reply

I ended up doing it in the Order BeforeSave event, and that seems to work. Gets called a lot though :(

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Cool.

Yeah, that save is called too many times - we hope to get to a point where we can do that part better - and not break solutions that kind of rely on it... #DW10

 
Stephen Anthony Jackson
Reply

What if I do this instead? Will it create some kind of race condition between the order being exported to xml and my aftersave operation modifying it? Is the db commit before or after "aftersave"?

 public override void OnNotify(string notification, NotificationArgs args)
 {
     if (!(args is Ecommerce.Order.AfterSaveArgs))
         return;

     Ecommerce.Order.AfterSaveArgs item = (Ecommerce.Order.AfterSaveArgs)args;

     var user = Dynamicweb.Security.UserManagement.User.GetCurrentFrontendUser();
     var userId = user.ID;
     var custNo = Helper.GetGroupCustomerNumber(userId);


    if (custNo.IsNotNullOrEmpty() &&  item.Order.CustomerNumber != custNo && !item.Order.IsCart)
 {
     item.Order.CustomerNumber = custNo;
     item.Order.Save();
 }
 }

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

It is not a good idea to call save inside a save notification subscriber as that can go wrong as that might trigger another notification.

So simply set the property onbefore save, but do not save it as that will happen as soon as your notification is exited. So beforesave, and do not call save.

Also you are missing a null check on the user instance - it could very well be null if you are not logged in or not in the frontend.

 
Stephen Anthony Jackson
Reply

Ok thanks. 

 

You must be logged in to post in the forum