How To Integrate WooCommerce With Saipos (external API)

J. M. Rodrigues
4 min readAug 23, 2023

In this post, we’re diving into the dynamic world of WooCommerce integrations, particularly with Saipos. Whether you’re a developer looking for a hands-on guide or simply curious about how WooCommerce can work with Saipos or some external API, you’re in the right place. I’ll break down the steps I've followed in order to make this integration.

Woo is the open-source ecommerce platform that helps merchants and developers build successful businesses for the long term.

First off, let’s briefly introduce Saipos for those unfamiliar. Saipos is a solution for the restaurant business, it offers diverse service modules that adapt to various restaurant styles — whether it’s table service, ticket-based, or counter service. If you're working with some ecommerce that is restaurant like, Saipos can be a way to go.

To start, we’ll use the functions.php file to test our integration. This allows us to experiment and ensure the integration works smoothly. Once we’ve got everything running smoothly, we’ll transition to creating a dedicated WordPress plugin for a permanent solution.

Appearance → Theme File Editor

In the world of WooCommerce, hooks are your best friend for integrating external systems. For our integration, we’ll be utilizing hooks to make POST requests to the Saipos API at specific points in the order process. These hooks are pivotal in orchestrating a seamless connection between WooCommerce and Saipos. Check out this Stack Overflow post for guidance: stackoverflow.com/questions/37868183/woocommerce-new-order-action-get-order-information.

During our journey, I discovered an essential insight. I initially used the woocommerce_new_order hook to access order items, only to realize that it didn't grant us the desired access. Thankfully, I've found a solution in the form of the woocommerce_checkout_order_processed hook. This change proved vital in successfully accessing and handling order items. Find more details here: stackoverflow.com/a/71456129.

<?php
add_action('woocommerce_checkout_order_processed', 'saipos_api_integration', 10, 10);
function saipos_api_integration($order_id) { }

In order to Saipos accept our items we need to parse them to a specific object format, here's the method I created to create a simple item reference and insert to the array of items expected by Saipos:

function getOrderItems($total_order_items)
{
$items_to_return = [];
foreach ($total_order_items as $item_key => $item) {
$item_data = $item->get_data();
$product_name = $item_data['name'];
$quantity = $item_data['quantity'];
$product = $item->get_product();
$item_object = [
'integration_code' => '1234',
'desc_item' => $product_name,
'quantity' => $quantity,
'unit_price' => floatval($product->get_price()),
'notes' => '',
'choice_items' => [],
];
$items_to_return[] = $item_object;
}
return $items_to_return;
}

You could indeed explore this code further and find the fields that fits you the best, I didn't need the 'notes' or 'choice_items', therefore I left those empty. To dig a little deeper on the possible fields I recommend you to var_dump the produt and/or item_data.

Saipos API reference to create an order: link here.

With our order items in hand, we crafted the necessary payload and started sending requests to the Saipos API. This phase marks a turning point, where we bring our integration to life by interacting with the API. The Saipos API documentation and details can be found here.

For obtaining the API token, we encapsulated the process in a function named getTokenSaipos. This function handles the authentication by making a secure POST request to the Saipos API. The Brazilian character quirks you may encounter are due to Saipos' origin.

function getTokenSaipos()
{
$api_url = 'https://homolog-order-api.saipos.com/auth';
$request_args = array(
'headers' => array(
'accept' => 'application/json',
'content-type' => 'application/json',
),
'body' => json_encode(array(
'idPartner' => 'id_partner_here',
'secret' => 'secret_here',
)),
);

$response = wp_safe_remote_post($api_url, $request_args);

if (is_wp_error($response)) {
error_log('Error: ' . $response->get_error_message());
return 'Response body: ' . wp_remote_retrieve_body($response);
} else {
$response_body = wp_remote_retrieve_body($response);
return $response_body;
}
}

After acquiring the token, we proceeded to make the POST request to the Saipos API. If any issues arise during this process, error messages are logged so you can debug.

After acquiring the token, we proceeded to make the POST request to the Saipos API to create the order. The complete code for this step can be found on my GitHub repository here. Keep in mind that you might need to make adjustments to the code to suit your specific requirements and ensure the integration remains stable and reliable.

Once I was confident that my integration was working seamlessly, Idecided to create a dedicated WordPress plugin for it. To start, I created a new folder with the name of my plugin, as well as a PHP file with the same name, "saipos_integration".

In the “saipos_integration.php” file, it’s essential to include the following headers at the beginning of the file. These headers provide crucial information about the plugin to WordPress and ensure its proper functioning:

/**
* Plugin Name: Saipos Integration
* Description: Integrates WooCommerce with Saipos API
* Version: 1.0.0
* Author: Your Name
* Author URI: Your Author URI
* Text Domain: saipos-integration
*/

Feel free to make the necessary adjustments to the plugin name, description, version, and other details according to your preferences.

With the headers in place, your integration plugin is good to go. Now place your PHP code below the header, zip the folder and publish at the WordPress page.

As a final step, remember to remove any references to the integration code from the functions.php file before activating the plugin, this avoids conflicts.

I hope this article has provided you with valuable insights into integrating WooCommerce with the Saipos API. Remember, you can find the complete code for this integration on my GitHub repository here. If you have any questions or need further assistance, feel free to reach out to me on Telegram (@joao_rodrigues1). Happy coding!

--

--

J. M. Rodrigues

A fullstack software developer who loves problem solving.