PRO: Sync Woocommerce-Products and JSON
Your situation: You have JSON data with Products to be stored in the WordPress-Shopsystem Woocommerce.
Solution: With the JCI-Plugin and the Woocommerce-REST API, you can create, update and delete Woocommerce-Products.
The following example syncs JSON and Woocommerce-Products.
Other applications would update the prices of a Product out of JSON or the number of available items.
If your data is in XML or CSV: No problem, the PRO JCI-Plugin can read such data too.
JSON-Example: http://api.json-content-importer.com/extra/example-gen-cp-ts/example_events.json
Step 1: Create a JCI-Template needed for authorization
Name of template: dowoo
Curl options: param1
Twig-Template:
{{_context | json_encode }}
Step 2: Create a 2nd JCI-Template for communication with Woocommerce
Shortcode:
[jsoncontentimporterpro nameoftemplate="woocreateproduct" orderofshortcodeeval=10]
JCI-Template:
Name of Template: woocreateproduct
URL: http://api.json-content-importer.com/extra/example-gen-cp-ts/example_events.json
Twig-Template:
{% set starttime = ("now"|date("U")) %} {# set basic parameter #} {% set wurl = 'YOUR_DOMAIN/wp-json/wc/v3/products' %} {% set customer_key= 'YOUR_CUSTOMER_KEY' %} {% set customer_secret = 'YOUR_CUSTOMER_SECRET' %} {% set jcish_start = '[jsoncontentimporterpro orderofshortcodeeval=10 httpstatuscodemustbe200=no nameoftemplate=dowoo url="' %} {% set httpheader = 'CURLOPT_HTTPHEADER=Content-Type:application/json##' %} {#<hr>loop JSON and update or create Products:<hr>#} {% set listofproductids = [] %} {% for prodin in _context %} {% if prodin.name and prodin.id %} {#{{prodin.id}}: {{prodin.name}}<br>#} {% set listofproductids = listofproductids | merge([prodin.id]) %} {# insert or update product #} {% set prod = '{"name": "'~prodin.name~'", "type": "simple", "description": "'~prodin.description~'", "short_description": "'~prodin.description~'"}' %} {% set chhash = (prod|md5) %} {% set chfoundid = wp_get_cp_by_cpf_keyvalue("any", "jciwooid", prodin.id) %} {% if chfoundid %} {% set valhash = wp_get_custom_field_value(chfoundid.0, "jciwoohash") %} {% if valhash.0==chhash %} {#no update needed, no data change<hr>#} {% else %} {% set wurlupdate = wurl ~ "/" ~ chfoundid.0 %} {% set wooauthstr = httpheader ~ jci_woo_calc_auth(customer_key, customer_secret, wurlupdate, 'PUT') ~ ';CURLOPT_POSTFIELDS=' ~ prod %} {% set jc = jcish_start~wurlupdate~'" method="curlput" param1=\''~wooauthstr~'\']' %} {% set z1 = (jc |doshortcode) %} {% set z = (z1 | json_decode(TRUE) ) %} {% if z.id==chfoundid.0 %} {% set newcpf = wp_insert_custom_field_keyvalue(chfoundid.0, "jciwoohash", (chhash)) %} update ok: {{prodin.name}}<hr> {% else %} update FAILED:<br>{{z1}}<hr> {% endif %} {% endif %} {% else %} {% set wooauthstr = httpheader ~ jci_woo_calc_auth(customer_key, customer_secret, wurl , 'POST') ~ ';CURLOPT_POSTFIELDS=' ~ prod %} {% set jc = jcish_start~wurl~'" method="curlpost" param1=\''~wooauthstr~'\']' %} {% set z1 = (jc |doshortcode) %} {% set z = (z1 | json_decode(TRUE) ) %} {% set pageidofnewproduct = z.id %} new page created: {{pageidofnewproduct}}<hr> {% set newcpf1 = wp_insert_custom_field_keyvalue(pageidofnewproduct, "jciwooid", (prodin.id)) %} {% set newcpf2 = wp_insert_custom_field_keyvalue(pageidofnewproduct, "jciwoohash", (chhash)) %} {% endif %} {% endif %} {% endfor %} {% set timeafterupdate = ("now"|date("U")) %} time for update: {{timeafterupdate-starttime}}<br> {#listofproductids: {{listofproductids | dump}}<br>#} {#get all product #} {% set wooauthstrgetprod = httpheader ~ jci_woo_calc_auth(customer_key, customer_secret, wurl , "GET" ) %} {% set jcget = jcish_start ~ wurl ~ '" method="curlget" param1=\''~wooauthstrgetprod~'\']' %} {% set z1gt = (jcget | doshortcode | json_decode(TRUE)) %} existing products:<br> {% for p in z1gt %} {% if p.id %} {% set pgid = wp_get_custom_field_value(p.id, "jciwooid") %} {% if pgid.0 in listofproductids %} ok: {{p.name}} ({{p.id}})<br> {% else %} delete {{p.name}} ({{p.id}})<br> {# way 1: keys in url #} {# {% set wurldelete = wurl ~ "/"~p.id~"?consumer_key="~customer_key~"&consumer_secret="~customer_secret~"&force=true" %} wurldelete: {{wurldelete}}<br> {% set jc = jcish_start ~ wurldelete~'" method="curldelete"]' %} #} {# way 2: calc auth #} {% set wurldelete = wurl ~ "/"~p.id %} wurldelete: {{wurldelete}} {% set wooauthstrdelprod = httpheader ~ jci_woo_calc_auth(customer_key, customer_secret, wurldelete , "DELETE" ) %} {% set jc = jcish_start~wurldelete~'" method="curldelete" param1=\''~wooauthstrdelprod~'\']' %} {% set z1 = (jc |doshortcode) %} {#{% set z = (z1 | json_decode(TRUE) ) %} jc: {{jc}}<hr> del: {{z1| dump}}<hr>#} <hr> {% endif %} {% endif %} {% endfor %} {% set timetotal = ("now"|date("U")) %} totaltime: {{timetotal-starttime}}
Code-Explanation:
- Set essential parameter: Replace YOUR_DOMAIN, YOUR_CUSTOMER_KEY, and YOUR_CUSTOMER_SECRET with your actual values. You must use an https-URL!
- Then we loop through all JSON elements: In our example, the “id” is the unique key to each data/product element. If there is a CPT with CPF “jciwooid” and the “id” then there is already a Woocommerce-Product for this “id”. Then we check if an update is needed. For that, the hash (“chhash”) with the new JSON data is compared to the hash stored for the present page (CPF “jciwoohash”). If these are not equal, an update is done.
- If there is no Product for the “id” we create one and store the CPFs “jciwooid” and “jciwoohash”.
- We also create a list of “id” in the JSON: “listofproductids”.
- Then we have to check if there are Woocommerce-Products, which are not in the JSON anymore. For that, we ask Woocommerce for a list of all Products (“z1gt”) and loop through it: If there is a Product with an “id” not in “listofproductids,” this Product is not in the JSON. Those products we delete from woocommerce.
- In the end, we synced the JSON with Woocommerce.