Developer forum

Forum » Development » Voucher class, MarkAsUsed not working?

Voucher class, MarkAsUsed not working?

Marco Johannesen
Reply

We needed to make some additional options for the voucher discount, so we made a custom discount provider.

The problem is when we have to set the voucher as used, we try doing MarkAsUsed, but it dosen't seem to do anything.

 

What am i doing wrong here:

 

    [Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.eCommerce.Cart.CheckoutDoneOrderIsComplete)]
    public class OrderCompleteVoucherUpdate : Dynamicweb.Extensibility.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
        {
            var myArgs = (Dynamicweb.Notifications.eCommerce.Cart.CheckoutDoneOrderIsCompleteArgs)args;
            Order order = myArgs.Order;
            Voucher.MarkAsUsed(order.VoucherCode, order.ID);
        }
    }

 

It does nothing, i then tried updaing the fields manually - which works, but you don't have access to all Voucher fields (like discount and so on)

 

    [Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.eCommerce.Cart.CheckoutDoneOrderIsComplete)]
    public class OrderCompleteVoucherUpdate : Dynamicweb.Extensibility.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
        {
            var myArgs = (Dynamicweb.Notifications.eCommerce.Cart.CheckoutDoneOrderIsCompleteArgs)args;
            Order order = myArgs.Order;
            Voucher.MarkAsUsed(order.VoucherCode, order.ID);

            Voucher orderVoucher = Voucher.GetVoucherByCode(order.VoucherCode);
            orderVoucher.UsedOrderID = order.ID;
            orderVoucher.DateUsed = order.Date;
            orderVoucher.Status = "Used";
            orderVoucher.Save();
        }
    }

Any ideas on what im doing wrong?


Replies

 
Andrey Pospelov
Reply

Hello.

 The Voucher class also has function isValid. It has to return true in order for MarkAsUsed to save data to the database.

isValid itself checks if the list, to which the voucher belongs to is active, DateUsed is null and the discount associated wit this voucher list is active and not outdated.

 I think one of the conditions isn't met.

 

WBR Andrey.

 
Marco Johannesen
Reply

Hmmm....

 

I get the following error...

 

[6/26/2013 1:56:41 PM]:
Order 84820: Unexpected exception with the message: Method not found: 'Boolean Dynamicweb.eCommerce.Orders.SalesDiscounts.Voucher.isValid(System.String, Int32, System.String)'.

Logging 'Exception':
Type: System.MissingMethodException
Message: Method not found: 'Boolean Dynamicweb.eCommerce.Orders.SalesDiscounts.Voucher.isValid(System.String, Int32, System.String)'.
StackTrace:    at Dynamicweb.CustomModules.HjemIS.OrderCompleteVoucherUpdate.OnNotify(String notification, NotificationArgs args)
   at Dynamicweb.Extensibility.NotificationManager.Notify(String notification, Object[] args)
   at Dynamicweb.eCommerce.Cart.Frontend.CheckoutDone(Order order, Int32 pageID)

Any ideas?

 
Andrey Pospelov
Reply

Could you check if you really have the isValid function in the dynamicweb.dll you are referencing in your custom module?

 
Marco Johannesen
Reply

Well.... seems the API has changed, but their documention hasn't been updated.

Now IsValid takes 2 arguments, instead of 3.

 

IsValid returns false... but is there anyway to know why it returns false (which parameter)?

 

