Page 2 of 2

Re: How to add a Product Custom Field

Posted: Wed Jan 04, 2012 2:01 pm
by AbleMods
It's been a few years since that code was written. It may not work with 7.0.5 since this post (and the code) was written prior to the release of 7.0.5.

Re: How to add a Product Custom Field

Posted: Wed Jan 04, 2012 5:05 pm
by RKS_213
That's what I was thinking. So far I haven't been able to find anything to work adding custom fields to products. Every tutorial looks to be written for something older than 7.0.5.

Re: How to add a Product Custom Field

Posted: Wed Jan 04, 2012 5:30 pm
by AbleMods
Most people don't use them nowadays, they use merchant-side fields in product templates.

Adding a custom field and implementing it on the shopper side gets "involved" pretty quickly. AC7 has matured quite a bit since the early days, so there's more complexity than there used to be :)

Re: How to add a Product Custom Field

Posted: Wed Jan 04, 2012 7:10 pm
by david-ebt
Here is some code I tried out in 7.0.7 that will let you upload a file from the product edit page in admin. It should work without any modifications in 7.0.5. I've named everything assuming this is an MSDS file.

In the Admin/Products/EditProduct.ascx file, search for the word TAXES. Replace this code:

Code: Select all

<tr class="sectionHeader">
    <td colspan="4">
        TAXES &AMP; SHIPPING
    </td>
</tr>
with this code which will create a new section for MSDS file.

Code: Select all

<tr class="sectionHeader">
    <td colspan="4">
        MSDS SHEET
    </td>
</tr>
<tr>
    <th class="rowHeader" nowrap>
        <cb:ToolTipLabel ID="MSDSLabel" runat="server" Text="Current MSDS File" ToolTip="MSDS sheet for this product."></cb:ToolTipLabel>
    </th>
    <td colspan="3">
	    <asp:Label ID="MSDS" runat="server" />
    </td>
</tr>
<tr>
    <th class="rowHeader" nowrap>
        &nbsp;
    </th>
    <td colspan="3">
	    <asp:FileUpload ID="MSDSUpload" runat="server" />&nbsp;
        <cb:ToolTipLabel ID="MSDSUploadLabel" runat="server" Text="" ToolTip="Select MSDS sheet to upload for this product."></cb:ToolTipLabel>
    </td>
</tr>
<tr class="sectionHeader">
    <td colspan="4">
        TAXES &AMP; SHIPPING
    </td>
</tr>
Then in the Admin/Products/EditProduct.aspx.cs page make two changes.

First insert a new reference at the top:

Code: Select all

using System.IO;
Then search for the Page_Load function and replace this code. This loads the MSDS file name from the custom fields when the page loads.

Code: Select all

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        //BASIC INFO
with this code:

Code: Select all

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        //BASIC INFO

        // BEGIN MSDS MOD
        string currentMSDS = "No MSDS specified";
        foreach (ProductCustomField _PCF in _Product.CustomFields)
        {
            if (_PCF.FieldName == "MSDS")
            {
                currentMSDS = _PCF.FieldValue;
                break;
            }
        }
        MSDS.Text = current
Then search for SaveButton_Click and replace this code. This is the code that will save or update the MSDS field.

Code: Select all

