Developer forum

Forum » Development » How to...

Reply

Hi,

 

I can't find any documentation on how to do anything with the newsletter API, does anyone know how to find such help?

 

I want to add a user programatically to the newsletter list.

 

 

The 2nd Step of eCommerce is to request user information, and I have a checkbox in the template, so that user can be added to the Newsletter list as well the format type.

 

 

BasketExtender code below:

 


    public class Diff : Dynamicweb.eCommerce.Orders.OrderTemplateExtender
    {
        public override void ExtendTemplate(Dynamicweb.Templatev2.Template Template, Dynamicweb.eCommerce.Frontend.TemplateExtenderRenderingState RenderingState)
        {
            if (RenderingState == Dynamicweb.eCommerce.Frontend.TemplateExtenderRenderingState.Before)
            {
                if (Order.StateNum == 2)
                    CustumerTemplate(Template);
            }

            base.ExtendTemplate(Template, RenderingState);
        }


        private void CustomerTemplate(Dynamicweb.Templatev2.Template Template)
        {
            String ID = Base.Request("ID");
            if (ID == null) return;

            int AreaID = Dynamicweb.Frontend.PageView.Current().AreaID;
            String type = Base.Request("type") == null ? null : Base.Request("type");

            try
            {

                String customerName = HttpContext.Current.Request.Form["EcomOrderCustomerName"];
                String customerEmail = HttpContext.Current.Request.Form["EcomOrderCustomerEmail"];

                String newsSubs = HttpContext.Current.Request.Form["NewsletterSubscription"];
                String newsType = HttpContext.Current.Request.Form["MailFormat"];

                if (newsSubs.ToLower() == "yes")
                {
                    //Dynamicweb.NewsLetterV3... ?
                }

            }
            catch (Exception)
            {
                // Do nothing
            }
        }
    }

 

Thank you.


Replies

 
Reply

I think I gave you the code for this, when I visited you. If I did, here's an updated version, if not, here you go:

 

You create a user, create a subscription and add this to the users subscriptions collection. Finally you save the new user.

 

Dynamicweb.NewsLetterV3.Recipient MyRecipient = new Recipient();
MyRecipient.RecipientType = Consts.RecipientType.User;
MyRecipient.AccessUserActive = true;
MyRecipient.AccessUserName = MyOrder.CustomerName;
MyRecipient.AccessUserEmail = MyOrder.CustomerEmail;
MyRecipient.AccessUserPassword = ""; //have the user supply a pwd or generate it
MyRecipient.AccessUserMobile = MyOrder.CustomerCell;

        
Dynamicweb.NewsLetterV3.Subscription MySubscription = new Subscription( [The ID of the list to subscribe to], Dynamicweb.Modules.Common.Constants.MailFormat.HTML);

MyRecipient.Subscriptions.Add(MySubscription);
MyRecipient.Save();

 
Reply

Hi Lars,

 

thank you...

 

but weird enough we only have the HTML template and not the Module code for this :-( and I'm still struggling on the MyOrder variable.

 

What I still don't get quite well is the connection between the html templates and the custom modules, as you know I have plenty of live examples here and some I don't see any connection whatsoever, so my integration to the DW Develop world has been quite bumpy.

 

I hope you can smooth the bumps once you came here.

 

 
Reply
bra@communikanten.dk wrote:

Hi Lars,

 

thank you...

 

but weird enough we only have the HTML template and not the Module code for this :-( and I'm still struggling on the MyOrder variable.

 

What I still don't get quite well is the connection between the html templates and the custom modules, as you know I have plenty of live examples here and some I don't see any connection whatsoever, so my integration to the DW Develop world has been quite bumpy.

 

I hope you can smooth the bumps once you came here.

 


 

I don't remember the language of the project, but you should look for a file named something like Voucher.vb or Voucher.cs.

 

I didn't previously notice that it was a template extender, you were using. I don't think you'll be successfull in the attempt to register a user this way - please let me know, if I'm wrong on that one. The code I gave you was implemented in an notification subscriber that reacts to Dynamicweb.Notifications.eCommerce.Order.Steps.Completed. I made an OrderField for the user to check, if he wants to receive the newsletter. This value is automatically recorded by the order and stored in a OrderFieldValue of which I measure the value and register the user if desired. Thus there's no interaction with the Request object, but it runs straight on the API. The complete code is this:

 

[Subscribe(Dynamicweb.Notifications.eCommerce.Order.Steps.Completed)]
    public class NewsletterSubscription : NotificationSubscriber
    {
        public override void OnNotify(string notification, object[] args)
        {
            bool AddNewsletterSubscriber = false;
            Dynamicweb.eCommerce.Orders.Order MyOrder = (Dynamicweb.eCommerce.Orders.Order)args[0];

            foreach (OrderFieldValue MyOrderFieldValue in MyOrder.OrderFieldValues)
            {
                if (MyOrderFieldValue.OrderField.SystemName.ToLower() == "newslettersubscription")
                {
                    if (MyOrderFieldValue.Value.ToString() == "True")
                    {
                        AddNewsletterSubscriber = true;
                    }
                    break;
                }
            }

     if (AddNewsletterSubscriber)
     {
         int NewsletterListID = 261;
 
         Dynamicweb.NewsLetterV3.Recipient MyRecipient = new Recipient();
         MyRecipient.RecipientType = Consts.RecipientType.User;
         MyRecipient.AccessUserActive = true;
         MyRecipient.AccessUserName = MyOrder.CustomerName;
         MyRecipient.AccessUserEmail = MyOrder.CustomerEmail;
         MyRecipient.AccessUserPassword = "";//make a random password or something
         MyRecipient.AccessUserMobile = MyOrder.CustomerCell;
       
       
         Dynamicweb.NewsLetterV3.Subscription MySubscription = new Subscription( NewsletterListID, Dynamicweb.Modules.Common.Constants.MailFormat.HTML);

         MyRecipient.Subscriptions.Add(MySubscription);
         MyRecipient.Save();
     }
        }

 

}

 

You must be logged in to post in the forum