Expressions are created by referencing variables that you define, or the underlying attributes of the Shopping Cart. Expressions are built using variables and properties of the shipping request (e.g. Customer, Items collection, etc.). Simple expressions just use typical operators like "+", "-", "and", "or", etc. Queries use a variant of Linq syntax (no lambda symbol "=>" required). Queries are applied to collections to "extract" the items in the collection that meet the query criteria.
When variable names are used in expressions, they must be enclosed in square brackets - e.g. [ZipCode3]. Variables can be boolean, string, decimal, integer, etc. Query expressions and Attributes are referenced without enclosing brackets. Most attributes are "objects" that themselves contain other attributes. Some attributes are collections (lists), that can contain 0 or more other objects. Developers having a familiarity with the nopCommerce shopping cart object and linq syntax certainly have an advantage when creating expressions, but the syntax is not that hard to grasp given some examples, and future blogs will discuss the cart,, and provide more examples. Here are some examples that may commonly be used:
Only 2 free items allowed | Items.Where(ProductVariant.Price = 0).Sum(Quantity) > 2 |
|
Has Role with Free Shipping | Customer.CustomerRoles.Any(Name = "FreeShipping") (* see below update)
|
|
Affiliated to XYZ | Customer.AffiliateId = 1 |
|
Placed 2 or more orders in past 30 days | Customer.Orders.Where((DateTime.Now - CreatedOnUtc).TotalDays <= 30).Count() >= 2 |
|
IsOverweight2 | [$TotalWeight] > 150 |
|
ZipCode3 ZoneRate | ShippingAddress.ZipPostalCode.Substring(0,3) EXECUTE ShippingZone_LookupRate @p1, @p2; [ZipCode3], [$TotalWeight] |
(Update: 12/12/2012 - see this for more about Variable and Expression Data Types )
(Update: 12/20/2021 - Customer.CustomerRoles won't work in 4.x and above. Use Customer.GetCustomerRoles() to get the full collection, or to check a specific role, use helper property Customer.IsInRole("role system name") )