Blog posts of '2013' 'January'

RSS
Exclude Shipping Methods - choose best one- Monday, January 7, 2013

NOTE:  This is an old blog.  nopCOmmerce no longer has ProductVariant.  Just use Product.  E.g.  (Product.Height * Product.Length * Product.Width)

In a previous blog, I showed how in SD you can calculate the shipping method name to show the customer using the Name Expression field on an Option record.  If the expression evaluates to a blank string, then SD will suppress the method.  Here's another example of how, for external shipping rate methods (carrier plugins like FedEx, USPS, etc.) which can return many options, it may be desirable to conditionally eliminate some options - or only show a single option.

In this example we'll offer the following USPS methods

USPS Priority Mail® Large Flat Rate Box
USPS Priority Mail® Medium Flat Rate Box
USPS Priority Mail® Small Flat Rate Box
USPS Priority Mail®

But we only want to show one - the best match based on the volume of items in the cart:

Order

Type

Name

Expression

Rate Expression

Name Expression

10

Reference

ItemVolume

(ProductVariant.Height * ProductVariant.Length * ProductVariant.Width)

 

 

20

Reference

CartVolume

Items.Sum([@ItemVolume] * Quantity)

 

 

30

Option

US Mail

true

Shipping.USPS

[$Name].Contains("Small") and [@CartVolume] <= 75 ? [$Name] : [$Name].Contains("Medium") and [@CartVolume] > 75 and [@CartVolume] <= 546 ? [$Name] :

[$Name].Contains("Large") and [@CartVolume] > 546 and [@CartVolume] <= 792 ? [$Name] :

[$Name] = "Priority Mail" and [@CartVolume] > 792 ? [$Name] : ""

Tags :  ExcludeOption
Comments (0)
Query with Extension Methods- Friday, January 4, 2013

(Note!!  This blog has been updated. Due to changes in nopCommerce, use just "Product", not "ProductVariant.Product".)

A little while back we introduced some new functions that you can use to query your cart when creating complex shipping scenarios.  These are also available in Payment Director.  Here's some more details:

You can check if a product is in a particular category (or any category matching a pattern):

Product.HasCategory(categoryName)

Product.HasCategory(categoryId)

Product.HasCategoryMatch(regular_expression)

For example, you can check if there are any items in the cart that have a category that includes the word “Brushes”:

Items.Any(Product.HasCategoryMatch("Brushes"))

You can test if an item has a specific attribute/value (e.g. “Color”,”Blue”), or the specific attribute with any value (“Color”):

HasAttributeValue(attributeName, valueName)

HasAttributeValue(attributeName)

For example, you can get the quantity of items in cart that have the Vanilla scent:

Items.Where(HasAttributeValue("Scent","Vanilla")).Sum(Quantity)

 You can use the value id (integer) if you prefer (in case you change the name)

HasAttributeValue(valueId)

You can get a list of attribute values that the customer has applied to a cart item:

Item.GetAttributeValues(attributeName, valueSeparator)

Item.GetAttributeValues(attributeName)  // value separator defaults to “,”

Of course it does not need to be a list – for example, if you have a “Size” attribute, then you can just get the value the customer entered for the Size.  For example, if you have many products in the cart with a Size attribute, you can calculate a shipping rate based on the size values – let’s say you want the rate to be on a per item basis, for each ($3.33 * Size * Quantity)

Items.Where(HasAttributeValue("Size")) .Sum(3.33 * Decimal.Parse(GetAttributeValues("Size")) * Quantity)

