New nVelocity variables

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
User avatar
jmestep
AbleCommerce Angel
Posts: 8164
Joined: Sun Feb 29, 2004 8:04 pm
Location: Dayton, OH
Contact:

New nVelocity variables

Post by jmestep » Thu Apr 09, 2009 3:38 pm

I think I am going to have to create a few new nVelocity variables and have looked thru their documentation and the Able source code.
Can you give an example of how you created the variables for the store?
Judy Estep
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx

User avatar
nickc
Captain (CAPT)
Captain (CAPT)
Posts: 276
Joined: Thu Nov 29, 2007 3:48 pm

Re: New nVelocity variables

Post by nickc » Thu Apr 09, 2009 4:57 pm

Don't have API source, but just had to re-write email generation as we now operate multiple stores out of a single code-base/database. nVelocity.Process method takes a hashtable of objects and a string argument, and returns a parsed string - here's some sample code...

Code: Select all

    protected void SendOrderPlacedEmailNotifications(int _orderId)
    {
        // set up values needed by templates
        Order order = OrderDataSource.Load(_orderId);
        PaymentCollection payments = order.Payments;
        Store store = Token.Instance.Store;
        VendorCollection vendors = VendorDataSource.LoadForOrder(_orderId);
        OrderShipmentCollection shipments = OrderShipmentDataSource.LoadForOrder(_orderId);

        Hashtable ht = new Hashtable();
        ht.Add("customer", order.User);
        ht.Add("order", order);
        ht.Add("payments", payments);
        ht.Add("store", store);
        ht.Add("site", SiteInformation.Current);

        // we always notify customer
        CommerceHelper.SendSiteMail("Customer Order Notification", ht);

        // notify vendors
        foreach (Vendor vendor in vendors)
        {
            // for vendors, we need 2 collections:
            // VendorShipments and VendorItems
            OrderShipmentCollection VendorShipments = new OrderShipmentCollection();
            OrderItemCollection VendorItems = new OrderItemCollection();

            foreach (OrderItem item in order.Items)
            {
                Product product = ProductDataSource.Load(item.ProductId);
                if (product != null && product.VendorId == vendor.VendorId)
                {
                    VendorItems.Add(item);
                    if (VendorShipments.IndexOf(item.OrderShipmentId) < 0)
                        VendorShipments.Add(item.OrderShipment);
                }
            }

            // clone the hashtable and add the 3 new objects, send e-mail
            Hashtable vendorHt = ht;
            vendorHt.Add("vendor", vendor);
            vendorHt.Add("VendorShipments", VendorShipments);
            vendorHt.Add("VendorItems", VendorItems);
            CommerceHelper.SendSiteMail("Vendor Notification", vendorHt);
        }
    }

Code: Select all

       public static void SendSiteMail(string templateName, Hashtable parameters)
        {
            // hashtable contains objects for template replacements:
            // store, customer(user), basket, order, shipments, vendor, etc.
            // see template source for details (velocity references for "objectname" are ${objectname})
            NVelocityEngine nVengine = new NVelocityEngine();

            // load up the template
            EmailTemplate template = Token.Instance.Store.EmailTemplates.Find(delegate(EmailTemplate t) { return t.Name == templateName; });
            if (template != null)
            {
                // invoke velocity engine processing and send mail
                nVengine.Init();
                template.ToAddress = nVengine.Process(parameters, template.ToAddress);
                template.FromAddress = nVengine.Process(parameters, template.FromAddress);
                template.CCList = nVengine.Process(parameters, template.CCList);
                template.BCCList = nVengine.Process(parameters, template.BCCList);
                template.Subject = nVengine.Process(parameters, template.Subject);
                template.Body = nVengine.Process(parameters, template.Body);
                template.Send();
            }
        }
    }

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: New nVelocity variables

Post by mazhar » Fri Apr 10, 2009 5:01 am

Just create instance of Email Template and then set your desired parameters to it for example

Code: Select all

EmailTemplate emailTempalte = EmailTemplateDataSource.Load(1);
emailTempalte.Parameters.Add("store",store);
emailTempalte.Parameters.Add("customvalue",customvalue);
...................
...................
emailTemplate.Send()

User avatar
jmestep
AbleCommerce Angel
Posts: 8164
Joined: Sun Feb 29, 2004 8:04 pm
Location: Dayton, OH
Contact:

Re: New nVelocity variables

Post by jmestep » Fri Apr 10, 2009 5:30 am

OK, thank you. Just to make sure I understand, if I want them outside of the emails. I will create a class and create the variables like the pattern in these posts?
I'm trying to do this because in one place I need to use the Able user name data inside the body of some text that is entered in a custom field, like the contact us control could be entered inside the website description content.
Text
[[ConLib:ContactUs]]
more text.
Judy Estep
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx

