Public Ticket #3663998
Creating a rule with REST API


  •  1
    Lasse started the conversation

    Im trying to get rest api to write some dynamic rules - but i get the following message: { "code": "rest_cannot_create", "message": "Du har desværre ikke tilladelse til at oprette indlæg som denne bruger.", "data": { "status": 401 }

    do you have any idea?

    im following this guide: https://woocommerce-b2b-plugin.com/docs/import-or-setup-dynamic-rules-programatically/

  •  1
    Lasse replied

    example on the code we are trying to push:




    "meta": {

    "b2bking_rule_what": [



    "b2bking_rule_howmuch": [



    "b2bking_rule_applies": [



    "b2bking_rule_who": [





    to this url:: 


  •  1,970
    WebWizards replied

    Hello Lasse,

    The issue seems to be the REST API authentication, as you get a 401 - unauthorized error. Therefore I think the issue is not the code, but the general authentication (getting the site to understand you have the right to create rules).

    Previously in our tests, it helped to install this REST API authentication plugin: https://wordpress.org/plugins/wp-rest-api-authentication/ - it may be needed to try a few methods in order to find one that works for your setup.

    Kind regards,


  •  1
    Lasse replied

    What worked. Now for a different problem. When exporting product and user data, we have to use, the predefined parameters in wordpress fx the first user is user_1 the second is user_2. This makes it incredible hard to target, as we have a unik parameter (Debitor number) and to that, we want to target the product by sku number, but it's the same story here. 

    Is there some way, to define that the product is selected based on sku number?

  •  1
  •  1,970
    WebWizards replied

    Regarding that, I don't see a simple way of making rules work directly with SKU or other user data I'm afraid. I looked at whether we can add some kind of snippet or code, but it seems code changes would be needed in dozens of places including internal caches for which rules apply to which users.

    It may be easier to run calls to get the product ID from the SKU first.

    Another possibility I'd like to mention:

    I noticed that in your rule example below you are setting a specific price for a specific product and user. We are currently working on a new feature for user-based price lists, and as part of that it is now already possible to set this kind of pricing directly by modifying post / product meta data.

    Specifically, for each product you can set:

    - 'b2bking_regular_price_user_'.$user_id

    - 'b2bking_sale_price_user_'.$user_id


    - 'b2bking_regular_price_user_'.$username

    - 'b2bking_sale_price_user_'.$username

    So for example to set the price of a product for the user admin as 50, you can create a product meta entry for that product, setting the meta key 'b2bking_regular_price_user_admin' to the value 50.