# Dynamic Metal Price Calculator

Dynamic Metal Price Calculator provides market-rate jewellery and precious metal pricing for WooCommerce. Product prices update automatically based on daily gold and silver rates, metal purity (karat), item weight, and store pricing rules (making charges, wastage, markup, GST/tax, flat shipping, and authoritative price rounding).

100% self-contained on your WordPress server with zero external network requests and zero tracking.

---

## How It Works

Instead of manually editing prices when commodity rates fluctuate, administrators update daily base rates in **Metal Prices** -> **Metal Rates**:
* **24K Gold Rate / gram**
* **Silver Rate / gram**

The pricing engine automatically computes the active price across product pages, shop archives, cart, checkout, and orders:

```text
Product Price = [ (Metal Rate × Purity % × Weight) + Base Price ]
              + Making Charges (%)
              + Wastage (%)
              + Markup (%)
              + GST / Tax (%)
              + Flat Shipping
              ↓
      Authoritative Price Rounding (Nearest 10, 50, 100, or Custom)
              ↓
      Active WooCommerce Price
```

---

## Key Features

* **Daily Metal Rates**: Fast admin inputs for 24K Gold and Silver base prices per gram.
* **Metals & Purities Manager**: Add, edit, rename, enable, disable, and delete metal options (24K, 22K, 20K, 18K, Silver, and custom alloys).
* **Safe Custom Formulas**: Create custom metals (e.g. Platinum, 14K, 10K) using mathematical expressions (e.g. `gold_rate * 1.25`) or fixed rates (e.g. `500`) with zero arbitrary code execution.
* **Active vs. Show in Dropdown**: Decouple catalog enablement from dropdown visibility so you can phase out metals without breaking existing products.
* **Product-Level Whitelists**: Restrict allowed metal choices on individual products (e.g. Silver only on silver jewellery).
* **Authoritative Price Rounding**: Clean up odd price totals into shopper-friendly figures (None, Nearest 10, 50, 100, or Custom increment) that persist into cart and checkout.
* **Variable Product Support**: Assign independent metal types, weights, and charges to individual variations.
* **Automatic Sale Pricing**: Automatically shows calculated dynamic prices as sale prices with strikethrough regular prices when regular price is set.
* **Frontend Breakdown**: Optional single product page breakdown display (metal value, weight, making charge, wastage, markup, GST, shipping, total).
* **HPOS Ready**: Declared compatibility with High-Performance Order Storage and Cart/Checkout Blocks.

---

## Quick Start Guide

1. **Install & Activate**: Upload the plugin zip via **Plugins** -> **Add New** and activate.
2. **Set Rates**: Open **Metal Prices** -> **Metal Rates**, enter current Gold and Silver rates per gram, and click **Save Rates**.
3. **Configure Rounding (Optional)**: In **Price Rounding**, choose a mode (e.g. *Nearest 50*).
4. **Edit a Product**: Open a WooCommerce product. In **Product Data** -> **General**, locate **Dynamic Metal Pricing**.
5. **Enter Metal Details**: Select a **Metal Type** (e.g. `Gold 22k`) and enter the **Weight (g)** (e.g. `3.5`).
6. **Publish & Verify**: Save the product and check the price on the product page and in checkout.

---

## Metals & Purities Manager

Access under **Metal Prices** -> **Metals & Purities Manager**:

* **Display Label**: Public name shown in product selectors and customer breakdowns.
* **Internal ID**: Unique slug saved in product metadata (e.g. `22k`, `silver`).
* **Group**: Categorized into `Gold` (scales with gold rate), `Silver` (scales with silver rate), or `Custom` (formula-based or fixed rate).
* **Purity % / Formula**: Percentage of base rate (e.g. `91.6%` for 22K) or custom formula / fixed number.
* **Active**: Storewide enable/disable toggle.
* **Show in Dropdown**: Controls whether the metal appears in product edit dropdowns.
* **Safe Deletion**: Unused metals can be permanently deleted; in-use metals show an **In Use** badge and cannot be deleted until unassigned.

