How to Program Custom Fees
The Advanced Dynamic Pricing for WooCommerce plugin provides a powerful rule builder for discounts, but some stores need to add conditional fees—surcharges that depend on the cart total, product options, quantities, or other custom logic. For example, you might want to add a small‑order fee when the cart total falls below a threshold, charge a service fee for items with a specific add‑on, or apply a special surcharge for orders that contain products from a particular category. The plugin’s user interface does not offer a “fee” rule type, but you can easily program these fees using a dedicated PHP hook.
This guide explains the exact mechanism for adding custom fees, provides a ready‑to‑use code example, and shows you how to adapt the logic to your own store.
Important: This guide is intended for developers who are comfortable editing PHP files and working with WordPress hooks. Always test custom code on a staging site before deploying to production.
Prerequisites
Before you start programming custom fees, ensure that:
- The Advanced Dynamic Pricing for WooCommerce plugin is installed and activated (the
WDP_Functions::add_feemethod works in both the free and Pro versions). - You have administrator access to your WordPress installation.
- You are comfortable editing your child theme’s
functions.phpfile or creating a custom plugin. - You have a clear understanding of the conditions under which the fee should be applied.
Complete Code Example
The following example demonstrates two different conditional fees:
- A small‑order fee of 15.00whenthecarttotal(afterdiscounts)islessthan150.00.
- A small embroidery run fee of $25.00 applied to each product from a specific category (category ID 255) when the product quantity is less than 8.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
//This code runs after ADP processed WooCommerce cart add_action( 'wdp_after_apply_to_wc_cart', function ( $wdp_cart ) { $wc_cart = WC()->cart; $totals = $wc_cart->get_totals(); //add fees for each matched item if order total <150 if ( isset( $totals['total'] ) && $totals['total'] < 150.00 ) { foreach ( $wc_cart->get_cart_contents() as $item_key => $cart_content ) { // check item meta keys added by external plugin YITH Product Addons if ( isset( $cart_content['yith_wapo_options'] ) ) { foreach ( $cart_content['yith_wapo_options'] as $option ) { //fee for some values only if ( "Upload Logo" === $option['name'] && ( "Upload file now" === $option['value'] || "Add logo later" === $option['value'] ) ) { WDP_Functions::add_fee( __( 'Total fee' ), 15 ); } } } } } //add fees for each item if qty <8 and item belongs to category, category ID - hardcoded! foreach ( $wc_cart->get_cart_contents() as $item_key => $cart_content ) { $product = $cart_content['data']; $parent = wc_get_product($cart_content['product_id']); /** * @var $product WC_Product * @var $parent WC_Product */ if ( isset( $cart_content['quantity'] ) && $cart_content['quantity'] < 8 && in_array( 255, $parent->get_category_ids() ) ) // Clothing { WDP_Functions::add_fee( __( 'Small embroidery run fee' ), 25 ); } } }); |
What the code does
| Element | Explanation |
|---|---|
add_action( 'wdp_after_apply_to_wc_cart', ... ) | Runs the function after Advanced Dynamic Pricing has finished processing the cart. |
$wc_cart = WC()->cart; | Retrieves the WooCommerce cart object. |
$totals = $wc_cart->get_totals(); | Gets the cart totals, including the final total after discounts. |
WDP_Functions::add_fee( __( 'Total fee' ), 15 ); | Adds a $15.00 fee with the label “Total fee”. |
$parent->get_category_ids() | Retrieves the category IDs for the product’s parent (important for variations). |
in_array( 255, $parent->get_category_ids() ) | Checks whether the product belongs to category ID 255. |
WDP_Functions::add_fee( __( 'Small embroidery run fee' ), 25 ); | Adds a $25.00 fee for each qualifying product. |
$cart_content['yith_wapo_options'] | Accesses the YITH Product Add‑Ons data stored in the cart item. |
Note about category ID: The example uses a hardcoded category ID (
255). To find a category ID, go to Products → Categories in your admin area. Hover over a category name, and the URL will display thetag_IDparameter (e.g.,tag_ID=255).
Step‑by‑Step Implementation
Follow these steps to add the custom fee code to your WooCommerce store.
Step 1: Choose where to add the code
You have two safe options for adding custom PHP code:
- Option A (Recommended): Use the free Code Snippets plugin.
Install the plugin from Plugins → Add New by searching for “Code Snippets”. This method allows you to add, activate, and deactivate snippets without ever editing your theme files. - Option B: Add to your child theme’s
functions.phpfile.
If you are comfortable editing theme files, add the code to your active child theme’sfunctions.phpfile. Never add custom code directly to a parent theme, because updates will erase your changes.
Step 2: Insert the snippet
- If using Code Snippets:
- If editing
functions.php:
Step 3: Test the functionality
- Add qualifying products to the cart (e.g., products from category ID 255 with quantity less than 8).
- Ensure the cart total after discounts is below the threshold (e.g., $150.00) if you want to trigger the small‑order fee.
- Go to the cart page or checkout page.
- Verify that the custom fees appear as separate line items in the cart totals.
If the fees appear correctly, the implementation is successful.