Page 1 of 1

Product Custom Fields in Email

Posted: Mon Nov 23, 2009 8:30 am
by jsmits
Is it possible to access ProductCustomFields through NVelocity for purposes of displaying in an email template?

Re: Product Custom Fields in Email

Posted: Mon Nov 23, 2009 8:57 am
by mazhar
Yes possible do something like this

Code: Select all

$product.CustomFields["CustomFieldName"].FieldValue

Re: Product Custom Fields in Email

Posted: Tue Nov 24, 2009 7:48 am
by jsmits
Sorry Mazhar, I can't quite seem to get it.

What I tried was:

Code: Select all

$orderItem.Product.CustomFields["Availability"].FieldValue
And this is the output:

CommerceBuilder.Products.ProductCustomFieldCollection["Availability"].FieldValue

Re: Product Custom Fields in Email

Posted: Tue Nov 24, 2009 8:15 am
by mazhar
EDIT MADE

Hmm give a try to this

Code: Select all

$orderItem.Product.CustomFields.get_Item('Availability').FieldValue
or if each product has only single product custom field then

Code: Select all

$orderItem.Product.CustomFields.get_Item(0).FieldValue

Re: Product Custom Fields in Email

Posted: Tue Nov 24, 2009 8:39 am
by jsmits
The latter worked so it doesn't look like get_Item will work by passing in the FieldName. For now my products only have the 'availability' field, but in the future they may have more. In that case I guess it might be possible to loop through them and only display the ones I need.

Thanks for your help.

Re: Product Custom Fields in Email

Posted: Thu Mar 18, 2010 10:41 am
by jsmits
Mazhar,

I know I am resurrecting an old thread, but I found a bug with the solution. If the item doesn't have a custom field then the email generation crashes as the index is out of range.

I tried to check if( $orderItem.Product.CustomFields.get_item(0) != null) and encountered the error:

Encountered "null" at line 102, column 64.
Was expecting one of:
"[" ...
"(" ...
<STRING_LITERAL> ...
"true" ...
"false" ...
<NUMBER_LITERAL> ...
<IDENTIFIER> ...
"{" ...
"!" ...
;

What is the proper way to check to see if the collection is null?

Re: Product Custom Fields in Email

Posted: Thu Mar 18, 2010 11:03 am
by mazhar
Try this one instead

Code: Select all

#if($orderItem.Product.CustomFields.Count > 0)
$orderItem.Product.CustomFields.get_Item(0).FieldValue
#end

Re: Product Custom Fields in Email

Posted: Thu Mar 18, 2010 11:06 am
by jsmits
Bingo! thx.