Advanced Dynamic Pricing For WooCommerce

Helpers

Here’s the fully rewritten article:


PHP Helper Functions — Advanced Dynamic Pricing for WooCommerce

Overview

Advanced Dynamic Pricing for WooCommerce exposes a set of public PHP helper functions through the adp_functions() global accessor. These functions give developers programmatic access to the plugin’s internal pricing engine — letting you query active rules, calculate discounted prices, retrieve free gift products, and build pre-discounted product lists from your own code, without hooking deep into the plugin’s internals.

All helpers live in the static class WDP_Functions. The legacy class WDP_Frontend still works as an alias if you encounter it in older code, but adp_functions() is the recommended way to access them going forward.


adp_functions()->getGiftedCartProducts()

This function returns an array of all free products the plugin has added to the current cart through active gift/free product rules. The array format is [ product_id => quantity ] — for example [ 42 => 1, 75 => 2 ]. Use it when you need to identify which cart items came from a free gift rule rather than a customer-initiated add-to-cart action, for example to exclude them from loyalty point calculations or custom cart displays.


adp_functions()->getOnSaleProducts( $include_wc_onsale = false )

This function returns an array of product IDs that currently have at least one active Advanced Dynamic Pricing rule applying a discount to them. Pass true as the argument to also include products that carry a native WooCommerce sale price alongside the plugin’s dynamically discounted products. This is useful for building custom sale product grids, feeds, or widgets that need to reflect the plugin’s active discounts rather than only WooCommerce’s stored sale prices.


adp_functions()->getActiveRulesForProduct( $productId, $qty = 1, $useEmptyCart = false )

This function adds the specified product to the current cart (or an empty cart if $useEmptyCart is true) at the given quantity and returns an array of all pricing rules that apply to it under those conditions. Each entry in the returned array is a rule object you can inspect to determine the rule type, discount amount, adjustment handler, and other properties.

The $useEmptyCart parameter is important — passing true evaluates rules against a clean empty cart, which removes any cart-condition effects from other items already in the session. Pass false to evaluate rules in the context of the customer’s real current cart.

Example usage — display “From” price using the highest bulk discount tier:

Code Explained (for Developers)

ElementDescription
woocommerce_get_price_html at PHP_INT_MAXHooks into WooCommerce’s price HTML filter at the highest possible priority, ensuring this code runs after all other price modifiers — including Advanced Dynamic Pricing’s own filters.
getActiveRulesForProduct( $id, 50, true )Retrieves active rules for the product at quantity 50 using an empty cart, which simulates a high-quantity purchase and surfaces bulk discount tiers that only apply above a threshold.
SingleItemRule instanceof checkFilters out cart-level or other rule types — this example targets product-level rules with a range adjustment (bulk pricing) only.
getProductRangeAdjustmentHandler()->getRanges()Retrieves the bulk price tiers defined in the rule. array_pop() gets the last (highest quantity) range, which carries the deepest discount.
$range->getFrom()The minimum quantity threshold for the selected range tier. Passing it to calculateProduct() simulates the price at that quantity.
CartBuilder->create()Builds a cart context object from the current customer and session, which the price processor needs to evaluate conditions correctly.
getProcProductPriceToDisplay() + format()Retrieves the display-ready price from the processed product and formats it using WooCommerce’s currency settings.
return 'From ' . ...Outputs the lowest available bulk price as a “From £X.XX” string, replacing the default price HTML.

adp_functions()->calculateProduct( $product, $qty, $useEmptyCart = true )

This function runs the plugin’s full pricing calculation for a single product at a given quantity and returns a processed product object. The returned object exposes methods like getCalculatedPrice(), getOriginalPrice(), and areRulesApplied() that let you read the post-discount price and inspect whether any rules fired.

The return type depends on the product type: simple and external products return an ADPBaseVersionIncludesProductProcessedProductSimple object, variable products return ADPBaseVersionIncludesProductProcessedVariableProduct (or the PRO version’s equivalent). You’ll see this function used throughout the compatibility code samples in this documentation — for example in the sale badge percentage calculation and the original price display fix.


adp_functions()->getDiscountedProductsForCart( $listOfProducts, $plain = false )

This function calculates the discounted prices for a list of products by temporarily building a virtual cart from your input array and running the full rule engine against it. It suits pre-checkout price previews, custom order forms, and external integrations where you need to know what a set of products would cost with all active rules applied before the customer actually adds anything to their cart.

The $listOfProducts parameter accepts an array of product arrays, each with product_id, qty, and an optional cart_item_data key. The $plain parameter controls the return format — true returns a flat array of discounted product data, while false splits the results by the rules that applied to each product.

Example:

Use true when you just need a flat price list. Use false when you need to know which rule drove each discount — useful for displaying rule-level discount breakdowns in a custom quote or order preview UI.


adp_functions()->getDiscountedProductPrice( $product, $qty, $useEmptyCart = true )

This is the simplest helper for getting a single discounted price. Pass a product ID or WC_Product object, a quantity, and whether to use an empty cart for the calculation. The function returns the discounted price in one of three formats depending on the product type: a float for simple products, a [min, max] array for variable products (representing the lowest and highest discounted variation prices), or null if the product argument is invalid.

This function suits straightforward use cases where you need the discounted price as a number rather than a formatted string — for example feeding it into a custom price display, an API response, or a calculation that combines multiple product prices.


When Should You Use These Helpers?

The helper functions suit any situation where you need to interact with Advanced Dynamic Pricing’s pricing engine from outside the standard WooCommerce loop. Common use cases include building custom product price displays that reflect bulk discount tiers, powering a custom quote request form that pre-calculates discounted totals, identifying which cart items arrived via free gift rules, creating custom sale grids based on the plugin’s active rules rather than WooCommerce’s stored sale flags, and building integrations with third-party systems that need to know the correct discounted price before the customer reaches the cart.

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