Re: How to add a Product Custom Field
Posted: Wed Jan 04, 2012 2:01 pm
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.
http://forums.ablecommerce.com/
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>
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
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
Code: Select all
FileAuthorizationModule.Delete(_MSDSpathname);
Code: Select all
File.Delete(_MSDSpathname);
Code: Select all
using System.IO;
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;
}
}
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.