=== API Data Sync for WooCommerce ===
Contributors: berkux
Tags: api, woocommerce, webhook, sync, rest api
Requires at least: 6.5
Tested up to: 7.1
Requires PHP: 7.4
Stable tag: 1.0
License: GPLv3
License URI: https://www.gnu.org/licenses/gpl-3.0.html

Run a shortcode whenever WooCommerce completes an order, changes an order status or saves a product – and send the data to any external API.

== Description ==

WooCommerce fires an action whenever an order is completed, its status changes, or a product is created or updated. This plugin lets you pick one of these events and define a shortcode that is executed each time the event occurs.

The shortcode receives all relevant WooCommerce data (order, line items, SKUs, or product details) as a URL-encoded JSON string via the placeholder `#orderdata#`. Use it to push orders or products to another shop system, an ERP, a CRM, or any REST API – for example with the PRO version of the [JSON Content Importer](https://json-content-importer.com/) or with a shortcode plugin of your own.

= Features =

* **Event-based trigger** – choose one WooCommerce hook per site:
  * Order thank-you page (`woocommerce_thankyou`)
  * Order completed (`woocommerce_order_status_completed`)
  * Order status changed (`woocommerce_order_status_changed`)
  * Product created or updated (`woocommerce_update_product`)
* **Shortcode execution** – the configured shortcode runs with the event data injected via `#orderdata#`.
* **Duplicate protection** – orders are marked after a successful sync, so reloading the thank-you page does not trigger a second call.
* **Multisite-aware SKU lookup** – if a product is not found on the current site, the other sites of the network are checked.
* **Logging** – the last 20 events (event, order/product, shortcode result) are stored and shown in the admin, with links to the order or product.
* **E-mail notification** – optionally receive a short e-mail per event with order number, event type and shortcode result.
* **HPOS-compatible** – works with WooCommerce High-Performance Order Storage and the block-based Cart/Checkout.
* **Clean uninstall** – all settings and log data are removed on deletion if you opt in.

= Privacy =

The plugin only processes data on your own site. Order and product data are handed to the shortcode you configure; whether that shortcode sends data to a third party is up to the shortcode plugin you use. The built-in log stores event type, order/product ID and shortcode result – no customer names, addresses or e-mail addresses. The optional notification e-mail contains the order number and the shortcode result, not the order data itself.

== Installation ==

1. Install and activate WooCommerce.
2. Install and activate this plugin.
3. Install a plugin that provides the shortcode you want to run (e.g. JSON Content Importer PRO), or write your own.
4. Go to **API Data Sync → Basic Settings**, choose the WooCommerce event and enter the shortcode. Use `#orderdata#` as the placeholder for the event data.
5. Optionally enable logging and/or enter an e-mail address for notifications.
6. Place a test order or save a product and check the log.

== Frequently Asked Questions ==

= Which other plugins do I need? =

WooCommerce, and a plugin that provides the shortcode to be executed. The JSON Content Importer PRO plugin can do this out of the box; alternatively you can write a small plugin yourself that registers a shortcode, reads the `param1` attribute and calls your API.

= What data does the shortcode receive? =

For order events: the complete order (as returned by `WC_Order::get_data()`), all line items and the resolved SKU per item. For product events: ID, name, descriptions, prices, SKU, stock status, categories and tags. For status changes: order ID, old and new status. Everything is passed as one URL-encoded JSON string.

= An order was synced twice. Why? =

Each event is only processed once per order. If you switched the trigger from "thank-you page" to "order completed", both events can fire for the same order once. Also note that `woocommerce_order_status_changed` fires on every status change, including the change to "completed".

= Does the plugin work with HPOS? =

Yes. Compatibility with High-Performance Order Storage and Cart/Checkout Blocks is declared, and all order access goes through the WooCommerce CRUD API.

= Where can I get help? =

Open a topic in the plugin's support forum on WordPress.org.

== Screenshots ==

1. Check Installation – PHP and WooCommerce requirements
2. Basic Settings – event, shortcode, logging and log view

== Changelog ==

= 1.0 =
* Declared compatibility with WooCommerce High-Performance Order Storage (HPOS) and Cart/Checkout Blocks
* Orders are now marked after sync to prevent duplicate calls on thank-you page reloads
* Log and notification e-mails no longer contain customer data; log entries link to the order or product
* Log option is no longer autoloaded
* Admin capability changed from role name to `manage_woocommerce`
* Simplified nonce handling on the settings page
* Multisite SKU lookup checks the current site first and stops at the first match
* Tested with WordPress 7.1, WooCommerce 11.1 and Plugin Check 2.1.0

= 0.7 =
* Tested with WordPress 7.0 and Plugin Check 1.9.0

= 0.3 =
* Initial version

== Upgrade Notice ==

= 1.0 =
Security and privacy improvements, HPOS compatibility. The log format has changed; existing log entries are shown without event details.