Referring to Custom Data Fields in Scriptlets

Store UI, layout, design, look and feel; Discussion on the customer facing pages of your online store. Cascading Style Sheets, Themes, Scriptlets, NVelocity and the components in the ConLib directory.
Post Reply
dale.jones
Ensign (ENS)
Ensign (ENS)
Posts: 5
Joined: Thu Oct 07, 2010 11:18 am

Referring to Custom Data Fields in Scriptlets

Post by dale.jones » Thu Oct 28, 2010 11:39 am

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.

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

Re: Referring to Custom Data Fields in Scriptlets

Post by mazhar » Fri Oct 29, 2010 6:12 am

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

User avatar
napacabs
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 45
Joined: Mon Jan 14, 2008 2:58 pm
Location: Chino, California
Contact:

Re: Referring to Custom Data Fields in Scriptlets

Post by napacabs » Tue Dec 13, 2011 8:49 pm

How can one add this to a control such as ConLib:FeaturedProductsGrid?

User avatar
david-ebt
Captain (CAPT)
Captain (CAPT)
Posts: 253
Joined: Fri Dec 31, 2010 10:12 am

Re: Referring to Custom Data Fields in Scriptlets

Post by david-ebt » Wed Dec 14, 2011 1:57 pm

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.
David
http://www.ecombuildertoday.com
Enhanced Reporting for AbleCommerce
Image

User avatar
napacabs
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 45
Joined: Mon Jan 14, 2008 2:58 pm
Location: Chino, California
Contact:

Re: Referring to Custom Data Fields in Scriptlets

Post by napacabs » Wed Dec 14, 2011 11:20 pm

David, code works perfectly, thank you very much for sharing.

sfeher
Captain (CAPT)
Captain (CAPT)
Posts: 220
Joined: Fri Jun 04, 2004 1:58 pm
Location: Steubenville, Ohio

Re: Referring to Custom Data Fields in Scriptlets

Post by sfeher » Fri Dec 23, 2011 10:13 am

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!

User avatar
david-ebt
Captain (CAPT)
Captain (CAPT)
Posts: 253
Joined: Fri Dec 31, 2010 10:12 am

Re: Referring to Custom Data Fields in Scriptlets

Post by david-ebt » Fri Dec 23, 2011 12:28 pm

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
David
http://www.ecombuildertoday.com
Enhanced Reporting for AbleCommerce
Image

Post Reply