(You can do date/time calculations too – e.g. … .Min(DateTime.Parse(GetAttributeValues("DeliveryDate")) )

Here’s an example of how the new methods make your expressions shorter and more readable:

We’ve blogged before about “Free Shipping over $X - unless there are any excluded items”.  We used a Reference type variable to shorten the query:

Order

Type

Name

Expression

Rate Expression

10

Reference

categories

Product.ProductCategories

 

20

Reference

hasExcludeFromFreeOverX

[@categories].Any(Category.Name = "Exclude from Free Over X")

 

30

OptionExit

Free Shipping

[$SubTotalWithDiscounts] > 50 and !Items.Any([@hasExcludeFromFreeOverX])

0

40

Option

Standard

true

10

Here the same scenario as above with new HasCategory method -

Order

Type

Name

Expression

RateExpression

30

OptionExit

Free Shipping

[$SubTotalWithDiscounts] > 50 and !Items.Any(Product.HasCategory("Exclude from Free Over X"))

0

40

Option

Standard

true

10

Tags :  Functions
Comments (0)
Payment Director - Conditional payment methods- Friday, January 4, 2013

Payment Director allows a store owner to conditionally determine which payment methods to show the customer and to calculate additional payment fees.  Let’s start with the basics…

By default, all Payment Methods that are marked Active in admin > Configuration > Payment Methods are available to your customers.   Then, you set up Payment Director Option records as needed to hide options and calculate fees.   Each option’s Expression field is evaluated, and if ‘true’ (or ‘Show’) then the option will be presented to the customer.  If ‘false’ (or ‘Hide’), then the payment option is removed.  You can include an Option record for all your active payment methods, but if only some are conditional, then it’s easiest to just have Option records for those;  if an active payment method is not included in payment director, then it will automatically be shown to the customer.

Here are some example scenarios for offering payment methods, and their Option records/expressions:

Offer 'payment in store' method only when "In-Store Pickup" shipping method selected:

  Option  Payments.PayInStore         ShippingOptionName = "In-Store Pickup"

Local Deliveries can include CashOnDelivery:

  Option  Payments.CashOnDelivery Customer.ShippingAddress.ZipPostalCode.StartsWith("100")

Customers in role "Members" can include PurchaseOrder  (use the role's System Name):

  Option  Payments.PurchaseOrder      Customer.IsInCustomerRole("Members")

If an admin is impersonating a Customer, then use Manual Credit Card, otherwise the customer enters a credit card that Authorize.Net will process:

  Option  Payments.Manual             OriginalCustomerIfImpersonated != null

  Option  Payments.AuthorizeNet       OriginalCustomerIfImpersonated = null

Only offer Paypal if a FedEx shipping method was selected (a method that has tracking :)

  Option  Payments.PayPalStandard     ShippingOptionName.StartsWith("FedEx")

Only offer credit card if there are  no Gift Cards in the cart

  Option  Payments.Manual           NOT Items.Any(Product.IsGiftCard)

When entering records in the Add new 'Payment Director' record dialog, select Option in the Type  field drop down, and then the Payment Name field will present a drop down with only active payment methods.    


Then, enter an expression that evaluates to a Boolean (true or false) using one or more or more of the following operands:

  • Customer  (also has function IsInRole("systemname") )
  • ShippingAddress
  • ShippingOption  (.ShippingRateComputationMethodSystemName, .Rate, .Name, .Description)
  • ShippingOptionName
  • Items  (shopping cart items collection)
  • OriginalCustomerIfImpersonated (if != null then you are impersonating)
  • WorkingLanguage  (.Name, and .LanguageCulture)
  • WorkingCurrency (.Name, .CurrencyCode, .Rate, and .DisplayLocale)
  • TaxDisplayType  (IncludingTax = 0, ExcludingTax = 10)
  • CreditCardType  (e.g. for Authorize.NET :  "Visa", "MasterCard", "Discover", "Amex")
  • PurchaseOrderNumber
  • OrderTotalWithoutPaymentFee
  • FriendlyName

   (UPDATE: Here are more operands added added from 3.60 and on)

  • CurrentStoreId
  • CurrentStoreName
  • IsImpersonating (Boolean)
  • CustomValues  
    • (The nopC PaymentInfoCustomValues is a Dictionary of Key/Values set by other payment plugins.  This variable converts that dictionary to a string of "key:value|key:value|...".  You can use CustomValues.StringBetween("key:","|") to get a value.

Here are some examples:

WorkingLanguage.Name = "English"

WorkingLanguage.LanguageCulture = "en-US"

We'll introduce more features in the next blog :)


Comments (4)