Advanced Dynamic Pricing For WooCommerce

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_fee method 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.php file 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:

  1. small‑order fee of 15.00whenthecarttotal(afterdiscounts)islessthan15.00whenthecarttotal(afterdiscounts)islessthan150.00.
  2. 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.

What the code does

ElementExplanation
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 the tag_ID parameter (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.php file.
    If you are comfortable editing theme files, add the code to your active child theme’s functions.php file. 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

  1. Add qualifying products to the cart (e.g., products from category ID 255 with quantity less than 8).
  2. Ensure the cart total after discounts is below the threshold (e.g., $150.00) if you want to trigger the small‑order fee.
  3. Go to the cart page or checkout page.
  4. Verify that the custom fees appear as separate line items in the cart totals.

If the fees appear correctly, the implementation is successful.

Have questions? Please submit a support request. We're always happy to help!

Advanced Dynamic Pricing
for WooCommerce
PRO

From $60
Buy
✓ 30-day money-back guarantee