Developer forum

Forum » Development » Set shippingmethodid on login

Set shippingmethodid on login

Martin Nielsen
Reply

Hi guys,

I have a soultion with 2 deliverytypes 'Fragtmand' and 'Ab fabrik'. On my Extranet users i have a field where i define which delivery method is available to that person.

How would i go about making this feature?

I'm thinking that i need to make a notificationt that runs each time a new cart is created and then set the correct shipping method.

Is that the right way to go?

// Martin


Replies

 
Martin Nielsen
Reply

Here's the code i built. For some reason it doesn't update my shipping method. I stepped through the code and i know that my order.save() method is called.

  [Dynamicweb.Extensibility.Subscribe( Dynamicweb.Notifications.eCommerce.Cart.Created )]
  public class EcomCartCreatedObserver1 : Dynamicweb.Extensibility.NotificationSubscriber
  {
    public override void OnNotify( string notification, Dynamicweb.Extensibility.NotificationArgs args )
    {
      if( args == null || !(args is Dynamicweb.Notifications.eCommerce.Cart.CreatedArgs) )
        return;

      Dynamicweb.Notifications.eCommerce.Cart.CreatedArgs cArgs = (Dynamicweb.Notifications.eCommerce.Cart.CreatedArgs)args;

      bool hasAccessToFreeFreight = false;
      Dynamicweb.Modules.UserManagement.User currentUser = Dynamicweb.Modules.UserManagement.User.GetCurrentUser();
      var field = currentUser.CustomFieldValues.FirstOrDefault( p => p.CustomField.SystemName.Equals( "AccessUser_AccessToFreeFreight" ) );
      if (field != null) {
        string fieldValue = field.Value.ToString();
        if (fieldValue.ToLower().Equals("true")){
          hasAccessToFreeFreight = true;
        }
      }

      // Fragt metoder SHIP3 og SHIP4
      string shippingMethod = "SHIP4";
      if (hasAccessToFreeFreight == true) {
        shippingMethod = "SHIP3";
      }

      cArgs.Order.ShippingMethodID = shippingMethod;
      cArgs.Order.Save();
    }
  }

 

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Martin,

The code seems to be in order. One thing to note, though, is that updating the ShippingMethodID is not enough to update the ShippingMethod (name of the shipping method) and ShippingMethodDescription (description of the shipping method). Maybe the ID is updated correctly, but it's not visibly clear because the name wasn't updated?

Also, if your method uses a ShippingProvider, you probably also need to update the ShippingProviderValues dictionary.

- Jeppe

 
Martin Nielsen
Reply

That seemed to do the trick.

would be awesome to be able to just do:

order.SetShippingMethod("SHIP1");

and

order.SetPaymentMethod("PAY1");

 

 
Martin Nielsen
Reply

And Thanks :)

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

I agree, so I've added a feature request for this :)

 
Martin Nielsen
Reply

