Developer forum

Forum » Development » % Discount provider - show saving on each orderline

% Discount provider - show saving on each orderline

Martin Nielsen
Reply
Hi,

I've created a SalesDiscountProvider that applies a 20% discount based on a custom voucher code.
When my Provider is run i can extend the order inside the "ProcessOrder" and i've added an extra Orderline containing info about the discount.

What i need now is to calculate the discount pr. orderline, and update the unitprice to make the math work.

Inside the "ProcessOrder" i do this, but my price isn't updated when viewing the card:

Replies

 
Martin Nielsen
Reply
// Loop all orderlines and substract vouchervalue percentage
foreach ( OrderLine oLine in order.OrderLines )
{
  // Calculate amount saved pr. row
  double rowUnitPrice = oLine.UnitPrice.Price;
  double discPrice = rowUnitPrice * discountpercent;
  double newRowPrice = rowUnitPrice - discPrice;
  // Set new unit price
  oLine.SetUnitPrice( newRowPrice );
  
  if ( oLine.OrderLineFieldValues != null ) {
    // Save "Discount amount" to customfield.
    if ( oLine.OrderLineFieldValues.Count > 0 ) {
      foreach ( OrderLineFieldValue fVal in oLine.OrderLineFieldValues )
      {
        if ( fVal.OrderLineFieldSystemName.Equals( "DiscountValue" ) )
        {
          fVal.Value = discPrice.ToString();
          break;
        }
      }
    } else {
      OrderLineFieldValue olv = new OrderLineFieldValue( "DiscountValue", discPrice.ToString() );
      oLine.OrderLineFieldValues.Add( olv );
    }
  }
  oLine.Save(oLine.ID);
}

 

 
Martin Nielsen
Reply

Am i doing it wrong, or in the wrong place?

Any advice is appreciated.

Regards
 Martin

PS. Code insertion doesn't work very well. It doesn't insert the codesnippet where the cursor was, instead it's places as the first thing inside the editor. (Sitting in IE9).

 

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Yes, the editor has some major usability issues. I already reported it, and a backlog item has been added to TFS. Hopefully this gets addressed real soon.

AFAIK, you shouldn't update the price of each item. Instead you need to create a new order line of type discount with a value that equals the combined discount of all products. You may want to take a look at the documentation here: http://developer.dynamicweb-cms.com/documentation/for-developers/ecommerce/sales-discount-providers.aspx for an example.

Cheers,

Imar
 

 
Martin Nielsen
Reply

Hi Imar,

That is how it works now, but our client would like to have the discount pr. orderline.
Is this at all possible?

Shouldn't oLine.SetUnitPrice( newRowPrice ); update the price on the orderline?

// Martin

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
AFAIK, no. The discount is calculated again each time the cart is shown and if I am not mistaken, it always takes the actual product prices into account.

What you could do is use an OrderLineTemplateExtender and make it *appear* as if prices have changed. The actual order discount will still be added as a separate line in the OrderLines table though....

Hope this helps,

Imar
 
Martin Nielsen
Reply

I was thinking down that path aswell, but it doesn't feel like the right way to go.

I hope our client can live without this feature.


// Martin

 

You must be logged in to post in the forum