# EDD\Fees\Handler Class

The `EDD\Fees\Handler` class can be used to add arbitrary fees to the shopping cart, both **positive** and **negative** (discount) fees. For example, you could add a special 10% off fee automatically for all registered users that have a certain user role, or add a &quot;handling&quot; fee for all products inside of a certain category. The options are limitless.

Fees are stored as session variables, which means once they&#039;re set for a user they&#039;ll persist until either the user checks out or they&#039;re removed by some code based on a decision or setting.

### Types of fees

Fees can have two types:

- `fee`: This is just what it sounds like--an additional cost or discount added to the entire purchase.
- `item`: This is a fee that is not associated with anything else. Think of it like a temporary product made up on the spot. Wallet deposits are an example of this kind of fee.

**Note:** negative fees *always* set no\_tax to true.

### add\_fee()

Adding an expense or giving a discount are both considered adding fees, despite the fact that one of them is removing money.

Adding a fee is done with code similar to this:

```
function eddwp_set_admin_discount() {&lt;br&gt;&lt;/br&gt;	// Check to see the customer is a site admin.&lt;br&gt;&lt;/br&gt;	if ( ! current_user_can( &#039;manage_options&#039; ) ) {&lt;br&gt;&lt;/br&gt;		return;&lt;br&gt;&lt;/br&gt;	}&lt;br&gt;&lt;/br&gt;&lt;br&gt;&lt;/br&gt;	// find 20% of the total&lt;br&gt;&lt;/br&gt;	$amount = edd_get_cart_subtotal() * 0.20;&lt;br&gt;&lt;/br&gt;&lt;br&gt;&lt;/br&gt;	// Flip the 20% from a positive number to a negative number&lt;br&gt;&lt;/br&gt;	$amount = $amount * -1;&lt;br&gt;&lt;/br&gt;&lt;br&gt;&lt;/br&gt;	// Add the fee, sending the amount, the reason printed to the front end, and the handle&lt;br&gt;&lt;/br&gt;	EDD()-&gt;fees-&gt;add_fee(&lt;br&gt;&lt;/br&gt;		array(&lt;br&gt;&lt;/br&gt;			&#039;price_id&#039;    =&gt; null, // set this to link the fee to a specific price ID.&lt;br&gt;&lt;/br&gt;			&#039;amount&#039;      =&gt; $amount,&lt;br&gt;&lt;/br&gt;			&#039;label&#039;       =&gt; &#039;You are an admin special!&#039;,&lt;br&gt;&lt;/br&gt;			&#039;id&#039;          =&gt; &#039;admin_special&#039;,&lt;br&gt;&lt;/br&gt;			&#039;type&#039;        =&gt; &#039;fee&#039;,&lt;br&gt;&lt;/br&gt;			&#039;no_tax&#039;      =&gt; false,&lt;br&gt;&lt;/br&gt;			&#039;download_id&#039; =&gt; 0, // set this to link the fee to a specific product.&lt;br&gt;&lt;/br&gt;		)&lt;br&gt;&lt;/br&gt;	);&lt;br&gt;&lt;/br&gt;}&lt;br&gt;&lt;/br&gt;add_action( &#039;template_redirect&#039;, &#039;eddwp_set_admin_discount&#039; );&lt;br&gt;&lt;/br&gt;
```

This example automatically adds a 20% discount if the current user is an admin.

Here&#039;s another example that adds a handling fee:

```
function eddwp_add_handling_fee() {&lt;br&gt;&lt;/br&gt;	// Add a $10 handling fee no matter what&lt;br&gt;&lt;/br&gt;	EDD()-&gt;fees-&gt;add_fee(&lt;br&gt;&lt;/br&gt;		array(&lt;br&gt;&lt;/br&gt;			&#039;amount&#039;      =&gt; 10,&lt;br&gt;&lt;/br&gt;			&#039;label&#039;       =&gt; &#039;Handling fee&#039;,&lt;br&gt;&lt;/br&gt;			&#039;id&#039;          =&gt; &#039;handling_fee&#039;,&lt;br&gt;&lt;/br&gt;			&#039;type&#039;        =&gt; &#039;fee&#039;,&lt;br&gt;&lt;/br&gt;			&#039;no_tax&#039;      =&gt; false,&lt;br&gt;&lt;/br&gt;			&#039;download_id&#039; =&gt; 0,&lt;br&gt;&lt;/br&gt;		)&lt;br&gt;&lt;/br&gt;	);&lt;br&gt;&lt;/br&gt;}&lt;br&gt;&lt;/br&gt;add_action( &#039;template_redirect&#039;, &#039;eddwp_add_handling_fee&#039; );&lt;br&gt;&lt;/br&gt;
```

