nopcommerce中文网

nopcommerce是国外asp.net领域一个高质量的b2c开源项目,基于EntityFramework和MVC开发,QQ群1:75272942(2000人超级群,已满) QQ群2:640322459

导航 - 搜索

为nopcommerce自定义用户积分功能(2)

积分功能第二部,不了解情况的可以先看看第一部

nopcommerce的总体架构我们就不多说了,从WEB到逻辑再到数据库访问都有地方要修改,本文主要演示逻辑和数据库访问的修改,Web界面上的后边会提到如何调用。

首 先我们有一个新的reward point provider,那么在web.config的sectionGroup里要加上:<section name=”RewardProvider” type=”NopSolutions.NopCommerce.DataAccess.DBProviderSection, Nop.DataAccess” requirePermission=”false”/>

在nopDataProviders的最后也要加上:

<RewardProvider defaultProvider=”SQLRewardProvider”>
<providers>
<add name=”SQLRewardProvider” type=”NopSolutions.NopCommerce.DataAccess.Promo.SQLRewardProvider, Nop.DataAccess.SqlServer” connectionStringName=”NopSqlConnection”/>
</providers>
</RewardProvider>

NopContext里也要加个字段用来保存当前session下的积分值:

public decimal RewardPoint
{
get {
if (HttpContext.Current.Session["RewardPoint"] == null)
return decimal.Zero;

decimal ret = decimal.Zero;
if (decimal.TryParse(HttpContext.Current.Session["RewardPoint"].ToString(), out ret))
return ret;
else
return decimal.Zero;
}
set
{
HttpContext.Current.Session["RewardPoint"] = value;
}
}

现在再来看数据库抽象接口的代码,各个方法什么意思想必大家一看就懂:

namespace NopSolutions.NopCommerce.DataAccess.Promo
{
[DBProviderSectionName("nopDataProviders/RewardProvider")]
public abstract partial class DBRewardProvider : BaseDBProvider
{
public abstract bool RewardCalculation(decimal point, int customerID, int orderid, string details);
public abstract DBCustomerCollection CustomerRewardList();
public abstract DBRewardAuditCollection GetAllAuditList(DateTime from, DateTime to);
public abstract DBRewardAuditCollection GetAuditByOrderID(DateTime from, DateTime to, int OrderID);
public abstract bool RewardPointSetTo(decimal point, int customerID, string details);
}
}

DBRewardAudit的定义:

namespace NopSolutions.NopCommerce.DataAccess.Promo
{
public partial class DBRewardAudit : BaseDBEntity
{
public int RewardID { get; set; }
public int OrderID { get; set; }
public DateTime RewardTime { get; set; }
public string Status { get; set; }
public decimal Amount { get; set; }
public string RewardDetails { get; set; }
}
}

按照标配来说还得要个DBRewardAuditCollection,我形式上加了这个文件,但没实质内容,如果管理员要求在登录网店后看到历史记录,可以用这个。

数据层的DBCustomer还要加上个RewardPoint属性:

namespace NopSolutions.NopCommerce.DataAccess.CustomerManagement
{
public partial class DBCustomer : BaseDBEntity
{
public decimal RewardPoint { get; set; }
}
}

数 据库层的UpdateCustomer方法的参数要修改,把reward point属性传进去,SQL里update的时候也能更新积分,在这儿就省略了。DBOrder也要加同样的public decimal RewardPoint { get; set; }属性。InsertOrder方法也要把RewardPoint包含进来。

具体的数据访问层,我这儿有个SQLRewardProvider.cs供参考。SQLRewardProvider

然后再看看业务逻辑层的RewardManager.cs,RewardManager,简单的代码,就不用再描述了。

OK到这儿大部分接口已经实现了,只等客户端代码调用了,所有的逻辑层,web层的代码都可以称为客户端代码,那么我们来试试下订单后消耗积分的情况。OrderManager.cs 中的PlaceOrder,

….

