=== Afileasy for WooCommerce ===
Contributors: afileasy, uellpalma
Tags: woocommerce, affiliates, referrals, commissions
Requires at least: 6.0
Tested up to: 7.1
Requires PHP: 7.4
Stable tag: 1.3.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Track affiliate referrals, register referred sign-ups as leads, attribute orders automatically, and sync sales and refunds with Afileasy to generate commissions.

== Description ==

Afileasy for WooCommerce connects your store to your Afileasy affiliate program without writing a single line of code:

* **Automatic tracking** — embeds the Afileasy script on your storefront and stores the referral (`?ref=...`) in a cookie.
* **Lead capture** — when a referred visitor creates an account, the sign-up is registered as a lead for the affiliate, before any purchase.
* **Automatic attribution** — when the customer checks out, the referral is saved on the order, and the coupon codes used are reported so an affiliate coupon also credits the sale.
* **Affiliate coupons** — coupons created in Afileasy are published in your store as real WooCommerce coupons, and expired there when they are deactivated.
* **Coupon autofill** — a visitor who arrives through an affiliate link gets that affiliate's coupon applied to the cart automatically, without typing it.
* **Synchronization** — when an order is paid or refunded, the event is sent to Afileasy, which creates or reverses the commission.

Events are delivered through Action Scheduler (already bundled with WooCommerce), so checkout is never blocked and failed deliveries are retried automatically.

= External services =

This plugin connects your store to **Afileasy** (https://afileasy.com), an affiliate program management service. The plugin does not work without that connection, and **no data leaves your store until you paste your connection key** under WooCommerce → Afileasy.

Once connected, the integration communicates with Afileasy at four moments:

**1. Tracking script (every storefront page)**

The plugin adds a `<script>` loaded from the Afileasy servers to every public page of your store. That script reads the referral parameter from the URL (`?ref=...`) and stores it in the `afileasy_ref` cookie in the visitor's browser, so the sale can be credited to the right affiliate. When the affiliate has a coupon, Afileasy also returns it and the script stores it in the `afileasy_coupon` cookie, which is what lets this plugin apply the coupon to the cart. Loading the script tells Afileasy the address of the page being visited and the visitor's IP address, as with any HTTP request.

**2. Leads (when a referred visitor creates an account)**

When a visitor who arrived through an affiliate link creates a customer account — from My Account or from checkout — the plugin sends their **email address and name** to Afileasy, along with the referral identifier stored in the cookie, so the affiliate is credited with the lead. Accounts created without a referral send nothing.

Developers can disable this with the `afileasy_capture_leads` filter:

`add_filter( 'afileasy_capture_leads', '__return_false' );`

**3. Order events (when an order is paid or refunded)**

On those two events, and only on those, the plugin sends the following to Afileasy:

* From the order: identifier, status, currency, total, refunded amount, the coupon codes applied, and the creation, payment and modification dates.
* From the customer: identifier, **email address and name** provided at billing.
* The referral identifier stored on the order, used to credit the commission.

This data is required to calculate, create and reverse affiliate commissions. By using this plugin you share your customers' personal data with Afileasy — make sure your store's privacy policy covers it.

**4. Coupons (when you connect the store)**

So that the coupons you create in Afileasy become real coupons in your store, the plugin creates a WooCommerce REST API key (visible under WooCommerce → Settings → Advanced → REST API) and sends it to Afileasy together with your store address. Afileasy then uses the WooCommerce REST API to create, update and expire only those coupons. The key is deleted when you uninstall the plugin, and you can revoke it at any time in WooCommerce — coupon synchronization stops, everything else keeps working.

* Terms of service: https://afileasy.com/termos
* Privacy policy: https://afileasy.com/privacidade

== Installation ==

1. Install and activate the plugin.
2. In your Afileasy dashboard, go to Settings → Integrations → WooCommerce and click "Generate connection key".
3. Copy the key and paste it under WooCommerce → Afileasy.
4. Click "Test connection" to confirm it works.

== Frequently Asked Questions ==

= Do I need an Afileasy account? =

Yes. The plugin is a client for the Afileasy service and needs a connection key generated in the Afileasy dashboard.

= When is a lead registered? =

When a visitor arrives through an affiliate link and creates a customer account, either from My Account or by opting to create an account at checkout. Guest checkouts are not leads — those orders are attributed directly. Other forms (newsletter, contact) can be captured by adding `data-afileasy-lead` to the form in your theme.

= Does it work with block checkout? =

Yes. The referral is read server-side when the order is created, so both the classic checkout and the block (Store API) checkout are supported. Coupon autofill runs when the cart totals are calculated, which covers both checkouts as well.

= Why was the affiliate's coupon not applied? =

The coupon has to exist in this store: coupons created in Afileasy before you connected the integration are only published here after you sync them (Afileasy → Coupons → select → "Sincronizar com as integrações"). Autofill is also skipped when the shopper already applied a coupon of their own, when they removed the affiliate's coupon, or when the cart does not meet the coupon's own rules.

= Is it compatible with High-Performance Order Storage (HPOS)? =

Yes. The plugin declares compatibility with WooCommerce custom order tables.

== Changelog ==

= 1.3.0 =
* Coupon autofill: the affiliate's coupon is applied to the cart on its own when the visitor arrived through the affiliate link. A coupon the shopper applied themselves is never replaced, and removing the coupon keeps it removed.

= 1.2.1 =
* The store now announces itself right after the plugin is updated, so coupon synchronization starts without re-saving the connection key.

= 1.2.0 =
* Affiliate coupons: coupons created in Afileasy are published in your store as WooCommerce coupons, and expired there when deactivated. Requires reconnecting once so the store can register itself.
* Orders now report the coupon codes used, so a sale closed with an affiliate coupon is credited even without a referral click.

= 1.1.0 =
* Lead capture: referred visitors who create an account are registered as leads for the affiliate. Can be disabled with the `afileasy_capture_leads` filter.
* The connection key now carries the endpoints assembled by Afileasy, so the plugin keeps working when those services move to their own hosts. Existing connections are unaffected; generate a new key to pick them up.

= 1.0.0 =
* Initial release: tracking, automatic attribution, and sales/refund synchronization.
