Alrighty. Everyone knows how to set a coupon to work for specific customers only. How about for specific customer levels? Well it requires source code, but here's a quick step-by-step using ExtensionData. For this demonstration, I'll use C# and version 8.1.4 since it is the latest ML8 as of this writing. Keep in mind line numbers are approximates so please be sure you know what you're looking for, and have a solid backup of your original files.
First thing you want to do is set up the environment to allow for it:
1. Make a string resource in your admin. Name it "shoppingcartcoupon.cs.1" with a value of something along the lines of "This coupon is invalid for your customer level."
2. Open editCoupons.aspx. At or around line 11, add:
Code:
$window_addLoad(function() { { new ToolTip('imgCustomerLevels', 'AdminSiteTooltip', '<font class=\"exampleText\">Enter the customer level id(s) for which this coupon is valid, or leave blank to allow any customer level to use it. Enter customer level id\'s separated by a comma, e.g. 1, 3, 9, etc...</font>'); } })
You'll see where it goes. Right with all the others.
3. Add a table row right below "Valid for customers". This should be at or around line 206:
Code:
<tr><td align="right" valign="middle"><font class="subTitleSmall">Valid For Customer Level(s):</font></td><td align="left" valign="top"><asp:TextBox ID="txtCustomerLevels" runat="server"></asp:TextBox><asp:Literal runat="server" ID="literalCustomerLevels"></asp:Literal><img id="imgCustomerLevels" src="skins/skin_1/images/info.gif" border="0" href="javascript:void(0);" style="cursor: normal;" /></td></tr>
This will give us a place to edit the coupon for Customer Levels.
4. We now alter the editCoupon.aspx.cs file to make it work. Open editCoupons.aspx.cs. At line 82, add:
Code:
txtCustomerLevels.Text = Server.HtmlEncode(DB.RSField(rs, "ExtensionData"));
5.On line 198, change the SQL statement to the following:
Code:
sql.Append("insert into coupon(CouponGUID,CouponCode,CouponType,ExpirationDate,Description,DiscountPercent,DiscountAmount,DiscountIncludesFreeShipping,ExpiresOnFirstUseByAnyCustomer,ExpiresAfterNUses,NumUses,ExtensionData,ExpiresAfterOneUsageByEachCustomer,ValidForCustomers,ValidForProducts,ValidForManufacturers,RequiresMinimumOrderAmount,ValidForCategories,ValidForSections,NotValidForProducts,ExcludeSaleProduct) values(");
Take note "ExtensionData" is expected after "NumUses".
6. On line 213, under the line where NUses is updated, add the following:
Code:
sql.Append(DB.SQuote(txtCustomerLevels.Text) + ",");
7. On Line 250, add the following line to the update command:
Code:
sql.Append("ExtensionData=" + DB.SQuote(txtCustomerLevels.Text) + ",");
Now the update should work.
8. In method "ResetForm" at or around line 281, clear the new field with the following:
Code:
txtCustomerLevels.Text = "";
We have the value editable, and it updates and saves and retrieves. Now we have to use the data to validate the potential coupon based on our list of valid customer levels. Everything else happens in the source code in our ShoppingCart.cs class.
1. In the public struct CouponStruct, we're going to add a public variable:
Code:
public String m_ValidForCustomerLevels;
2. Find GetCouponOrGiftCard method. Add the following line in the first "if" block:
Code:
myCoupon.m_ValidForCustomerLevels = Regex.Replace(DB.RSField(rscoup, "ExtensionData"), "\\s+", "", RegexOptions.Compiled);
You'll see where it goes. There's a handy space for it just above ValidForCustomers.
3. Find GetNewCoupon method. Within the rs.Read() if block, add the following:
Code:
coupon.m_ValidForCustomerLevels = Regex.Replace(DB.RSField(rs, "ExtensionData"), "\\s+", "", RegexOptions.Compiled);
Again, you'll see where it goes.
4. Find ValidateNewCoupon method and in the validation block:
Code:
if (status == AppLogic.ro_OK){
if (coupon.m_ValidForCustomerLevels.Length != 0 && !CommonLogic.IntegerIsInIntegerList(m_ThisCustomer.CustomerLevelID, coupon.m_ValidForCustomerLevels)){
status = AppLogic.GetString("shoppingcartcoupon.cs.1", this.SkinID, m_ThisCustomer.LocaleSetting);
}
}
5. Find ClearCoupon method. Add in the declaration:
Code:
m_Coupon.m_ValidForCustomerLevels = String.Empty;
6. Find SetCoupon method. Add inside the first rs.Read() conditional block:
Code:
m_Coupon.m_ValidForCustomerLevels = Regex.Replace(DB.RSField(rs, "ExtensionData"), "\\s+", "", RegexOptions.Compiled);
7. Find CheckIfCouponIsValid method. Add the conditional block:
Code:
if (status == AppLogic.ro_OK){
if (m_Coupon.m_ValidForCustomerLevels.Length != 0 && !CommonLogic.IntegerIsInIntegerList(m_ThisCustomer.CustomerLevelID, m_Coupon.m_ValidForCustomerLevels)){
status = AppLogic.GetString("shoppingcartcoupon.cs.1", m_SkinID, m_ThisCustomer.LocaleSetting);
}
}
Now we are done. When you make your coupons in the admin, simply provide the comma delimited list of allowed CustomerLevelIDs. If you import your coupons, put that list in the ExtensionData field using the same comma delimited structure. Whenever a customer moves to use a coupon, they will be checked and validated or not based on CustomerLevel as well as all other criteria. GiftCards can be done in a similar fashion, but is beyond the scope of this offering.
Ex-Jesse