For Zoho Services only:


I'm actually part of something bigger at Ascent Business Solutions recognized as the top Zoho Premium Solutions Partner in the United Kingdom.

Ascent Business Solutions offer support for smaller technical fixes and projects for larger developments, such as migrating to a ZohoCRM.  A team rather than a one-man-band is always available to ensure seamless progress and address any concerns. You'll find our competitive support rates with flexible, no-expiration bundles at http://ascentbusiness.co.uk/zoho-support-2.  For larger projects, check our bespoke pricing structure and receive dedicated support from our hands-on project consultants and developers at http://ascentbusiness.co.uk/crm-solutions/zoho-crm-packages-prices.

The team I manage specializes in coding API integrations between Zoho and third-party finance/commerce suites such as Xero, Shopify, WooCommerce, and eBay; to name but a few.  Our passion lies in creating innovative solutions where others have fallen short as well as working with new businesses, new sectors, and new ideas.  Our success is measured by the growth and ROI we deliver for clients, such as transforming a garden shed hobby into a 250k monthly turnover operation or generating a +60% return in just three days after launch through online payments and a streamlined e-commerce solution, replacing a paper-based system.

If you're looking for a partner who can help you drive growth and success, we'd love to work with you.  You can reach out to us on 0121 392 8140 (UK) or info@ascentbusiness.co.uk.  You can also visit our website at http://ascentbusiness.co.uk.

Zoho Deluge and Wordpress/WooCommerce API: Get All Products

What?
A quick article on retrieving all the products from a WooCommerce instance on a client's Wordpress website.

Why?
A client of ours wants the information entered against products in their WooCommerce to display on their estimates in Zoho Books; such as product image, product name, product description.

Granted that in some cases, the design should go in the other direction, as in enter the products in Zoho Inventory and this feeds the website. More than often, however, this request comes in when the customer has already been setting their Zoho environment up and the Wordpress site has the more information.

How?
So first, this article shows you how to setup an API connection, and then we'll include the code to count the total number of products, and then finallly show the code to loop through all the pages to retrieve every product from the WooCommerce system.

