7. Items real-time sync from Zoho to Woo

7. Items real-time sync from Zoho to Woo

Idea

Enable real-time sync from Zoho to WooCommerce using Zoho Webhooks!

Alert*This feature requires the Premium plan!

Setup:

  1. Setup a webhook called “Webhook Items”. See this link if you are new to webhooks.

  2. Create a new Webhook for the Module "Item". The webhook should be POST method with your domain as base and ‘wp-json/v2/zoho-product’ as the endpoint slug: https://mydomain.com/wp-json/v2/zoho-product/
    webhook zoho item
  3. Then go to Workflows and Create new Workflow called "Product to Woo" (or other name) and use the following trigger:
 

Select below options for the workflow


Then simply link the webhook the above workflow: 

Repeat all the above steps now for "Inventory Adjustments" webhook type! 

End result:

What data will get synced:

  • Product Name
  • Price
  • SKU
  • Dimensions & Weight
  • Featured Image
  • Description
  • Brand
  • Stock (via the "inventory adjustments" webhook type)

How to trigger Item Webhook when a new Sales Order is confirmed

Unfortunately the Item Webhooks are not triggered when a new Sales Order is created. For some stores this might cause problems with up-to-date stock level data to the store, especially when you have a high order frequentie. There is only one solution for this, namely to create a custom function and having that linked to a Sales Order Workflow Rule. 

Notes
First create a custom field for items called "textarea" for example. If you give it a different name, then please make sure to copy the API name as you will need it in the code below.

1. Create a Custom Function via Settings > Webhook Actions > Custom Functions


2. Click on "connections" and create a new connection. 

3. Give the application name called "zom" for example and find the scope "Full" and enable it like this:
Now click on "Create and Connect" to create the connection and to use it. 

4. Copy the below script and paste it into the script field, make sure to replace "organisation_id" with your actual organisation id:
Notes
// Update the Items custom field cf_textarea with a short random string 
salesorderID = salesorder.get("salesorder_id");
// Get the line items of the sales order 
items = salesorder.get("line_items");
// Iterate through each item and update cf_textarea 
itemLis = List();
for each  item in items
{
// get item id 
itemId = item.get("item_id");
// Generate a short random string (e.g., 9 characters) 
myList = {0,1,2,3,4,5,6,7,8,9,10,11,12};
passString = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
newList = "";
for each index i in myList
{
randnum = randomNumber(1,62);
newList = newList + substring(passString,randnum,randnum + 1);
}
unique_pass = replaceAll(newList,",","");
// info unique_pass;
// Update the cf_textarea field of the item 
itemmap = Map();
itemmap.put("item_id",itemId);
Linecfs = List();
cf = Map();
cf.put("api_name","cf_textarea");
cf.put("value",unique_pass);
Linecfs.add(cf);
itemmap.put("custom_fields",Linecfs);
itemLis.add(itemmap);
info itemmap;
response = zoho.inventory.updateRecord("Items","organisation_id",itemId,itemmap,"zom");
}
info response;
Then click on "Save".  The above code will generate a random value for the text field "textarea", which in turn will trigger the Item Webhook :)

5. Finally create or edit your Sales Order Workflow Rule and ensure the Custom Function is triggered whenever an Order is created or edited.