This article aims to help you set up dynamic rules with Code/Wp All Import/REST API/Any import tool.
1. Dynamic rules are posts of the post type b2bking_rule
The first step to create 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.
Below you will find the main keys and possible values to configure a rule:
b2bking_rule_what - this is the most important key, which controls rule type. Possible values:
The above correspond to Discount Percentage rules, Discount Amount rules, etc.
b2bking_rule_who - this controls the category of users / user that this rule applies to. Possible values:
To set multiple options, you must set the additional meta key b2bking_rule_who_multiple_options. This key would contain the same values as above, comma separated, without spaces:
Example: user_0,user_5 - this configuration would be visible to all guest users and the logged in user with id 5
b2bking_rule_applies - this controls the products / categories the rule applies to. Possible values:
To set multiple options, you must set the additional meta key b2bking_rule_applies_multiple_options. This key would contain the same values as above, comma separated, without spaces:
b2bking_how_much - this is the actual value of the price/discount/minimum order depending on the rule type.
Values can be numbers between 1-999999999
Other meta keys:
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 programatically, is to first create it manually in the backend. Then you can check the meta keys and configuration in the wp_postmeta table.
1) Create desired rule manually
Example: I configure the following rule:
I click "Publish" and then check the URL/Link. I get: http://b2bking.local/wp-admin/post.php?post=118&action=edit
This url has the number 118, which is the rule id
2) Go to the wp_postmeta table with a tool such as ARI Adminer or PHPMyAdmin and search for the rule id
I go there with ARI Adminer and search for 118 and 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.
Let's look at how we can use POSTMAN ( https://www.postman.com/ ) to create a rule with rest api.
It may be necessary to also 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 we add a new header with Content-Type = application/json.
We can use the following request body, under Body->raw:
The request code is:
"title": "My New Rule",
You can then find the rule in the backend under B2BKing -> Dynamic Rules:
Dynamic rules use an internal cache to improve performance. If you make changes to rules via the REST API, for your changes to take effect this cache must also be cleared.
It can be cleared manually by opening the website backend and updating any random rule.
If you want to also clear the cache programmatically, you can do so by running this code:
require_once B2BKING_DIR . '/admin/class-b2bking-admin.php';
Powered by BetterDocs