=== Mr. J Dev Smart Discount Rules ===
Contributors: ajay2188, iamdeveloperajay
Tags: woocommerce, discount, pricing rules, buy x get y, auto-apply
Requires at least: 5.8
Tested up to: 6.9
Stable tag: 1.0.0
Requires PHP: 7.4
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Create advanced auto-apply discount rules beyond default WooCommerce coupons — with role, category, and day-of-week conditions.

== Description ==

Mr. J Dev Smart Discount Rules gives you a flexible, priority-based discount engine on top of WooCommerce. Rules are evaluated automatically at cart/checkout — no coupon code needed.

**Discount Types:**

* **Percentage** — e.g. "10% off the entire cart"
* **Fixed Amount** — e.g. "₹100 off cart"
* **Buy X Get Y** — e.g. "Buy 2, get 1 free" (cheapest eligible items are discounted)

**Conditions (can be combined):**

* Minimum cart spend
* User role (customer, wholesale, subscriber, etc.)
* Product category slug present in cart
* Day(s) of the week (time-based deals like weekend sales)

**Rule Control:**

* Priority system — only the highest-priority matching rule fires (no stacking)
* Enable / disable rules without deleting them
* Delete rules permanently

The discount appears as a cart fee line item with the rule name as its label, keeping the cart transparent to customers.

== Installation ==

1. Upload the `mr-j-dev-smart-discount-rules` folder to `/wp-content/plugins/`.
2. Activate the plugin through the **Plugins** menu in WordPress.
3. Go to **WooCommerce → Discount Rules** to create rules.

== Usage Examples ==

**Weekend 15% sale for all users:**

* Type: Percentage | Value: 15
* Conditions → Days: Saturday, Sunday

**₹500 spend → ₹50 off for Wholesale customers:**

* Type: Fixed | Value: 50
* Min Spend: 500 | Conditions → Role: wholesale_customer

**Buy 2 T-shirts, get 1 free (cheapest):**

* Type: Buy X Get Y | Buy: 2 | Get: 1
* Conditions → Category: t-shirts

== How Buy X Get Y Works ==

The Buy X Get Y rule uses a **group-based algorithm** that always gives the cheapest qualifying items for free.

**Algorithm (step by step):**

1. All items in the cart are flattened into individual unit prices.
   e.g. Cart: 2× T-shirt @ ₹200, 1× Jeans @ ₹800, 1× Cap @ ₹150
   → unit prices: [200, 200, 800, 150]

2. Prices are sorted cheapest-first:
   → [150, 200, 200, 800]

3. The number of complete qualifying groups is calculated:
   group size = Buy + Get = 3 + 1 = 4
   total items = 4 → floor(4 ÷ 4) = **1 group qualifies**

4. For each group, the cheapest `Get` items are free:
   → ₹150 (cap) is free → discount applied = **−₹150**

The discount is added as a negative cart fee line item labelled with the rule name, keeping the breakdown transparent to customers.

**Why cheapest-first?**

This prevents customers from gaming the rule by adding one cheap item to get an expensive item free. The cheapest item always fills the free slot.

**Only complete groups qualify:**

With Buy 2 Get 1 and 5 items in the cart:
→ floor(5 ÷ 3) = 1 complete group → 1 item free (the cheapest)
The leftover 2 items do not form a group and get no discount.

**Configuration examples:**

* Buy 1 Get 1 Free        → Buy: 1, Get: 1
* Buy 2 Get 1 Free        → Buy: 2, Get: 1
* Buy 3 Get 2 Free        → Buy: 3, Get: 2
* Buy 4 Get 1 Free (BOGO4)→ Buy: 4, Get: 1

Combine with a **Product Category** condition to restrict the rule to specific product lines (e.g. only T-shirts, only Electronics).

== Changelog ==

= 1.0.0 =
* Initial release.
