Advanced Dynamic Pricing For WooCommerce

How to add more rules

The Advanced Dynamic Pricing for WooCommerce plugin provides a user-friendly interface for creating bulk discounts, role‑based pricing, BOGO offers, and many other rule types. However, there are scenarios where you need to add or update a large number of rules programmatically. For example:

  • You want to integrate the plugin with an external product information system (PIM).
  • You need to bulk‑upload pricing rules from a CSV or JSON feed on a regular schedule.
  • You are building a custom WordPress solution that automatically creates rules based on real‑time data.

By default, the plugin does not expose a “one‑click” API for rule creation. Nevertheless, it offers a powerful import/export function that you can call from your own PHP code. This function accepts a JSON representation of your pricing rules and can either replace the existing rule set or append new rules to it.

This guide explains the exact mechanism for programmatically importing rules, shows you how to obtain the JSON structure from the plugin’s Tools tab, provides a ready‑to‑use PHP code example, and points you to the built‑in Backup Tool as a safer alternative for manual migrations.

Important: This guide is intended for developers who are comfortable editing PHP files and working with WordPress hooks. If you only need to migrate rules from one site to another, the Backup Tool is the recommended approach (see section 5).


Prerequisites

Before you can programmatically import rules, ensure that:

  • The Advanced Dynamic Pricing for WooCommerce plugin is installed and activated.
  • You have administrator access to your WordPress installation.
  • You are comfortable editing functions.php files or creating a custom plugin.
  • You have a valid JSON representation of the rules you want to import. You can obtain this from the Tools tab (see section 2).

Note: Both the free and Pro versions of the plugin support the import function described below. Pro‑only features (such as Product Collections) are included in the JSON only if you export them from a Pro installation.


Obtaining the Rule JSON Structure from the “Tools” Tab

Every pricing rule you create in the plugin is stored internally as a structured JSON object. You can view the JSON representation of your current rules directly from the plugin’s Tools tab.

Step‑by‑step instructions

  1. In your WordPress admin, go to Pricing Rules → Tools.
  2. Locate the Export Rules section.
  3. You will see a text area containing the JSON representation of all your existing rules.
  4. Copy this JSON string to a safe location. You can use it as a template for creating new rules programmatically.

Tip: The JSON format follows the same structure that the plugin uses internally. To understand it better, export a simple rule (e.g., a quantity‑based bulk discount), examine the JSON, and then gradually add more complex rule definitions.


The Import Function: WDP_Importer::import_rules()

The plugin provides a dedicated function for importing rules: WDP_Importer::import_rules( $data, $reset ). This function is part of the plugin’s internal API and is available in both the free and Pro versions.

Parameters

ParameterTypeDescription
$dataarrayA decoded JSON array of the rules you want to import. This must be the result of json_decode( $json, true ).
$resetboolDetermines whether to delete existing rules before importing the new ones. Set to true to replace all current rules, or false to add the new rules alongside existing ones (be careful – this may create duplicate rules).

Return value

The function does not return a value. If the import succeeds, the rules are saved to the database. If the operation fails, the plugin may throw an exception or log an error.

Note: In some versions of the plugin, the fully qualified class name is ADP\BaseVersion\Includes\ImportExport\Importer. The WDP_Importer alias is provided for backward compatibility. It is safe to use either.


Complete Code Example: Importing Rules from JSON

The following example demonstrates how to use the import function within a WordPress custom plugin or a functions.php snippet. It assumes you have already obtained a JSON string from the Tools tab.

What the code does

ElementExplanation
add_action( 'wp_loaded', ... )Runs the code after WordPress finishes loading all core files and plugins. You can use any other hook as well.
$json = '...';Contains the raw JSON string copied from the Tools tab. Replace the placeholder with your actual rule data.
$data = json_decode( $json, true );Converts the JSON string into a PHP associative array, which is the format the importer expects.
WDP_Importer::import_rules( $data, $reset );Executes the import. The rules are saved to the database immediately.
error_log( ... )Logs any JSON decoding errors to the PHP error log for debugging.

Important: This example runs the import on every page load (because it is attached to wp_loaded). Use this only as a starting point. In a production environment, you should place the import code inside a custom admin page, a settings page, or a one‑time script that runs only when explicitly triggered.


Alternative: Using the Backup Tool for Manual Import

If you do not need a fully automated, programmatic solution, the plugin’s built‑in Backup Toolprovides a safe and user‑friendly way to import rules manually.

Step‑by‑step instructions

  1. Navigate to Pricing Rules → Tools → Backup Tool.
  2. In the Import Settings section, paste your exported JSON data into the text box.
  3. Choose what you want to import: RulesOptions, or Product Collections (Pro version only).
  4. If you want to delete all existing rules before importing, turn on the “Clear all rules before import” checkbox.
  5. Click the Import button.

The backup tool also allows you to export your current rules, options, and product collections for migration to another WordPress site. This method is recommended for one‑time rule migrations because it does not require any coding.


Advanced: Using the Raw Importer Class

In the WordPress support forum, a plugin team member provided an alternative way to call the importer using the full namespace. This can be useful if the WDP_Importer alias is not available on your system.

This code achieves the same result as WDP_Importer::import_rules(). Use whichever form is more convenient for you.


Troubleshooting Common Import Issues

SymptomLikely CauseSolution
The import fails with a “critical error”.The JSON data is malformed, or the $data parameter is not a valid array.Validate your JSON using a tool like JSONLint. Ensure you decode the JSON with the true flag to get an associative array.
The function WDP_Importer::import_rulesdoes not exist.The class may have been renamed in a newer version of the plugin.Use the fully qualified class name: ADP\BaseVersion\Includes\ImportExport\Importer::importRules().
Rules are imported, but they do not appear in the admin UI.The rule cache may need to be refreshed.The plugin automatically refreshes its internal cache after an import. If you still do not see the rules, try clearing your WordPress caching plugins.
Duplicate rules are created on each page load.You are calling the import function on every WordPress request.Move the import code to a one‑time admin screen or use a transient flag to ensure it runs only once.
The imported JSON works in the Tools tab, but not when called programmatically.The JSON may contain escaped characters or line breaks that break json_decode().Use wp_unslash( $json )before decoding, and ensure the JSON string is a plain string without HTML entities.

At the tab “Tools” you can see a rule`s JSON structure.

For importing them, call WDP_Importer::import_rules($data, $reset).

$data is the decoded JSON.

$reset is the flag which determines whether to delete the rules before import or not.

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