Import or Set Up Dynamic Rules Programmatically

This article aims to help you set up dynamic rules using code, WP All Import, REST API, or any other import tool.

1. Dynamic rules are posts of the post type b2bking_rule

The first step in creating a rule is to create a new post of the type b2bking_rule (this is a custom post type that our plugin creates).

2. Dynamic rules are configured through their metadata

The plugin uses metadata to configure rule type, amounts, conditions, etc.

List of Meta Keys and Values #

Below you will find the main keys and possible values for configuring a rule:

1)

b2bking_rule_what — this is the most important key, which controls the rule type. Possible values:

  • discount_percentage
  • discount_amount
  • fixed_price
  • hidden_price
  • free_shipping
  • minimum_order
  • maximum_order
  • required_multiple
  • tax_exemption_user — this is a regular tax exemption
  • tax_exemption — this is zero-tax products
  • add_tax_percentage
  • add_tax_amount

The above correspond to Discount Percentage rules, Discount Amount rules, etc.

2)

b2bking_rule_who — this controls the category of users or the specific user that this rule applies to. Possible values:

  • everyone_registered — All registered users
  • everyone_registered_b2b — All registered B2B users
  • everyone_registered_b2c — All registered B2C users
  • user_0 — All guest users (logged out)
  • group_x — Here x is the B2B group ID such as 25, 55, e.g. group_25
  • user_x — Here x is the user ID (to apply rules to a specific user)
  • multiple_options — Multiple options; a separate meta key will be configured

To set multiple options, you must set the additional meta key b2bking_rule_who_multiple_options. This key should contain the same values as above, comma-separated, without spaces:

Example: user_0,user_5 — this configuration would apply to all guest users and the logged-in user with ID 5.

3)

b2bking_rule_applies — this controls the products or categories the rule applies to. Possible values:

  • cart_total — All products / Cart total
  • category_x — Here x is the ID of the category
  • product_x — Here x is the ID of the product or variation
  • b2bking_one_time — One-time tax or fee
  • multiple_options — Multiple options; a separate meta key will be configured

To set multiple options, you must set the additional meta key b2bking_rule_applies_multiple_options. This key should contain the same values as above, comma-separated, without spaces:

Example: category_5,product_2

4)

b2bking_how_much — this is the actual value of the price, discount, or minimum order, depending on the rule type.

Values can be numbers between 1 and 999999999.

Other meta keys:

  • b2bking_rule_currency
  • b2bking_rule_quantity_value
  • b2bking_rule_taxname
  • b2bking_rule_discountname
  • b2bking_rule_conditions
  • b2bking_rule_discount_show_everywhere
  • b2bking_rule_requires
  • b2bking_rule_showtax

How to Get Full Configuration Details for a Rule #

There are a large number of possible keys and combination/configuration options. The best way to get the precise configuration for a specific rule that you are trying to create programmatically is to first create it manually in the backend. Then you can check the meta keys and configuration in the wp_postmeta table.

Steps:

1) Create the desired rule manually

Example: We configure the following rule:

Then click "Publish" and check the URL. On our test site it is: http://b2bking.local/wp-admin/post.php?post=118&action=edit

This URL contains the number 118, which is the rule ID.

2) Go to the wp_postmeta table using a tool such as ARI Adminer or phpMyAdmin and search for the rule ID.

Example:

We go there with ARI Adminer and search for 118 to get the full configuration:

Not all keys in the configuration are necessary for the rule. If a key is empty or comes from another rule (e.g., b2bking_rule_currency: AED in the example), then you can ignore it.

Creating a Rule with REST API — Example #

Let's look at how we can use Postman to create a rule via the REST API.

It may also be necessary to install and activate a REST authentication plugin such as https://wordpress.org/plugins/wp-rest-api-authentication/

In the REQUEST URL field, the address we use is site.com/wp-json/wp/v2/b2bking_rule (this is the REST API endpoint).

In the Headers section, we disable the default content type (text/plain) and add a new header with Content-Type = application/json.

Headers in API request

We can use the following request body, under Body → raw:

The request code is:

{
    "status": "publish",
    "type": "b2bking_rule",
    "title": "My New Rule",
     "meta": {
            "b2bking_rule_what": [
                "fixed_price"
            ],"b2bking_rule_howmuch": [
                "20"
            ]
     }
}

You can then find the rule in the backend under B2BKing → Dynamic Rules:

Clearing the Rules Cache #

Dynamic rules use an internal cache to improve performance. If you make changes to rules via the REST API, this cache must be cleared for your changes to take effect.

It can be cleared manually by going to B2BKing → Tools → Cache → Clear all plugin caches, or by updating any random rule.

If you want to also clear the cache programmatically, you can do so by running this code:

b2bking()->clear_caches_transients();
b2bking()->b2bking_clear_rules_caches();

Powered by BetterDocs