Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial how-to guide of implementing deposit price adjuster #65

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
77 changes: 77 additions & 0 deletions content/core/1.4.0/how-to-guides/add-custom-deposit-fee.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
title: Add Custom Deposit Fee
description: How-To Guide to add custom deposit fee in Vendr, the eCommerce solution for Umbraco v8+
---

In this guide we will be looking at price adjustments in Vendr to be able to add custom fee when to total price reach a specific price.

## DepositPriceAdjustment

We define a custom `PriceAdjustment` making it possible to enhance with custom properties we need and when accessing adjustments we can cast to this specific type.

````csharp
[Serializable]
public class DepositPriceAdjustment : PriceAdjustment<DepositPriceAdjustment>
{
public string DepositAdjustmentRef { get; set; }

// A parameterless constructor is required for cloning
public DepositPriceAdjustment()
: base()
{ }

// Additional helper constructors
public DepositPriceAdjustment(string name, string reference, Price adjustment)
: base(name, adjustment)
{
DepositAdjustmentRef = reference;
}
}

````

## DepositPriceAdjuster

Furthermore we need a `PriceAdjuster` to implement thie calculation of the price, which in this case will be adding a fee. In other cases you may want add a discount instead by adding a negative price value.

````csharp
public class DepositPriceAdjuster : PriceAdjusterBase
{
public override void ApplyPriceAdjustments(PriceAdjusterArgs args)
{
// Add fee if total price is more than £1000
if (args.Calculation.TotalPrice.Value.WithTax >= 1000)
{
var taxRate = args.Calculation.TaxRate.Value;

var fee = 100M;
var tax = fee * taxRate / (1 + taxRate);
var priceWithoutTax = fee / (1 + taxRate);

// Create a £100 fee
var price = new Price(priceWithoutTax, feeTax, args.Order.CurrencyId);
var adjustment = new DepositPriceAdjustment("Deposit", "deposit", price);

args.SubtotalPriceAdjustments.Add(adjustment);
}
}
}

````

## Register price adjuster

Finally we need to register the Vendr price adjuster in a `IComposer`.

````csharp
[ComposeAfter(typeof(VendrWebComposer))]
public class StoreComposer : IUserComposer
{
public void Compose(Composition composition)
{
// Register price adjuster
composition.WithPriceAdjusters()
.Append<DepositPriceAdjuster>();
}
}
````
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class MyAdjustment : PriceAdjustment<MyAdjustment>
{ }

// Additional helper constructors
public MyAdjustment (string name, string reference, Price adjustment)
public MyAdjustment(string name, string reference, Price adjustment)
: base(name, adjustment)
{
MyAdjustmentRef = reference;
Expand Down
2 changes: 2 additions & 0 deletions content/core/1.4.0/links.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
items:
- title: Overview
link: /how-to-guides/
- title: Add Custom Deposit Fee
link: /how-to-guides/add-custom-deposit-fee/
- title: Limit Order Line Quantity
link: /how-to-guides/limit-orderline-quantity/

Expand Down
77 changes: 77 additions & 0 deletions content/core/1.5.0/how-to-guides/add-custom-deposit-fee.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
title: Add Custom Deposit Fee
description: How-To Guide to add custom deposit fee in Vendr, the eCommerce solution for Umbraco v8+
---

In this guide we will be looking at price adjustments in Vendr to be able to add custom fee when to total price reach a specific price.

## DepositPriceAdjustment

We define a custom `PriceAdjustment` making it possible to enhance with custom properties we need and when accessing adjustments we can cast to this specific type.

````csharp
[Serializable]
public class DepositPriceAdjustment : PriceAdjustment<DepositPriceAdjustment>
{
public string DepositAdjustmentRef { get; set; }

// A parameterless constructor is required for cloning
public DepositPriceAdjustment()
: base()
{ }

// Additional helper constructors
public DepositPriceAdjustment(string name, string reference, Price adjustment)
: base(name, adjustment)
{
DepositAdjustmentRef = reference;
}
}

````

## DepositPriceAdjuster

Furthermore we need a `PriceAdjuster` to implement thie calculation of the price, which in this case will be adding a fee. In other cases you may want add a discount instead by adding a negative price value.

````csharp
public class DepositPriceAdjuster : PriceAdjusterBase
{
public override void ApplyPriceAdjustments(PriceAdjusterArgs args)
{
// Add fee if total price is more than £1000
if (args.Calculation.TotalPrice.Value.WithTax >= 1000)
{
var taxRate = args.Calculation.TaxRate.Value;

var fee = 100M;
var tax = fee * taxRate / (1 + taxRate);
var priceWithoutTax = fee / (1 + taxRate);

// Create a £100 fee
var price = new Price(priceWithoutTax, feeTax, args.Order.CurrencyId);
var adjustment = new DepositPriceAdjustment("Deposit", "deposit", price);

args.SubtotalPriceAdjustments.Add(adjustment);
}
}
}

````

## Register price adjuster

Finally we need to register the Vendr price adjuster in a `IComposer`.

````csharp
[ComposeAfter(typeof(VendrWebComposer))]
public class StoreComposer : IUserComposer
{
public void Compose(Composition composition)
{
// Register price adjuster
composition.WithPriceAdjusters()
.Append<DepositPriceAdjuster>();
}
}
````
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class MyAdjustment : PriceAdjustment<MyAdjustment>
{ }

// Additional helper constructors
public MyAdjustment (string name, string reference, Price adjustment)
public MyAdjustment(string name, string reference, Price adjustment)
: base(name, adjustment)
{
MyAdjustmentRef = reference;
Expand Down
2 changes: 2 additions & 0 deletions content/core/1.5.0/links.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
items:
- title: Overview
link: /how-to-guides/
- title: Add Custom Deposit Fee
link: /how-to-guides/add-custom-deposit-fee/
- title: Limit Order Line Quantity
link: /how-to-guides/limit-orderline-quantity/

Expand Down