Developer forum

Forum » Development » Assigning defaults to cart

Assigning defaults to cart

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

I am trying to set some defaults on a cart coming from an external system. I thought CartCreated would be a good idea, so I implemented the following:

  [Subscribe(eCommerce.Cart.Created)]
  public class AssignUserData : NotificationSubscriber
  {
    public override void OnNotify(string notification, NotificationArgs args)
    {
      if (!(args is eCommerce.Cart.CreatedArgs))
      {
        return;
      }

      var cArgs = (eCommerce.Cart.CreatedArgs) args;
      {
        cArgs.Order.CustomerCompany = "Default name";
        cArgs.Order.CustomerAddress = "Default address";
        cArgs.Order.Save(); // tried it with and without this call
      }
    }
  }

The code runs fine, but none of the changes are applied. Is CartCreated too early to do this? Is there a better way?

Thanks,

Imar

 


Replies

 
Nicolai Høeg Pedersen
Reply

That should be ok. Try calling Context.SetCart(order) after save.

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

I also had to call

cArgs.Order.HasSetUserDetails = true;

because I have a logged in user and the option on the cart to load data from the user had been turned on.

Thanks!

Imar

 

You must be logged in to post in the forum