Order order = InsertOrder(OrderGuid, //这儿注意,用刚才修改过的方法,把积分传进去。

….

等有了OrderID = order.OrderID;这句,订单也生成了,那么就要扣掉相应的积分了,直接在这句后边加上:

//reward function
if (NopContext.Current.RewardPoint > customer.RewardPoint)//reward point cannot greater than customer’s point.
NopContext.Current.RewardPoint = customer.RewardPoint;

if (OrderID >= 0 && NopContext.Current.RewardPoint > decimal.Zero)//success, deduct reward points
RewardManager.RewardCalculation(decimal.Zero – NopContext.Current.RewardPoint, customer.CustomerID, OrderID,”Order placed with ” + RewardManager.RewardName + NopContext.Current.RewardPoint.ToString());

然后再接着往下走找到SendOrderPlacedStoreOwnerNotification和SendOrderPlacedCustomerNotification,里边的方法改改,让email可以支持reward point的消息:

首先要定义这些消息的token是什么,在GetListOfAllowedTokens里添加:

//added comments and reward points – by dingsea:
allowedTokens.Add(“%Order.RewardPointApplied%”);
allowedTokens.Add(“%Customer.CurrentRewardPoint%”);
allowedTokens.Add(“%Customer.WinRewardPoint%”);
allowedTokens.Add(“%Settings.RewardName%”);
//ends here.

ReplaceMessageTemplateTokens里加上如下字段以方便在正则替换的时候找到对应的文字:

//added: dingsea
tokens.Add(“Order.RewardPointApplied”, HttpUtility.HtmlEncode(order.RewardPoint.ToString(“N2″)));
tokens.Add(“Customer.WinRewardPoint”,HttpUtility.HtmlEncode((order.OrderTotal * RewardManager.RewardPercentage).ToString(“N2″)));
tokens.Add(“Customer.CurrentRewardPoint”,HttpUtility.HtmlEncode(NopContext.Current.User.RewardPoint.ToString(“N2″)));
tokens.Add(“Settings.RewardName”,HttpUtility.HtmlEncode(RewardManager.RewardName));

既然都到这里了,那也把nopcommerce的一个小BUG也补上:

ProductListToHtmlTable方法中没有打折信息(在order details页面也有同样问题,同学们自行加上吧),所以我把打折和积分信息一起加上了:

//fixed by dingsea – discount is missing
string CusDiscount = string.Empty;
string CusRewardPoint = string.Empty;

……

//fixed by dingsea – discount and reward point
CusDiscount = order.OrderDiscount == decimal.Zero ? string.Empty : PriceHelper.FormatPrice(order.OrderDiscount, true, order.CustomerCurrencyCode, language, false);
CusRewardPoint = order.RewardPoint.ToString(“N2″);

…….

//fixed by dingsea – discount and reward point
if(CusDiscount!=string.Empty)
sb.AppendLine(“<tr><td style=\”text-align:right;\” colspan=\”3\”><strong>” + LocalizationManager.GetLocaleResourceString(“ShoppingCart.Sub-TotalDiscount”, LanguageID) + “</strong></td> <td style=\”text-align:right;\”><strong>” + CusDiscount + “</strong></td></tr>”);
sb.AppendLine(“<tr><td style=\”text-align:right;\” colspan=\”3\”><strong>” + LocalizationManager.GetLocaleResourceString(“Promotion.RewardPointName”, LanguageID) + “</strong></td> <td style=\”text-align:right;\”><strong>” + CusRewardPoint + “</strong></td></tr>”);
//fix ends

于是呼,在你的template->message template里订单生成时向用户和店主发的email里就可以支持积分的token了。

至于在web如何调用,那就比较容易了,在你想要的页面加上一个textbox,确保用户输入的是decimal,那就够了,这个数字会存到session里直到用户下单。而且在管理界面,利用rewardmanager的方法可以直接操作积分,方便快捷。

blog comments powered by Disqus