On the same notification, i want to set the customers country. (For some reason DW doesn't do that when pulling all the other info from the Extranet user.)

I refactored my code to this:

[Dynamicweb.Extensibility.Subscribe( Dynamicweb.Notifications.eCommerce.Cart.Created )]
  public class EcomCartCreatedObserver1 : Dynamicweb.Extensibility.NotificationSubscriber
  {
    public override void OnNotify( string notification, Dynamicweb.Extensibility.NotificationArgs args )
    {
      if( args == null || !(args is Dynamicweb.Notifications.eCommerce.Cart.CreatedArgs) )
        return;

      Dynamicweb.Notifications.eCommerce.Cart.CreatedArgs cArgs = (Dynamicweb.Notifications.eCommerce.Cart.CreatedArgs)args;

      Order cart = cArgs.Order;

      bool hasAccessToFreeFreight = false;
      Dynamicweb.Modules.UserManagement.User currentUser = Dynamicweb.Modules.UserManagement.User.GetCurrentUser();
      var field = currentUser.CustomFieldValues.FirstOrDefault( p => p.CustomField.SystemName.Equals( "AccessUser_AccessToFreeFreight" ) );
      if( field != null )
      {
        string fieldValue = field.Value.ToString();
        if( fieldValue.ToLower().Equals( "true" ) )
        {
          hasAccessToFreeFreight = true;
        }
      }

      // Fragt metoder SHIP3 og SHIP4
      string shippingMethod = "SHIP4";
      if( hasAccessToFreeFreight == true )
      {
        shippingMethod = "SHIP3";
      }

      // Update shipping
      SetShippingMethod( shippingMethod, ref cart );
      
      // Set country on cart
      SetCountry( "BY", ref cart ); // Test with Belarus

      cart.Save();
    }

    void SetCountry( string countryCode, ref Order cart )
    {

      Dynamicweb.eCommerce.International.Country foundCountry = new Dynamicweb.eCommerce.International.Country( countryCode );
      if( foundCountry != null && string.IsNullOrEmpty( foundCountry.Code2 ) == false ) {
        cart.CustomerCountryCode = foundCountry.Code2;
        cart.CustomerCountry = foundCountry.CountryText.Name;
      }      
    }

    void SetShippingMethod( string shippingMethodID, ref Order cart )
    {
      Dynamicweb.eCommerce.Orders.Shipping shipping = new Dynamicweb.eCommerce.Orders.Shipping( shippingMethodID );
      if( shipping != null && string.IsNullOrEmpty( shipping.Name ) == false )
      {
        cart.ShippingMethodID = shipping.ID;
        cart.ShippingMethod = shipping.Name;
        cart.ShippingMethodDescription = shipping.Description;
        cart.ShippingMethodCountryCode = shipping.CountryID;
      }
    }
  }

But my cart isn't updated, when i step though my cart and get to the information step, my country i 'DK' which is my websites default country.

Any ideas on how to change this?

 

// Martin

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

It should be enough to set CustomerCountry, as you do. The only thing you do differently than CartV2 internally is to set CustomerCountryCode as well. Try removing that line.

 
Martin Nielsen
Reply

Hi Jeppe,

I got my code running now, with the CustomerCountryCode update, but my notification doesn't run everytime i expect it to.

I have a scenario where my notification isn't fired when a returning custom logs in to the site and adds a product (the first one). If i remove the product and add it again, the notification fires.

Can i have have a cart with no products saved on my extranet users?

// Martin

 

 

 

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Technically it is possible.

If you save an order without orderlines from code or if the orderline collection is emptied from code, this situation can occur. If the products that were originally in the cart are no longer active (based on settings in Mgmt Center -> Ecommerce -> Advanced -> General) or have been removed from the database, the cart could also exist without orderlines.

Using the url parameters to add/increase, remove/decrease or empty the cart, this should not happen.

Are you sure that the cart is empty when the user logs in? Try subscribing to the Loaded cart notification and check the state of the cart.

 
Martin Nielsen
Reply

Hi Jeppe,

I tested your suggestion and i can see that my cart indeed set in some scenarios.

In my project i have code that removes orderlines from the cart if they are no longer available in an external system, and this can result in orders with no orderlines it seems.

I do this in my code:

// orderlinesToRemove contains a list of orderlines to remove
for( int i = 0;i < orderlinesToRemove.Count;i++ )
{
    order.OrderLines.RemoveLine( orderlinesToRemove[i] );
}
order.Save();

In cases when the order ends up with 0 orderlines, how would i go about setting the cart to NULL, so i can get the other logic to be consistent?

The above code runs everytime a user logs in.

// Martin

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

If you change line 6 in the above sample to the following, you get that behavior:

if (order.OrderLines.Count == 0)
{
	order.Delete();
	Dynamicweb.eCommerce.Frontend.Cart.CartCatch.ClearCart();
}
else
{
	order.Save();
}

Note: You do not get the Cart.Deleted notification when deleting the cart this way. You can of course broadcast the notification yourself using the NotificationManager, if you need to.

 
Martin Nielsen
Reply

Thanks, i'll give it a go.

 

You must be logged in to post in the forum