User avatar
jmestep
AbleCommerce Angel
Posts: 8164
Joined: Sun Feb 29, 2004 8:04 pm
Location: Dayton, OH
Contact:

Re: New nVelocity variables

Post by jmestep » Fri Apr 10, 2009 10:14 am

I've been doing more research. Are the store variables available in nVelocity on the scriptlet pages only because they are coded into an email template? To add new custom objects and variables would I have to make an email template?
I'm still not understanding how this all gets pulled together and the references I have found refer to a .vm file as a template.
Judy Estep
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx

User avatar
nickc
Captain (CAPT)
Captain (CAPT)
Posts: 276
Joined: Thu Nov 29, 2007 3:48 pm

Re: New nVelocity variables

Post by nickc » Fri Apr 10, 2009 10:28 am

Example was just meant to show how nVelocity can parse ${object.Property} in a string - use in email is incidental.
e.g. string newstring = nVengine.Process(hashtable_of_objects, oldstring);

so
Text
${user.FullName}
${user.PrimaryAddress.Address1}
More text

would yield user name and street address if processed by nVelocity using a hashtable that contained a User object with key "user".

If you are more interested in dynamically including a control based on a text reference, I'd guess that a .vm file is a Velocity Macro. I'll defer to the folks at Able about what role (if any) nVelocity plays in processing [[ConLib]] tags in scriptlet files. We have our own separate method that generalizes loading controls dynamically.

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

Re: New nVelocity variables

Post by AbleMods » Fri Apr 10, 2009 11:00 am

jmestep wrote:I've been doing more research. Are the store variables available in nVelocity on the scriptlet pages only because they are coded into an email template? To add new custom objects and variables would I have to make an email template?
I'm still not understanding how this all gets pulled together and the references I have found refer to a .vm file as a template.
Here's how I do it Judy:

The variables you find available within an email template are there simply because they were specifically added. Like at the example code below. You'll see that I've loaded in an existing email template from the store table.

From there, you add any class data you want as parameters to the email template. So if you want the currently logged in users data available to that email template when it's generated into an email, you would do _Template.Parameters.Add("user", Token.Instance.User). This tells the email template class that you want the contents of the user class object to be known as "$user" in the email template script code. This also works for any custom class designs you might have. You'll see an example of that below as well.

Note that it's very important to have the FALSE parameter on the email template data source. There's an issue with caching that will cause exceptions to throw if the routine that builds the email template gets called more than once.

In the email template itself, it looks no different than any other email template in AC7. It may have $mycustomclass.ObjectId references, but that's because I can since it was added as a parameter to the email template.

Finally, remember that all nVelocity references in email templates are case sensitive.

Code: Select all

' ok email template found.  
            Dim _Template As EmailTemplate = EmailTemplateDataSource.Load(_EmailTemplateId, False)
            If _Template Is Nothing Then
                Return
            End If

            ' good.  Everything is valid.  Proceed with email template construction.
            Try
                _Template.ToAddress = IIf(_Template.ToAddress = "customer", _MyCustomClass.User.Email, _Template.ToAddress)
                _Template.Parameters.Add("store", Token.Instance.Store)
                _Template.Parameters.Add("mycustomclass", _MyCustomClass)
                _Template.Parameters.Add("user", _MyCustomClass.User)
                _Template.Parameters.Add("order", _MyCustomClass.Order)
                _Template.Parameters.Add("primaryaddress", _MyCustomClass.User.PrimaryAddress)

                _Template.Send()
            Catch ex As Exception
                ErrorMessageDataSource.Insert(New ErrorMessage(MessageSeverity.Error, "AbleMods", "Error sending email type: " + _NoticeType + " regarding MyCustomClass " + _MyCustomClass.ObjectId.ToString + " error: " + ex.Message))
            End Try
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

User avatar
Logan Rhodehamel
Developer
Developer
Posts: 4116
Joined: Wed Dec 10, 2003 5:26 pm

Re: New nVelocity variables

Post by Logan Rhodehamel » Mon Apr 13, 2009 4:09 pm

Judy what you are looking for is to customize page display, which is a little different from the email system. They both use nvelocity. Currently, customizing the variables for page display can only be done if you have source code. I am pretty sure you do, so check in CommerceBuilder.Web.UI.WebControls.WebParts.ScriptletPart. Around line 191 in my copy is where the variables for nVelocity are set up. You can add in more there.
Cheers,
Logan
Image.com

If I do not respond to an unsolicited private message, it's not because I'm ignoring you. It's because the answer to your question is valuable to others. Try the new topic button.

User avatar
jmestep
AbleCommerce Angel
Posts: 8164
Joined: Sun Feb 29, 2004 8:04 pm
Location: Dayton, OH
Contact:

Re: New nVelocity variables

Post by jmestep » Mon Apr 13, 2009 4:41 pm

OK, thank you, Logan. I do have the source code and have seen that.
Judy Estep
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx

Post Reply