Here&#039;s a screenshot of both of the above examples being applied at the same time: ![](https://easydigitaldownloads.com/wp-content/uploads/2022/07/6184cc919c3ad.png)

### remove\_fee()

To remove a fee, simply pass the fee ID (third parameter above) to the remove\_fee() method:

```
function pw_remove_handling_fee() {&lt;br&gt;&lt;/br&gt;	EDD()-&gt;fees-&gt;remove_fee( &#039;handling_fee&#039; );&lt;br&gt;&lt;/br&gt;}&lt;br&gt;&lt;/br&gt;add_action( &#039;template_redirect&#039;, &#039;pw_remove_handling_fee&#039;, 9999 );&lt;br&gt;&lt;/br&gt;
```

### Other Methods

In addition to simply adding and removing fees there are a number of methods for working with fees on the back end.

##### has\_fees( string $type = &#039;fee&#039; )

This method simply checks to see if any fees exist. Defaults to the fee type, but can accept item as well.

Example:

```
if ( EDD()-&gt;fees-&gt;has_fees() ) {
	echo &#039;yep, we can haz fees!&#039;;
}
```

Returns: true or false.

##### get\_fees( string $type = &#039;fee&#039;, integer $download\_id = 0 )

This method will get an array of all existing fees. It can be limited by type and/or download id.

Example

The [Simple Shipping extension](https://easydigitaldownloads.com/extension/simple-shipping/) is a perfect example of how the EDD\_Fees class can be used.

```
$fees = EDD()-&gt;fees-&gt;get_fees();
```

Returns something like this:

```
Array
(
    [handling_fee] =&gt; Array
        (
            [amount]      =&gt; 11.00
            [label]       =&gt; Handling Fee
            [type]        =&gt; fee
            [no_tax]      =&gt; 
            [download_id] =&gt; 0
            [price_id]    =&gt;
        )

    [admin_special] =&gt; Array
        (
            [amount]      =&gt; -2
            [label]       =&gt; You are an admin special!
            [type]        =&gt; fee
            [no_tax]      =&gt; 
            [download_id] =&gt; 0
            [price_id]    =&gt;
        )

    [tophers_fee] =&gt; Array
        (
            [amount]      =&gt; 42.00
            [label]       =&gt; Topher&#039;s fee
            [no_tax]      =&gt; 
            [type]        =&gt; fee
            [download_id] =&gt; 114
            [price_id]    =&gt;
        )

)
```

##### get\_fee( string $id = &#039;&#039; )

This method will get an array of a specific fee, identified by the name it was given when it was created.

Example:

```
$my_custom_fee = EDD()-&gt;fees-&gt;get_fee( &#039;my_custom_fee&#039; );
```

Returns something like this:

```
 Array
(
    [amount] =&gt; 42.00
    [label] =&gt; The Final Fee
    [no_tax] =&gt; 
    [type] =&gt; fee
    [download_id] =&gt; 114
)
```

##### type\_total( string $type = &#039;fee&#039; )

This method calculate the total for a specific type of fee. The types supported are &#039;fee&#039; and &#039;item&#039;.

Example:

```
$item_total = EDD()-&gt;fees-&gt;type_total( &#039;item&#039; );
```

Returns an number like 51.00

##### total( integer $download\_id = 0 )

This method calculate the total for all fees OR all fees specifically attached to a given download.

**Note:** If a product has fees and variable prices, The fee will only be applied once per transaction, NOT once per item.

Example:

```
$total = EDD()-&gt;fees-&gt;total();

$total = EDD()-&gt;fees-&gt;total( &#039;42&#039; );
```

In the example above, the first would return a number like 51.00 that is the sum of all fees.

The second example would return a number like 51.00 that is the sum of all fees tied specifically to the Download with the ID of 42.

##### record\_fees( array $payment\_meta, array $payment\_data )

This method records the fee information about a specific transaction. It should never be called directly.