### Active vs Show in Dropdown
* **Active = ON, Show in Dropdown = OFF**: Phased-out metals won't appear on new products, while existing products continue calculating correctly.
* **Active = OFF**: Disables the metal completely across the store.

---

## Product-Level Metal Selection

Use the **Allowed Metals for this Product** checklist on the product edit screen:
* **All Unchecked (Default)**: Allows all globally active metals.
* **Checked Selection**: Restricts the product and its variations to only the checked metals.
* **Data Safety**: If an assigned metal is ever hidden or removed, the plugin preserves your saved selection with a `(Saved)` or `(Inactive)` badge so data is never lost.

---

## Price Rounding Modes

* **None**: Exact calculated price.
* **Nearest 10**: e.g. ₹2,254 → ₹2,250; ₹2,256 → ₹2,260.
* **Nearest 50**: e.g. ₹2,201 → ₹2,200; ₹2,249 → ₹2,250; ₹2,276 → ₹2,300.
* **Nearest 100**: e.g. ₹2,249 → ₹2,200; ₹2,250 → ₹2,300.
* **Custom Increment**: Any numeric increment (e.g. `25` or `5`).

Price rounding is authoritative: the rounded price carries into the cart, checkout, payment gateway, and customer invoices.

---

## Variable Products

1. In a Variable Product, open the **Variations** tab.
2. Expand any variation and check **"Use Dynamic Metal Price Calculator for this variation"**.
3. Select the metal type and enter the variation's weight in grams.
4. Leave charge fields blank to inherit parent or global settings.
5. Save variations.

---

## Custom Metals & Formulas

Add custom metals under **Add New Metal or Purity Option**:
* Set **Metal Group** to `Custom`.
* Enter a formula using variables `gold_rate` or `silver_rate` with `+`, `-`, `*`, `/`, `(`, `)` (e.g. `gold_rate * 1.25`).
* Alternatively, enter a fixed numerical price per gram (e.g. `500`) without any formula.

---

## Troubleshooting

### Metal option does not appear in product dropdown
* **Probable Cause**: The metal is inactive, hidden from dropdowns, or excluded by the product's allowed metals whitelist.
* **How to Fix**:
  1. Go to **Metal Prices** -> **Metals & Purities Manager** and verify **Active** and **Show in Dropdown** are checked.
  2. In the product editor, check the **Allowed Metals for this Product** list. Ensure your desired metal is checked.
  3. Save settings and refresh the product edit page.

### Price is not calculating on the product page
* **Probable Cause**: Weight is zero or daily metal rate is missing.
* **How to Fix**:
  1. Ensure **Weight (g)** is greater than `0`.
  2. Confirm today's rate is entered in **Metal Prices** -> **Metal Rates**.
  3. For variable products, ensure **"Use Dynamic Metal Price Calculator for this variation"** is checked.

### Existing product displays "(Saved)" or "(Inactive)" badge
* **Probable Cause**: The product was saved with a metal that has since been disabled, hidden, or excluded.
* **How to Fix**: Re-enable the metal in the manager, check it in the product whitelist, or assign an active metal.

### Rounded price looks different in cart or checkout
* **Probable Cause**: WooCommerce tax display settings or flat shipping fees added at checkout.
* **How to Fix**: Review tax display settings in **WooCommerce** -> **Settings** -> **Tax** (inclusive vs. exclusive).

---

## Frequently Asked Questions

**Q: Does it fetch live market rates automatically?**  
A: No. Store owners set manual daily rates in the admin panel. This gives merchants complete control over margins without third-party API dependencies.

**Q: Can I rename metal options?**  
A: Yes. Edit the **Display Label** in the Metals & Purities Manager and save.

**Q: Does price rounding affect the final charge at checkout?**  
A: Yes. Price rounding modifies the actual WooCommerce item price and carries into cart, checkout, payment gateway, and orders.

**Q: Can I hide charges from customers on the product page?**  
A: Yes. Under **Frontend Display Controls**, toggle individual components on or off. Hidden charges remain active in the calculation.

**Q: Does this plugin send data outside my server?**  
A: No. The plugin is 100% self-contained with zero external calls and zero tracking.
