Page 1 of 1
Referring to Custom Data Fields in Scriptlets
Posted: Thu Oct 28, 2010 11:39 am
by dale.jones
The product being sold is a collection of maps organized by region. Each region can contain hundreds of individual maps. When a customer picks a region a query runs against an existing table and the detail information of the region is displayed. I got that to work by using the existing SKU field in the products table. Since this process only needs to run for specific categories, I modified the Show Product 1 scriptlet like this:
#if($Category.ParentId == 7)
[[Conlib:Custom/ChartDetail RegionID="$Product.SKU"]]
#end
This feeds $Product.SKU into the custom control, ChartDetail. Works just fine. Unfortunately, we need to use the SKU field to contain a SKU, not a RegionID.
I added the RegionID as a custom field to the products admin page using the 'How to add a Product Custom Field' post. (Many thanks to AbelMods!) That's all working just fine. I can add a RegionID from the admin page and it shows up in the database.
Now I need to grab the value from that custom field and plug it into the ChartDetail web control in the Show Product 1 scriptlet. I've tried many combinations of syntax, but nothing is working yet.
Thanks in advance.
Re: Referring to Custom Data Fields in Scriptlets
Posted: Fri Oct 29, 2010 6:12 am
by mazhar
Suppose your custom field name is Region, now on show product 1 we want to read value for that field. Then we can do something like this
Code: Select all
#foreach($pcf in $Product.CustomFields)
#if($pcf.FieldName == "Region")
Region:- $pcf.FieldValue
#end
#end
Re: Referring to Custom Data Fields in Scriptlets
Posted: Tue Dec 13, 2011 8:49 pm
by napacabs
How can one add this to a control such as ConLib:FeaturedProductsGrid?
Re: Referring to Custom Data Fields in Scriptlets
Posted: Wed Dec 14, 2011 1:57 pm
by david-ebt
Tom,
I've included some sample code below. It's based on a great post from AbleMods that explains how to use the custom fields. Take a look
viewtopic.php?f=47&t=12056.
You can add this code to your FeaturedProductsGrid.ascx.
Code: Select all
<asp:Label ID="CustomFieldLabel" runat="server" Text='<%# GetCustomField(Container.DataItem, "CustomField") %>'></asp:Label><br />
Here's the code to add to your FeaturedProductsGrid.ascx.cs.
Code: Select all
protected string GetCustomField(object dataItem, string fieldName)
{
Product product = (Product)dataItem;
foreach (ProductCustomField pcf in product.CustomFields)
{
if (pcf.FieldName == fieldName)
return pcf.FieldValue;
}
return string.Empty;
}
The second argument in the call is the name of your custom field. You can put as many of these on your control as you need or you could hard code which fields to return in the function. If you wanted to return the same set of custom fields every time, your function might look like this. Note you would have to adjust the code in the ascx file to only pass the product object.
Code: Select all
protected string GetCustomField(object dataItem)
{
Product product = (Product)dataItem;
string customValues = string.Empty;
foreach (ProductCustomField pcf in product.CustomFields)
{
if (pcf.FieldName == "CustomField")
{
customValues += string.Format("Custom Field 1 = {0}<br />", pcf.FieldValue);
}
else if (pcf.FieldName == "CustomField2")
{
customValues += string.Format("Custom Field 2 = {0}<br />", pcf.FieldValue);
}
}
return customValues;
}
That will hopefully get you started.
Re: Referring to Custom Data Fields in Scriptlets
Posted: Wed Dec 14, 2011 11:20 pm
by napacabs
David, code works perfectly, thank you very much for sharing.
Re: Referring to Custom Data Fields in Scriptlets
Posted: Fri Dec 23, 2011 10:13 am
by sfeher
Similar question:
I need to pull a Custom Field value into my email templates. I suspect that there's a method in NVelocity to do this, but I don't see any mention of it in the posts. Anyone else been successful in getting a custom field value to display in the product grids on the email templates, packing slips, pull sheets, etc?
Thanks!
Re: Referring to Custom Data Fields in Scriptlets
Posted: Fri Dec 23, 2011 12:28 pm
by david-ebt
Take a look at this forum post for using custom product fields to an email template:
viewtopic.php?f=42&t=11146&p=47679&hili ... eld#p47679