Developer forum

Forum » Development » Create Voucher by API

Create Voucher by API

Martijn Bokhove
Reply

Hi

Is it possible to generate a vouchercode code behind with the API?

I'm using DW8, but don't see the voucher as part of the API.

 

We have a wish from our customer to send an personal vouchercode after registering an account in the webshop, that's why we are looking to generate a vouchercode codebehind.

 

Gr

Martijn


Replies

 
Andrey Pospelov
Reply

Hi Martijn.

 The Vouchers API is not described in the doc yet.

It'll be updated soon, after the nearest major release.You'll be able to find the description here http://developer.dynamicweb-cms.com/api8/

Meanwhile the Voucher classes can be found in this namespace:

eCommerce.Orders.SalesDiscounts

Method to generate vouchers can be found in this class: eCommerce.Orders.SalesDiscounts.Voucher

 

WBR Andrey.

 
Martijn Bokhove
Reply

Hi Andrey

 

Thanks for your support. I now see several options in the eCommerce.Orders.SalesDiscounts part for the Vouchers.

Only if I try to create a new Voucher, I cannot call the function:

eCommerce.Orders.SalesDiscounts.Voucher.GenerateVouchers

 

It needs 2 parameters, Number and ListID. If I fill in these values, Visual Studio says "Expression does not produce a value", so I can't use the function.

 

Is this an known issue?

 

Gr

Martijn

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

>> Expression does not produce a value

Are you trying this in an immediate window or watch window? If so, this is standard Visual Studio behavior for void methods and doesn't mean the function doesn't get execute correctly. Can you define "can't use the function"? Did you check the database for any changes to the vouchers?

Imar

 
Andrey Pospelov
Reply

Hello.

 Visual Studio is right. There is no return value from this function.

When you call it, it inserts the codes into the EcomVouchers table In the database.

Number is is the number of vouchers you want to generate.

ListID is the ID of the previously created list you want to generate the vouchers to.

WBR Andrey.

 

 

 
Martijn Bokhove
Reply

Hi Andrey & Imar

Ok it's clear now, the will work but not the way I hoped.

The only way to work with it codebehind is to use the function and loop trough the voucherlist to get the created voucher.

For sending a voucher to a new customer we have to generate a new voucherlist en create one voucher in that list, to make sure we can read the right voucher.

Will it be possible in the future to generate a single voucher, like it works in the frontend?

 

Gr

Martijn

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi Martjn,

I see your point. It would be nice if GenerateVouchers returned an IEnumerable so you knew which codes were generated. That would make it easy to generate a single voucher in an existing list and retrieve its code. For now you can work around that though by checking if the voucher is used. Something like this should work:

VoucherList voucherList = VoucherList.GetListByID(8);
if (voucherList != null)
{
  Voucher.GenerateVouchers(1, voucherList.ID);
  Voucher voucher = Voucher.GetAllVouchersForList(voucherList.ID).OrderBy(x => x.ID).Last(x => x.DateUsed == null);
  string code = voucher.Code;
}

This code gets a VoucherList based on a known ID. It then generates a new voucher and then queries the last available voucher.

Note that this could lead to issues on high volume websites as you may end up with someone else's voucher code. You could make it more bullet proof by first checking the number of vouchers (or the highest ID) before you do the insert and then check if you got what you expected.

Also note: this isn't very efficient code; the GetAll methods retrieves all vouchers and only one is used. This would be more efficient if GenerateVouchers returned the generated Vouchers. Then you could do this:

VoucherList voucherList = VoucherList.GetListByID(8);
if (voucherList != null)
{
  Voucher voucher = Voucher.GenerateVouchers(1, voucherList.ID).First();
  string code = voucher.Code;
}

Hope this helps,

 

Imar

 

You must be logged in to post in the forum