The only thing you supply is vouchercode and vouchercode list id.... and they are active... :-(

 
Marco Johannesen
Reply

Code:

 

using System;
using System.Collections.Generic;

using Dynamicweb;
using Dynamicweb.Extensibility;
using Dynamicweb.eCommerce.Prices;
using Dynamicweb.eCommerce.Orders;
using Dynamicweb.eCommerce.Common;
using Dynamicweb.eCommerce.Products;
using Dynamicweb.eCommerce.Orders.SalesDiscounts;
using Dynamicweb.Extensibility.Editors;
using System.Collections;

namespace Dynamicweb.CustomModules.HjemIS
{
    [AddInName("Voucher List Discount.")]
    [AddInDescription("Voucher list discount, with options.")]
    public class VoucherListDiscount : SalesDiscountProvider, IDropDownOptions
    {

        #region Fields
        private int _voucherListID;
        private int _minOrderValue;
        #endregion

        #region Properties
        [AddInParameter("Voucherlist"), AddInParameterEditor(typeof(Dynamicweb.Extensibility.Editors.DropDownParameterEditor), "")]
        public int VoucherListDropdown
        {
            get { return _voucherListID; }
            set { _voucherListID = value; }
        }

        [AddInParameter("Min. order value ")
        , AddInParameterEditor(typeof(Extensibility.Editors.TextParameterEditor), "")]
        public int OrderValueLimit
        {
            get { return _minOrderValue; }
            set { _minOrderValue = value; }
        }

        public Hashtable GetOptions(string optionName)
        {
            Hashtable htOptions = new Hashtable();
            VoucherListCollection list = VoucherList.GetAllVoucherLists();

            switch (optionName)
            {
                case "Voucherlist":
                    foreach (VoucherList item in list)
                    {
                        htOptions.Add(item.ID, item.ListName);
                    }
                    break;
            }

            return htOptions;
        }

        #endregion

        #region Methods

        public override void ProcessOrder(Order o)
        {
            try
            {
                if (IsGetDiscount(o))
                {
                    Voucher OrderVoucher = Voucher.GetVoucherByCode(o.VoucherCode);

                    AddDiscount(o, base.DiscountValue.Amount);
                }
            }
            catch (Exception ex)
            {
                o.DebuggingInfos.Add(new Order.DebuggingInfo(ex.ToString()));
                o.SaveDebuggingInfos();
            }
        }

        private bool IsGetDiscount(Order o)
        {
            if (!string.IsNullOrWhiteSpace(o.VoucherCode))
            {
                Voucher OrderVoucher = Voucher.GetVoucherByCode(o.VoucherCode);
                VoucherList OrderVoucherList = VoucherList.GetListByID(OrderVoucher.ListID);

                if (OrderVoucher.ListID == _voucherListID && OrderVoucher.DateUsed == null && o.TotalPrice >= _minOrderValue && OrderVoucher.Status != "Used" && OrderVoucherList.ListActive)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else {
                return false;
            }
        }

        private void AddDiscount(Order o, double discountPrice)
        {
            OrderLine line = new OrderLine();
            line.Order = o;
            line.Quantity = 1;
            line.Reference = "#";
            line.ProductName = this.DiscountName + " (" + o.VoucherCode +")";
            line.SetUnitPrice(GetCalculatedDiscountPrice(discountPrice));
            line.Type = Base.ChkString(Base.ChkNumber(OrderLine.OrderLineType.Discount));

            o.OrderLines.Add(line, false);
        }

        private double GetCalculatedDiscountPrice(double discountPrice)
        {
            PriceRaw rawprice = new PriceRaw(discountPrice, Application.DefaultCurrency);
            PriceCalculated calcPrice = new PriceCalculated(rawprice);
            calcPrice.PriceWithoutVAT = calcPrice.PriceWithVAT;
            calcPrice.VATPercent = 0;
            calcPrice.VAT = 0;

            return -1 * calcPrice.Price;
        }

        #endregion
    }

    [Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.eCommerce.Cart.CheckoutDoneOrderIsComplete)]
    public class OrderCompleteVoucherUpdate : Dynamicweb.Extensibility.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
        {
            var myArgs = (Dynamicweb.Notifications.eCommerce.Cart.CheckoutDoneOrderIsCompleteArgs)args;
            Order order = myArgs.Order;

            if(!string.IsNullOrWhiteSpace(order.VoucherCode)) 
            {
                Voucher orderVoucher = Voucher.GetVoucherByCode(order.VoucherCode);

                Voucher.isValid(order.VoucherCode, orderVoucher.ListID);
                Voucher.MarkAsUsed(order.VoucherCode, order.ID);

                orderVoucher.UsedOrderID = order.ID;
                orderVoucher.DateUsed = order.Date;
                orderVoucher.Status = "Used";
                orderVoucher.Save();
            }
        }
    }
}
 
Nicolai Høeg Pedersen
Reply

Hi Marco

 

Thanks for sharing!

 

BR Nicolai

 

You must be logged in to post in the forum