Developer forum

Forum » Development » Extend a SalesDiscountProvider

Extend a SalesDiscountProvider

Diogo Lino
Reply
Hello,

I'm trying to create a new SalesDiscountProvider to validate voucher codes from diferent tables from a database in C#.
I'm using version 19.1.0.5 ans Cart v1, for now i cannot change that.
I've tried to use the two functions "CreateProductOrderline" and "CreateAmountOrderline" as I saw in an example, but it says it cannot access internal method...

    [AddInName("One use Voucher discount")]
    [AddInDescription("If the user introduces a valid voucher number a discount will be triggered.")]
    public class OneUseVoucherDiscount : SalesDiscountProvider, IDropDownOptions
    {
        #region Parameters
        [AddInParameter("OrderField"), AddInParameterEditor(typeof(Dynamicweb.Extensibility.Editors.DropDownParameterEditor), "")]
        public string OrderFieldParameter { get; set; }
        
        [AddInParameter("VoucherTable"), AddInParameterEditor(typeof(Dynamicweb.Extensibility.Editors.DropDownParameterEditor), "")]
        public string VoucherTableParameter { get; set; }
        #endregion

        public override void ProcessOrder(Order order)
        {
            if (order.OrderFields.Contains(OrderFieldParameter))
            {
                string voucherCode = order.OrderFieldValues.GetOrderFieldValue(OrderFieldParameter).Value.ToString();
                
                //TODO seeks if exists on a database table
                
                bool isVoucherValid = true; //TODO let's assume it was found

                if (isVoucherValid)
                {
                    if (DiscountValue.Type == DiscountTypes.Products)
                    {
                        foreach (Products.Product product in DiscountValue.ProductCollection)
                        {
                            OrderLine line = CreateProductOrderline(order, product);


I also want to subscribe an order complete event as shown at http://developer.dynamicweb-cms.com/documentation/for-developers/ecommerce/extensibility/notifications/cart-v2.aspx but it does not recognize the "Dynamicweb.Notifications.eCommerce.Cart.CheckoutDoneOrderIsCompleteArgs" notification. How can i catch the order complete?

Thanks,
Diogo Lino

Replies

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Diogo

It's correct, CreateProductOrderline and CreateAmountOrderline are internal methods. I don't know where you've seen these methods used in an example but take a look at this: http://developer.dynamicweb-cms.com/documentation-8.0/for-developers/(moved)-ecommerce/(moved)-extensibility/providers/sales-discount-providers.aspx. The code is Visual Basic but I think you'll manage :) Pay particular attention the the if-block at line 60. This shows how to create the orderlines you need.

With regard to the notification, you're trying to listen for a Cart v2 notification yet you state at the very beginning of your post, that you're using Cart v1 and you can't change that. In Cart v1 you want to listen for "Dynamicweb.Notifications.eCommerce.Order.Steps.Completed".

Hope this helps :)

- Jeppe

 
Diogo Lino
Reply
Hi,

Thanks for such a quick answer. :)

Diogo Lino

 
Diogo Lino
Reply
One more thing, when using the subscriber to the order complete event, I can't use the Dynamicweb.Notifications.eCommerce.Cart.CheckoutDoneOrderIsCompleteArgs, so how can I get the order object to work with its Id and find the discount line (to get the voucher code)?

Thanks,
Diogo Lino
 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
This post has been marked as an answer

The notifications for Cart v1 use the old way of doing notifications where you get an object array as the parameter instead of the NotificationArgs object. The object array for "Steps.Completed" contains these objects in this order: Order object, Extranet (User), PageView.
You probably only need the first one, the Order object. Something like this in your Subscriber is probably what you want:

 

public override void OnNotify(string Notification, object[] args)
{
    if (args.Length > 0 && args[0] != null)
    {
        var order = (Order)args[0];
    }
}
Votes for this answer: 0

 

You must be logged in to post in the forum