How to add a Product Custom Field
Re: How to add a Product Custom Field
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.
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
Re: How to add a Product Custom Field
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
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
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
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
Re: How to add a Product Custom Field
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:
with this code which will create a new section for MSDS file.
Then in the Admin/Products/EditProduct.aspx.cs page make two changes.
First insert a new reference at the top:
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.
with this code:
Then search for SaveButton_Click and replace this code. This is the code that will save or update the MSDS field.
and change to this:
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.
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 & SHIPPING
</td>
</tr>
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>
</th>
<td colspan="3">
<asp:FileUpload ID="MSDSUpload" runat="server" />
<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 & SHIPPING
</td>
</tr>
First insert a new reference at the top:
Code: Select all
using System.IO;
Code: Select all
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//BASIC INFO
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
Code: Select all
public void SaveButton_Click(object sender, EventArgs e)
{
ValidateFieldsLengths();
if (Page.IsValid)
{
//BASIC INFO
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
Note that this code relies heavily on the original code from AbleMods - so thanks to him again for his work on this.
Last edited by david-ebt on Fri Jan 06, 2012 6:49 pm, edited 1 time in total.
Re: How to add a Product Custom Field
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
But now get no definition for 'Combine'.
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
Re: How to add a Product Custom Field
I just noticed it corrected me again while typing. It's not supposed to be but like you had it. I changed it but still get has no definition error.
Code: Select all
FileAuthorizationModule.Delete(_MSDSpathname);
Code: Select all
File.Delete(_MSDSpathname);
Re: How to add a Product Custom Field
Sorry, I forgot to note that you need to add
to the top of the EditProduct.aspx.cs file to get those references.
Code: Select all
using System.IO;
Re: How to add a Product Custom Field
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.
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
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:
ShowMSDS.ascx.cx
As written, this would just be another ConLib control in the Show Product scriptlet, called like this:
[[ConLib:Custom\ShowMSDS]]
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>
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;
}
}
[[ConLib:Custom\ShowMSDS]]
Re: How to add a Product Custom Field
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.
Thanks again.
Re: How to add a Product Custom Field
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!
Glad to help!
-
- Commander (CMDR)
- Posts: 118
- Joined: Sat Dec 20, 2008 11:27 pm
Re: How to add a Product Custom Field
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
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.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.
-
- Commander (CMDR)
- Posts: 129
- Joined: Thu Oct 19, 2006 5:33 pm
Re: How to add a Product Custom Field
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
Thanks!
Greg
Re: How to add a Product Custom Field
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.
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
-
- Commander (CMDR)
- Posts: 129
- Joined: Thu Oct 19, 2006 5:33 pm
Re: How to add a Product Custom Field
Hi Joe -
Thanks for that info, and thanks for taking the time to write this tutorial - very helpful!
Greg
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
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
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
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com