WooCommerce triggers specific actions whenever an order is completed, its status changes, or a product is created or modified. With the plugin “API Data Sync for WooCommerce”, you can define which shortcode should be executed in response to these actions.

For instance, this functionality enables you to send data about a new product or order to another shop system seamlessly. This can be achieved using the PRO version of the JSON Content Importer plugin or any other plugin that supports suitable shortcodes. By doing so, you can maintain synchronization between WooCommerce and another shop system.

Requirements

  1. WooCommerce Plugin: Installed on the store where actions will be triggered.
  2. Shortcode-Providing Plugin: A second plugin is necessary to provide a shortcode that will be executed by WooCommerce’s actions.
    • You can either create this plugin yourself in PHP and integrate it with an API of your choice.
    • The WooCommerce data related to the triggered action is available through the shortcode parameter param1.
    • Alternatively, the PRO version of the JSON Content Importer plugin can handle this task effectively.

Example: Connecting Dropshipzone with WooCommerce

Dropshipzone is an Australian B2B2C marketplace designed for dropshipping businesses. You can use WooCommerce as your frontend shop, while orders are fulfilled through Dropshipzone. Instead of manually copying and pasting order data, an automated process using the Dropshipzone-API is a much more efficient solution.

To achieve this, the WooCommerce event triggered by a completed order can send the order details directly to Dropshipzone via their Place order API endpoint. This integration streamlines the entire process, reducing manual effort and minimizing the risk of errors.

The “Place Order” action involves sending data to https://api.dropshipzone.com.au/placingOrder via an HTTP POST request, with proper authorization. This functionality is exactly what the JSON Content Importer PRO plugin is designed to handle.

Steps to Set Up the Integration

When a customer completes an order in your WooCommerce store, the defined shortcode is executed. During this process:

  • The placeholder #orderdata# is automatically replaced with the actual order data collected from WooCommerce.
  • This formatted data is then sent to the Dropshipzone API endpoint, ensuring the order details are communicated accurately for fulfillment.
1. Select the Event Trigger

In the “API Data Sync for WooCommerce” plugin, choose the option “Order completed” as the trigger event. This ensures that the action is executed whenever a customer completes an order.

2. Define the Shortcode

Set up the following shortcode in the plugin configuration:

[jsoncontentimporterpro nameoftemplate="woocommerce_is_sending" httpstatuscodemustbe200="no" param1='#orderdata#']
  • nameoftemplate: Refers to the template that defines how data is formatted and sent to the API.
  • httpstatuscodemustbe200="no": Configures the plugin to proceed regardless of the HTTP status code.
  • param1='#orderdata#': Ensures that the placeholder #orderdata# is replaced with real-time data from WooCommerce about the completed order.
3. Create the First JSONContentImporter Template for Sending Data

To integrate with Dropshipzone, you need to create a template in the JSON Content Importer PRO plugin. This template will define how WooCommerce order data is sent to Dropshipzone’s “Place Order” API endpoint.

  • Name of the Template: woocommerce_is_sending
  • URL: https://api.dropshipzone.com.au/placingOrder

Template Content:

The template should include the JSON structure for the API request. You may add data to ‘scres’ as needed. The plugin’s logging feature in “API Data Sync for WooCommerce” will track the API response under the apianswer field, adds some Info (“api”, “apiaction”, “apiurl”) for the Logfile and display the complete response for debugging and verification.

{% set scres = [] %}
{% set scres = scres|merge({ 'api': 'dropshipzone' }) %}
{% set scres = scres|merge({ 'apiaction': 'placingOrder' }) %}
{% set scres = scres|merge({ 'apiurl': 'https://api.dropshipzone.com.au/placingOrder' }) %}
{% set scres = scres|merge({ 'apianswer': (_context) }) %}
{{ scres | json_encode() }}

Curloptions: Here TWO challenges are solved!

  • Authorization:
    Dropshipzone requires an OAuth token for authorization. This can be handled by creating another JSONContentImporter template specifically for fetching the token. The retrieved token is then passed dynamically in the CURLOPT_HTTPHEADER for the API request.
    Another JCI-Template “gettoken” (see below) is executed to get the OAuth-Token from Dropshipzone for sending it via CURLOPT_HTTPHEADER
  • CURLOPT_POSTFIELDS: The Dropshipzone-API expects data as JSON, see https://www.dropshipzone.com.au/apidoc/index.html#api-Order-Place_Order . The twig-code uses the Woocommerce-Data which is send via “param1” in the Shortcode. Mind that “param1” comes as urlencoded, so that is has to be urldecoded (therefore “urldecode()”).
CURLOPT_HTTPHEADER=Authorization:jwt #BRO#jsoncontentimporterpro nameoftemplate=gettoken orderofshortcodeeval=10 httpstatuscodemustbe200=no#BRC###Content-Type:application/json;CURLOPT_POSTFIELDS={% set orderin = '##param1##' | urldecode() | json_decode(TRUE)%}{% set orderjson = [] %}{% set b = orderin.order.billing %}{% set orderjson = orderjson | merge({ 'your_order_no': (orderin.order.id), 'first_name': (b.first_name), 'last_name':  (b.last_name),'address1':  (b.address_1), 'address2':  (b.address_2),'suburb':  (b.city),'state':  (b.state),'postcode':  (b.postcode),'telephone':  (b.phone), 'comment': (orderin.order.customer_note) }) %} {% set orderprod = [] %}{% for pk,pv in orderin.product %}{% set orderprod = orderprod | merge([ {"sku": (pv.sku),"qty": (pv.quantity)} ]) %}{% endfor %}{% set orderjson = orderjson | merge({ 'order_items':  (orderprod) } ) %}{% set orderstr = orderjson | json_encode() %}{{orderstr}}
4. Create the SECOND JSONContentImporter-Template for Authorization

The Dropshipzone-API expects a “Authorization”-Headerfiled in the http-request. This fields has to have a Token, which has to be requested before (OAuth). With a 2nd JCI-Template you can do that. This Template is part of the Curloptions in the Step before.
Name of the Template: gettoken
URL: https://api.dropshipzone.com.au/auth
Template:

{{ token }}

Curloptions: Insert for YOUR_EMAIL / YOUR-PASSWORD the info you got from Dropshipzone.

CURLOPT_HTTPHEADER=Content-Type: application/json;CURLOPT_POSTFIELDS={"email": "YOUR_EMAIL", "password": "YOUR-PASSWORD" }

Key Benefits

  • Automation: Reduces manual order processing by automating data transfer.
  • Efficiency: Minimizes the potential for errors, ensuring orders are processed faster and more accurately.
  • Flexibility: Leverages the JSON Content Importer plugin’s ability to handle dynamic data and API integrations.

This setup ensures seamless synchronization between your WooCommerce store and Dropshipzone, enhancing the overall workflow and customer experience