public void SaveButton_Click(object sender, EventArgs e)
{
    ValidateFieldsLengths();
    if (Page.IsValid)
    {
        //BASIC INFO
and change to this:

Code: Select all

public void SaveButton_Click(object sender, EventArgs e)
{
    ValidateFieldsLengths();
    if (Page.IsValid)
    {
        //BASIC INFO

        // BEGIN MSDS MOD
        if (MSDSUpload.HasFile)
        {
            string _MSDSpathname = Path.Combine(Server.MapPath("/PDF/"), MSDSUpload.FileName);
            string _MSDSfilename = "/PDF/" + MSDSUpload.FileName;
            if (File.Exists(_MSDSpathname))
            {
                File.Delete(_MSDSpathname);
            }
            MSDSUpload.SaveAs(_MSDSpathname);

            MSDS.Text = _MSDSfilename;

            bool _PCFExists = false;

            foreach (ProductCustomField _PCF in _Product.CustomFields)
            {
                if (_PCF.FieldName == "MSDS")
                {
                    _PCF.FieldValue = _MSDSfilename;
                    _PCF.Save();
                    _PCFExists = true;
                    break;
                }
            }

            if (!_PCFExists)
            {
                ProductCustomField _NewPCF = new ProductCustomField();
                _NewPCF.ProductId = _Product.ProductId;
                _NewPCF.FieldName = "MSDS";
                _NewPCF.FieldValue = _MSDSfilename;
                _NewPCF.Save();
            }
        }
        // END MSDS MOD
This should do it.

Note that this code relies heavily on the original code from AbleMods - so thanks to him again for his work on this.

Re: How to add a Product Custom Field

Posted: Thu Jan 05, 2012 11:15 am
by RKS_213
Thanks, David. Are you sure HasFile exists in SaveButton_Click? The library has it only in UploadButton_Click. I tried what you have above but got a CS0117: 'System.Web.UI.WebControls.PathDirection' does not contain a definition for 'HasFile'.

I deleted from the SaveButton and added a new UploadButton_Click and had to change the code like so

Code: Select all

//BEGIN MSDS MOD
    protected void UploadButton_Click(object sender, EventArgs e)
    {
        if (MSDSUpload.HasFile)
            SaveFile(MSDSUpload.PostedFile);
        else
        { //do nothing
        }
        }
    void SaveFile(HttpPostedFile file)
    {
        string _MSDSpathname = PathDirection.Combine(ServerValidateEventArgs.MapPath("/PDF/"), MSDSUpload.Filename);
        string _MSDSfilename = "/PDF/" + MSDSUpload.FileName;
        if (System.IO.File.Exists(_MSDSpathname))
        {
            FileAuthorizationModule.Delete(_MSDSpathname);
        }
        MSDSUpload.SaveAs(_MSDSpathname);
        _MSDSfilename.Text = _MSDSfilename;

        bool _PCFExists = false;

        foreach (ProductCustomField _PCF in _Product.CustomFields)
        {
            if (_PCF.Fieldname == "MSDS")
            {
                _PCF.FieldValue = _MSDSfilename;
                _PCF.Save();
                _PCFExists = true;
                brak;
            }
        }
        if (!_PCFExists)
        {
            ProductCustomField _NewPCF = new ProductCustomField();
            _NewPCF.FieldName = "MSDS";
            _NewPCF.FieldValue = _MSDSfilename;
            _NewPCF.Save();
        }
    }
    //END MSDS MOD
But now get no definition for 'Combine'.

Re: How to add a Product Custom Field

Posted: Thu Jan 05, 2012 11:19 am
by RKS_213
I just noticed it corrected me again while typing. It's not supposed to be

Code: Select all

FileAuthorizationModule.Delete(_MSDSpathname);
but

Code: Select all

File.Delete(_MSDSpathname);
like you had it. I changed it but still get has no definition error.

Re: How to add a Product Custom Field

Posted: Thu Jan 05, 2012 1:06 pm
by david-ebt
Sorry, I forgot to note that you need to add

Code: Select all

using System.IO;
to the top of the EditProduct.aspx.cs file to get those references.

Re: How to add a Product Custom Field

Posted: Thu Jan 05, 2012 4:09 pm
by RKS_213
After all this time I had started to lose hope. Thanks, David. It works perfectly. The field was added to the edit product page (I will go back and copy it into the add products as well.) After I upload the pdfs they get saved to the database.

Also I have been successful in creating a ConLib to output the field on my product display page. One thing I did notice is that the output isn't a hyperlink. Is that an easy fix or pretty complex. I definitely don't want you troubling yourself any more since you helped me a great deal already.

Again, thanks. This has been my *almost* exclusive focus this entire week. I can get on to bigger and better things now so thanks a million.

Re: How to add a Product Custom Field

Posted: Thu Jan 05, 2012 5:22 pm
by david-ebt
I'm glad you've got it working - happy to help!

Here is a control that will display the MSDS sheet as a download link.

ShowMSDS.ascx:

Code: Select all

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ShowMSDS.ascx.cs" Inherits="ConLib_MSDS" EnableViewState="false" %>
<%--
<conlib>
<summary>Displays link to MSDS download.</summary>
</conlib>
--%>

<div style="margin-top:10px"><asp:HyperLink ID="MSDSlink" runat="server" Text="Download MSDS Sheet" Target="_blank"></asp:HyperLink></div>
ShowMSDS.ascx.cx

Code: Select all

using System;
using CommerceBuilder.Products;
using CommerceBuilder.Utility;

public partial class ConLib_MSDS : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int _ProductId = AlwaysConvert.ToInt(Request.QueryString["ProductId"]);

        ProductCustomFieldCollection customFields = ProductCustomFieldDataSource.LoadForProduct(_ProductId);

        foreach (ProductCustomField pcf in customFields)
        {
            if (pcf.FieldName == "MSDS")
            {
                MSDSlink.NavigateUrl = pcf.FieldValue;
            }
        }

        if (MSDSlink.NavigateUrl == null)
            this.Visible = false;
    }
}
As written, this would just be another ConLib control in the Show Product scriptlet, called like this:
[[ConLib:Custom\ShowMSDS]]

Re: How to add a Product Custom Field

Posted: Fri Jan 06, 2012 10:28 am
by RKS_213
Works perfectly, David. Thanks again for doing all this work. I've since gone back and tested a few more of your posts and many of them work with the "using System.IO" addition. So that's what I was missing and that's why I've been having a lot of trouble following things here in the forums.

Thanks again.

Re: How to add a Product Custom Field

Posted: Fri Jan 06, 2012 10:57 am
by david-ebt
I'm glad it's working for you now. I often forget about the references since they just drop in once and then the coding work begins.

Glad to help!

Re: How to add a Product Custom Field

Posted: Thu Mar 07, 2013 3:18 pm
by euroluxantiques
If anyone is wondering, I just used this thread to implement a custom field in Gold R4, and it still works. The only exception being the deprecated ProductId needs to be changed to "Id," but VS2010 told me that. I used it to add another tabbed panel in product display to show shipping details, and it worked great.

Re: How to add a Product Custom Field

Posted: Tue Mar 19, 2013 10:56 am
by mazhar
euroluxantiques wrote:If anyone is wondering, I just used this thread to implement a custom field in Gold R4, and it still works. The only exception being the deprecated ProductId needs to be changed to "Id," but VS2010 told me that. I used it to add another tabbed panel in product display to show shipping details, and it worked great.
We tried to keep gold API backward compatiable so most part of custom codes should work with Gold. There are some very common changes and I actully noted some of them down here viewtopic.php?f=47&t=17108. So in case any one is trying to port some old code it would be helpful to read that post.

Re: How to add a Product Custom Field

Posted: Mon Jun 22, 2015 7:52 am
by gdelorey@mitcs.com
Digging up an old post here - I've implemented a custom product field and have a question regarding setting this via the Data > Import > Products tool. Is there any way to allow my custom field to be imported with the new import tools in Gold?

Thanks!
Greg

Re: How to add a Product Custom Field

Posted: Mon Jun 22, 2015 9:52 am
by AbleMods
No the Gold product import doesn't support custom fields. A separate custom import routine would have to be built to accommodate product custom fields.

Re: How to add a Product Custom Field

Posted: Mon Jun 22, 2015 9:57 am
by gdelorey@mitcs.com
Hi Joe -

Thanks for that info, and thanks for taking the time to write this tutorial - very helpful!

Greg

Re: How to add a Product Custom Field

Posted: Mon Jun 22, 2015 10:38 am
by AbleMods
Glad you found it useful.

I miss writing the tutorials. I enjoy finding new and creative ways to utilize existing or obscure Able functionality.

A few months ago, I figured out how to use the little-known Gift Wrap feature to offer an up-charge'd engraving option right on the product page. No variants, no product templates. I like documenting stuff like that.

Nowadays, it's write it, test it and move on - too much work and too little time to play :)