Posted on 29/10/2014 16:07:22
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