API connection in WooCommerce
So we need to get an API connection to ensure we get all the products. This involves getting a consumer_key and a consumer_secret.
  1. Login to the administrator's section of the Wordpress website (mysite.com/wp-admin)
  2. Expand WooCommerce in the left sidebar
  3. Go to Settings (in the menu/sidebar) > Advanced (tab) > REST API (link under the tabs)
  4. Click on the Add key button
  5. Give it a description, assign a user that it will run as (preferably of the admin or account that will never leave the business), and permissions can be "Read".
  6. Click on the "Generate API Key" button
  7. Note the consumer_key and consumer_secret somewhere (these aren't displayed to you in future - so this is your one chance, or else you need to generate another key)

Zoho Deluge
Here's the code to retrieve all the product categories. We need this to get the count of all products (if there's another way, I'd love to hear it). We don't have more than 100 product categories so we'll just to page 1 but specifying 100 per page as the default is only 10.
copyraw
//
// API keys (these are made up so enter your own in these 2 lines below
v_MyWordPressSiteUrl = "https://mysite.com";
v_ConsumerKey = "ck_aaaabbbbccccddddeeeeffff1111222233334444";
v_ConsumerSecret = "cs_555566667777888899990000aaaabbbbccccdddd";
//
// connection parameters
l_ConnectionParams = List();
l_ConnectionParams.add("consumer_key=" + v_ConsumerKey);
l_ConnectionParams.add("consumer_secret=" + v_ConsumerSecret);
//
// get product count by using categories
v_EndpointCategories = v_MyWordPressSiteUrl + "/wp-json/wc/v3/products/categories";
l_PaginationParams = List();
l_PaginationParams.add("page=1");
l_PaginationParams.add("per_page=100");
l_PaginationParams.add("order_by=id");
//
// we have no more than 100 categories so let's start counting
r_Categories = invokeurl
[
	url: v_EndpointCategories + "?" + l_ConnectionParams.toString("&") + "&" + l_PaginationParams.toString("&")
	type: GET
];
v_TotalProducts = 0;
v_TotalCategories = r_Categories.toJSONList().size();
for each m_Category in r_Categories.toJSONList()
{
	if(!isNull(m_Category.get("count")))
	{
		v_TotalProducts = v_TotalProducts + m_Category.get("count");
	}
}
info v_TotalProducts + " in " + v_TotalCategories;
//
// if categories >= 100 then should loop this to get all the categories
  1.  // 
  2.  // API keys (these are made up so enter your own in these 2 lines below 
  3.  v_MyWordPressSiteUrl = "https://mysite.com"
  4.  v_ConsumerKey = "ck_aaaabbbbccccddddeeeeffff1111222233334444"
  5.  v_ConsumerSecret = "cs_555566667777888899990000aaaabbbbccccdddd"
  6.  // 
  7.  // connection parameters 
  8.  l_ConnectionParams = List()
  9.  l_ConnectionParams.add("consumer_key=" + v_ConsumerKey)
  10.  l_ConnectionParams.add("consumer_secret=" + v_ConsumerSecret)
  11.  // 
  12.  // get product count by using categories 
  13.  v_EndpointCategories = v_MyWordPressSiteUrl + "/wp-json/wc/v3/products/categories"
  14.  l_PaginationParams = List()
  15.  l_PaginationParams.add("page=1")
  16.  l_PaginationParams.add("per_page=100")
  17.  l_PaginationParams.add("order_by=id")
  18.  // 
  19.  // we have no more than 100 categories so let's start counting 
  20.  r_Categories = invokeUrl 
  21.  [ 
  22.      url: v_EndpointCategories + "?" + l_ConnectionParams.toString("&") + "&" + l_PaginationParams.toString("&") 
  23.      type: GET 
  24.  ]
  25.  v_TotalProducts = 0
  26.  v_TotalCategories = r_Categories.toJSONList().size()
  27.  for each m_Category in r_Categories.toJSONList() 
  28.  { 
  29.      if(!isNull(m_Category.get("count"))) 
  30.      { 
  31.          v_TotalProducts = v_TotalProducts + m_Category.get("count")
  32.      } 
  33.  } 
  34.  info v_TotalProducts + " in " + v_TotalCategories; 
  35.  // 
  36.  // if categories >= 100 then should loop this to get all the categories 

Code to get all products
Here we need to loop through all the available pages, so including the above plus some calculation to work out how many times to loop should retrieve every thing:
copyraw
//
// initialize
l_AllProducts = List();
v_TotalCategories = 0;
v_TotalProducts = 0;
v_Page = 1;
// do not set this to zero!!!
v_PerPage = 100;
//
// API keys
v_MyWordPressSiteUrl = "https://mysite.com";
v_ConsumerKey = "ck_aaaabbbbccccddddeeeeffff1111222233334444";
v_ConsumerSecret = "cs_555566667777888899990000aaaabbbbccccdddd";
//
// connection parameters
l_ConnectionParams = List();
l_ConnectionParams.add("consumer_key=" + v_ConsumerKey);
l_ConnectionParams.add("consumer_secret=" + v_ConsumerSecret);
//
// get product count by using categories
v_EndpointCategories = v_MyWordPressSiteUrl + "/wp-json/wc/v3/products/categories";
l_PaginationParams = List();
l_PaginationParams.add("page=" + v_Page);
l_PaginationParams.add("per_page=" + v_PerPage);
l_PaginationParams.add("order_by=id");
//
// we have no more than 100 categories so let's start counting
r_Categories = invokeurl
[
	url :v_EndpointCategories + "?" + l_ConnectionParams.toString("&") + "&" + l_PaginationParams.toString("&")
	type :GET
];
if(r_Categories.toString().contains("count"))
{
	v_TotalCategories = r_Categories.toJSONList().size();
}
for each  m_Category in r_Categories.toJSONList()
{
	if(!isNull(m_Category.get("count")))
	{
		v_TotalProducts = v_TotalProducts + m_Category.get("count");
	}
}
//
// if categories >= 100 then should loop this to get all the categories
// let's calculate how many pages we need
v_MaxPages = ceil(v_TotalProducts / v_PerPage);
//
// generate a loop list
l_ProductPages = " ".leftpad(v_MaxPages - 1).replaceAll(" ",",").toList();
for each  v_PageIndex in l_ProductPages
{
	//
	// products
	v_EndpointProducts = v_MyWordPressSiteUrl + "/wp-json/wc/v3/products";
	//
	l_PaginationParams = List();
	l_PaginationParams.add("page=" + v_Page);
	// maximum number of products per page
	l_PaginationParams.add("per_page=" + v_PerPage);
	// missing products if using default order by as date
	l_PaginationParams.add("order=asc");
	l_PaginationParams.add("order_by=id");
	//
	r_AllProducts = invokeurl
	[
		url :v_EndpointProducts + "?" + l_ConnectionParams.toString("&") + "&" + l_PaginationParams.toString("&")
		type :GET
	];
	l_ThisList = r_AllProducts.toJSONList();
	//
	// add this list to our existing list
	l_AllProducts.addAll(l_ThisList);
	//
	// stop the loop if there weren't any more products
	if(l_ThisList.size() < 1)
	{
		break;
	}
	//
	// increment page
	v_Page = v_Page + 1;
}
//
// generate a file
f_AllProducts = l_AllProducts.toFile("all_products-" + zoho.currenttime.toString("yyyy-MM-dd-HH-mm") + ".json");
//
// send this to us via email
sendmail
[
	from :zoho.adminuserid
	to :"This email address is being protected from spambots. You need JavaScript enabled to view it."
	subject :"All Products from " + v_MyWordPressSiteUrl + " as of " + zoho.currenttime.toString("yyyy-MM-dd HH:mm:ssXXX")
	message :"Please see the attached file"
	Attachments :file:f_AllProducts
]
//
return v_TotalProducts + " product(s) in " + v_TotalCategories + " categories across " + v_Page + " page(s).";
  1.  // 
  2.  // initialize 
  3.  l_AllProducts = List()
  4.  v_TotalCategories = 0
  5.  v_TotalProducts = 0
  6.  v_Page = 1
  7.  // do not set this to zero!!! 
  8.  v_PerPage = 100
  9.  // 
  10.  // API keys 
  11.  v_MyWordPressSiteUrl = "https://mysite.com"
  12.  v_ConsumerKey = "ck_aaaabbbbccccddddeeeeffff1111222233334444"
  13.  v_ConsumerSecret = "cs_555566667777888899990000aaaabbbbccccdddd"
  14.  // 
  15.  // connection parameters 
  16.  l_ConnectionParams = List()
  17.  l_ConnectionParams.add("consumer_key=" + v_ConsumerKey)
  18.  l_ConnectionParams.add("consumer_secret=" + v_ConsumerSecret)
  19.  // 
  20.  // get product count by using categories 
  21.  v_EndpointCategories = v_MyWordPressSiteUrl + "/wp-json/wc/v3/products/categories"
  22.  l_PaginationParams = List()
  23.  l_PaginationParams.add("page=" + v_Page)
  24.  l_PaginationParams.add("per_page=" + v_PerPage)
  25.  l_PaginationParams.add("order_by=id")
  26.  // 
  27.  // we have no more than 100 categories so let's start counting 
  28.  r_Categories = invokeUrl 
  29.  [ 
  30.      url :v_EndpointCategories + "?" + l_ConnectionParams.toString("&") + "&" + l_PaginationParams.toString("&") 
  31.      type :GET 
  32.  ]
  33.  if(r_Categories.toString().contains("count")) 
  34.  { 
  35.      v_TotalCategories = r_Categories.toJSONList().size()
  36.  } 
  37.  for each  m_Category in r_Categories.toJSONList() 
  38.  { 
  39.      if(!isNull(m_Category.get("count"))) 
  40.      { 
  41.          v_TotalProducts = v_TotalProducts + m_Category.get("count")
  42.      } 
  43.  } 
  44.  // 
  45.  // if categories >= 100 then should loop this to get all the categories 
  46.  // let's calculate how many pages we need 
  47.  v_MaxPages = ceil(v_TotalProducts / v_PerPage)
  48.  // 
  49.  // generate a loop list 
  50.  l_ProductPages = " ".leftpad(v_MaxPages - 1).replaceAll(" ",",").toList()
  51.  for each  v_PageIndex in l_ProductPages 
  52.  { 
  53.      // 
  54.      // products 
  55.      v_EndpointProducts = v_MyWordPressSiteUrl + "/wp-json/wc/v3/products"
  56.      // 
  57.      l_PaginationParams = List()
  58.      l_PaginationParams.add("page=" + v_Page)
  59.      // maximum number of products per page 
  60.      l_PaginationParams.add("per_page=" + v_PerPage)
  61.      // missing products if using default order by as date 
  62.      l_PaginationParams.add("order=asc")
  63.      l_PaginationParams.add("order_by=id")
  64.      // 
  65.      r_AllProducts = invokeUrl 
  66.      [ 
  67.          url :v_EndpointProducts + "?" + l_ConnectionParams.toString("&") + "&" + l_PaginationParams.toString("&") 
  68.          type :GET 
  69.      ]
  70.      l_ThisList = r_AllProducts.toJSONList()
  71.      // 
  72.      // add this list to our existing list 
  73.      l_AllProducts.addAll(l_ThisList)
  74.      // 
  75.      // stop the loop if there weren't any more products 
  76.      if(l_ThisList.size() < 1) 
  77.      { 
  78.          break
  79.      } 
  80.      // 
  81.      // increment page 
  82.      v_Page = v_Page + 1
  83.  } 
  84.  // 
  85.  // generate a file 
  86.  f_AllProducts = l_AllProducts.toFile("all_products-" + zoho.currenttime.toString("yyyy-MM-dd-HH-mm") + ".json")
  87.  // 
  88.  // send this to us via email 
  89.  sendmail 
  90.  [ 
  91.      from :zoho.adminuserid 
  92.      to :"This email address is being protected from spambots. You need JavaScript enabled to view it." 
  93.      subject :"All Products from " + v_MyWordPressSiteUrl + " as of " + zoho.currenttime.toString("yyyy-MM-dd HH:mm:ssXXX") 
  94.      message :"Please see the attached file" 
  95.      Attachments :file:f_AllProducts 
  96.  ] 
  97.  // 
  98.  return v_TotalProducts + product(s) in " + v_TotalCategories + " categories across " + v_Page + page(s)."

Additional:
  • Total product count may differ from the actual product count as products can belong to multiple categories. Consider just using the WooCommerce interface to check the total number of products (wp-admin > Products > All Products).

Source(s):
Category: Zoho :: Article: 871

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.