=== TrueMargin — Price & Margin Auditor for WooCommerce ===
Contributors: alainmilian
Tags: woocommerce, margin, profit, cost, pricing
Requires at least: 6.5
Tested up to: 7.1
Requires PHP: 7.4
Requires Plugins: woocommerce
Stable tag: 1.1.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Know your true margin before you sell at a loss. Track product cost, get a real-time margin health dashboard, and stop discount-driven losses.

== Description ==

TrueMargin adds a cost price field to every product and variation, calculates the real margin after all discounts, and warns you — or protects the sale — when a discount would push a product below cost.

= Free =

* Cost price field on simple products and every variation.
* Margin column in the products list.
* Filter products list by margin status (no cost, below cost, below threshold, excluded).
* Margin health dashboard with products below cost and below your minimum margin.
* Email alert every time an order is paid with a line item below the threshold.
* Daily digest of margin activity.
* Exclude products you sell on purpose at a low margin (loss leaders, gifts…).

= Pro =

* Block checkout when the cart would sell below cost (classic and Blocks checkout).
* Order profit and margin box: real-time breakdown of cost of goods, net profit, and profit margin in the order totals.
* Native CSV sync: import and export product costs and exclusions using WooCommerce's built-in CSV tool.
* Smart coupon exclusions: automatically skip discounting low-margin items without blocking the entire cart.
* Quick Edit and Bulk Edit: update cost prices directly from the WooCommerce products list.
* WordPress Dashboard Widget: instant overview of losses prevented, blocked checkouts, and catalog health.
* Coupon impact simulator on the coupon screen (supports simple and variable products).
* Alerts history with filters, stats, and CSV export.

== Installation ==

1. Upload the `truemargin` folder to `/wp-content/plugins/`, or install the ZIP from *Plugins → Add New → Upload Plugin*.
2. Activate the plugin through the *Plugins* screen. WooCommerce must be active.
3. Go to *TrueMargin → Settings* and set your minimum margin and alert recipients.
4. Open any product and fill in the **Cost price** field (or use Quick Edit in the products list).

== Frequently Asked Questions ==

= Do I need a license key? =
No. The free version works without a license. A Pro license unlocks checkout blocking, smart coupon exclusions, quick/bulk editing, coupon simulation, the dashboard widget, alerts history, and CSV exports.

= How does it calculate cost? =
You enter the purchase cost **without taxes**. TrueMargin multiplies it by the quantity and compares it with the net line total (after discounts, without taxes). So the margin is the real margin, not the one before coupons.

= What happens with variable products? =
The cost is stored per variation. The products list shows the worst margin among the variations that have a cost. The coupon simulator checks every variation.

= Can I exclude a product? =
Yes. Tick **Exclude from margin control** on the product screen and TrueMargin will ignore it in the dashboard, alerts, and checkout rule.

= Are unpaid orders counted? =
No. Alerts are only recorded once the order is paid (payment complete, processing or completed status).

= Why is the dashboard cached? =
Walking the whole catalog is expensive on big stores, so results are cached for 10 minutes. Use **Recalculate now** to force a fresh scan.

== Screenshots ==

1. Margin health dashboard.
2. Cost price field on the product edit screen and Quick Edit.
3. WordPress Dashboard Widget with losses prevented.
4. Coupon impact simulator with smart margin protection (Pro).
5. Alerts history with filters and CSV export (Pro).

== Changelog ==

= 1.1.0 =
* New (Pro): Real-time Order Profit & Margin breakdown in WooCommerce order totals (HPOS & legacy).
* New (Pro): Native WooCommerce CSV product import/export support for costs and exclusions.
* New (Pro): Quick Edit and Bulk Edit for product cost prices in the products list table.
* New (Pro): WordPress Dashboard Widget summarizing losses prevented and margin status.
* New (Pro): Smart coupon exclusion — exclude low-margin items from discounts automatically without blocking the checkout.
* New: Product list filter dropdown to quickly find products with no cost, below cost, below threshold, or excluded.
* New: Help & Support screen with direct links to documentation, email support, and WhatsApp chat.
* Fix (Pro): Coupon impact simulator now properly evaluates all variations of variable products.
* Fix (Pro): Checkout guard throttling is now scoped per customer session.
* Fix: Automatic local license API detection for development environments.

= 1.0.1 =
* Fix: margins now account for line quantity instead of comparing a unit cost against the whole line total.
* Fix: revenue is calculated without taxes, consistently across checkout, guard and coupon simulator.
* Fix: alerts are recorded for Blocks / Store API checkout too.
* Fix: only paid orders are recorded as sales.
* Fix: variable products are audited per variation.
* Fix: order links honour HPOS.
* Fix: licence keys are verified against the licence server instead of being accepted by format alone.
* New: exclude products from margin control.
* New: choose whether to block only below-cost lines or also below the minimum margin.
* New: margin column in the orders list.
* New: the daily digest is now actually scheduled and emailed at 08:00 site time.
* New: dashboard scan is cached for 10 minutes with a "Recalculate now" button.
* New: weekly cleanup of alerts older than one year.
* New: admin warnings when checkout blocking is inactive or WP-Cron is disabled.

= 1.0.0 =
* First release.

== Upgrade Notice ==

= 1.1.0 =
Adds Quick/Bulk Edit for costs, WordPress Dashboard widget, smart coupon exclusions, and fixes variable products in the coupon simulator. Update recommended.
