=== RRevX Shipping — Distance-based delivery fee ===
Contributors: rrevx
Tags: shipping, delivery, distance, thailand, woocommerce
Requires at least: 6.0
Tested up to: 7.1
Requires PHP: 7.4
Stable tag: 0.2.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

คิดค่าส่งตามระยะทางถนนจริงจากร้านถึงลูกค้า (ไทย + ลาว) · Charge delivery by real road distance from your shop to the customer (Thailand + Laos).

== Description ==

**ภาษาไทย**

ร้านที่ส่งของเอง (ไรเดอร์ของร้าน, แมสเซนเจอร์, ส่งด่วนในเมือง) คิดค่าส่งตามระยะทางถนนจริงได้อัตโนมัติในหน้าชำระเงินของ WooCommerce

* **ลูกค้าปักหมุดจุดส่งได้** ตอนชำระเงิน: "ส่งที่ตำแหน่งปัจจุบันของฉัน" (ตำแหน่งของมือถือ) หรือปักหมุดบนแผนที่ (ค้นชื่อคอนโด/หมู่บ้าน/อาคารได้) → ค่าส่งคิดจากหมุด ตรงจุด · ไม่ปักหมุด = ค้นจากที่อยู่ (โดยประมาณ) · ร้านเห็นหมุด + ปุ่มนำทางในออเดอร์และอีเมลออเดอร์ใหม่
* ตั้งกฎได้ใน Shipping Zones: ค่าส่งเริ่มต้น + กม. ที่รวมแล้ว, บาท/กม. ถัดไป, ค่าส่งขั้นต่ำ/สูงสุด, ส่งฟรีเมื่อยอดถึง, ระยะส่งไกลสุด, ปัดเศษ 1/5/10 บาท
* แสดงระยะทางในชื่อวิธีส่ง เช่น "ส่งด่วนในเมือง (7.4 กม.)"
* ระยะทางคำนวณจากเส้นทางถนนจริง (ไม่ใช่เส้นตรง) — ไม่ข้ามเรือ เกาะที่ไม่มีถนนเชื่อมใช้ "ค่าส่งสำรอง"
* คีย์ API อยู่บนเซิร์ฟเวอร์ของคุณเท่านั้น ไม่ส่งไปเบราว์เซอร์ลูกค้า
* จำผลไว้ (พิกัดที่อยู่ 30 วัน, ระยะทาง 7 วัน) — ที่อยู่เดิมไม่เรียก API ซ้ำ
* API ล่ม / คีย์มีปัญหา / หาที่อยู่ไม่เจอ → ใช้ค่าส่งสำรองที่ตั้งไว้ (หรือซ่อนวิธีนี้)
* รองรับ HPOS และหน้าตะกร้า/ชำระเงินแบบบล็อก

ใช้ RRevX Maps API (api.rrevx.com) — สมัครคีย์ฟรีที่ https://rrevx.com/maps/console ฟรี 10,000 ค้นหาสถานที่ + 10,000 เส้นทางต่อเดือน (ที่อยู่ใหม่ 1 ที่ ≈ ค้นหา 1–3 ครั้ง + เส้นทาง 1 ครั้ง)

**English**

For shops that deliver themselves (own riders, messengers, same-day in town): the delivery fee is worked out from the real road distance between your shop and the customer's address, right in the WooCommerce cart and checkout.

* **Customers can pin the delivery point** at checkout: "deliver to where I am now" (the phone's location) or a pin on the map (with a place search). The fee then uses the pin. Without a pin the address is searched (approximate). The shop sees the pin with a navigation link on the order and in the new-order e-mail.
* Rules per shipping zone: starting fee + km included, price per km after that, lowest/highest fee, free delivery over an order amount, farthest distance, round up to 1/5/10 baht.
* Optional distance in the rate name, e.g. "Express delivery in town (7.4 km)".
* Road distance, not a straight line. Places with no road link (islands) use the fallback fee.
* The secret API key stays on your server. The checkout map uses a separate web key (rx_pub_…) that only works from your domain.
* Results are cached (address coordinates 30 days, distances 7 days).
* API down, key problem or address not found → your fallback fee (or the option is hidden).
* Works with HPOS and the Cart/Checkout blocks.

== External services ==

This plugin connects to the **RRevX Maps API** (https://api.rrevx.com), run by HASHBX Global Co., Ltd. (Thailand), to turn a delivery address into a map point and to measure the road distance from the shop.

* When (from your server): when WooCommerce works out shipping rates for a cart with a Thai or Lao address or a delivery pin that is not in the cache yet, and when a shop manager presses "Test connection" or "Search" on the settings screen.
* What your server sends: the shop's coordinates and the customer's delivery pin (if the customer set one), or else parts of the delivery address (building / village / road names and the district — never the customer's name, e-mail, phone or house number) and the country; your secret API key, and a User-Agent with the plugin version and your site URL.
* When (from the customer's browser): only if the shop set a web key and the customer presses "Pin on the map": the browser loads the map (style, map tiles, fonts) from api.rrevx.com and sends what the customer types into the map's place search, with the map's position. "Deliver to where I am now" uses the browser's location service and sends nothing to api.rrevx.com from the browser.
* The pin is stored on the order (like the address) and is included in WooCommerce's personal data export and erasure.
* Terms: https://rrevx.com/maps/terms · Privacy: https://rrevx.com/privacy · Status: https://rrevx.com/maps/status

The settings screen shows the shop location on an embedded map from **OpenStreetMap** (https://www.openstreetmap.org, tiles loaded by the shop manager's browser only, nothing about customers is sent). OSM terms: https://wiki.osmfoundation.org/wiki/Terms_of_Use · Privacy: https://osmfoundation.org/wiki/Privacy_Policy

Data credits: routing © OpenStreetMap contributors (ODbL) · Places © Overture Maps Foundation (Meta, Microsoft, Foursquare, AllThePlaces). A short credit line is shown under the delivery option.

This plugin includes **MapLibre GL JS 4.7.1** (BSD-3-Clause, https://github.com/maplibre/maplibre-gl-js) in assets/vendor/, loaded only when a customer opens the map.

== Installation ==

1. ติดตั้งและเปิดใช้ปลั๊กอิน (ต้องมี WooCommerce 8.0 ขึ้นไป) · Install and activate (WooCommerce 8.0+).
2. สมัครคีย์ฟรีที่ https://rrevx.com/maps/console → สร้าง **คีย์ลับ (rx_sec_…)** · Create a free **secret key (rx_sec_…)**.
3. WooCommerce → Settings → Shipping → **RRevX distance**: ใส่คีย์ → กด "ทดสอบการเชื่อมต่อ" → ค้นหาร้าน (หรือใส่ lat, lng) → บันทึก · Paste the key, test it, find your shop, save.
4. (แนะนำ) แผนที่ตอนชำระเงิน: สร้าง **คีย์เว็บ (rx_pub_…)** ที่ใส่โดเมนร้านไว้ → วางในช่อง "คีย์เว็บสำหรับแผนที่ตอนชำระเงิน" → กด "ทดสอบคีย์เว็บ" · (Recommended) Create a **web key (rx_pub_…)** with your shop's domain for the checkout map, paste it and test it.
5. WooCommerce → Settings → Shipping → เลือกโซน (เช่น กรุงเทพฯ) → Add shipping method → **ส่งตามระยะทาง (RRevX)** → ตั้งค่าส่ง · Add the method to a zone and set the fees.

== Frequently Asked Questions ==

= ค่าใช้จ่าย API ต่อออเดอร์เท่าไร? / What does the API cost per order? =

มีหมุด = เส้นทาง 1 ครั้ง (+ แผนที่ 1 ครั้งถ้าลูกค้าเปิดแผนที่ + ค้นหาในแผนที่ 1 ครั้งถ้าค้น) · ไม่มีหมุด = ค้นหาสถานที่ 1–3 ครั้ง + เส้นทาง 1 ครั้ง · ที่อยู่/หมุดเดิมใช้ cache ไม่เสียเพิ่ม · ฟรีเดือนละ 10,000 + 10,000 พอสำหรับที่อยู่ใหม่ราว 3,000–10,000 ที่/เดือน · A new address ≈ 1–3 place searches + 1 route; repeat addresses come from the cache. The free monthly allowance covers roughly 3,000–10,000 new addresses.

= ลูกค้าปักหมุดอย่างไร? / How do customers pin the delivery point? =

ใต้ฟอร์มที่อยู่มีกล่อง "จุดส่งของ": ปุ่ม "ส่งที่ตำแหน่งปัจจุบันของฉัน" (ใช้ตำแหน่งของมือถือ ถ้าไม่แม่นเกิน 200 ม. จะเปิดแผนที่ให้เลื่อนแก้) และ "ปักหมุดบนแผนที่" (ต้องตั้งคีย์เว็บ) · ค่าส่งคิดจากหมุดด้วยเส้นทาง 1 ครั้ง ไม่ค้นที่อยู่ · แผนที่โหลดเฉพาะตอนลูกค้ากดเปิด (นับ 1 ครั้งเปิดแผนที่) · A "Delivery point" box under the address: "Deliver to where I am now" (phone location; worse than 200 m opens the map to adjust) and "Pin on the map" (needs the web key). The fee then needs one route call and no address search. The map loads only when opened (1 map load).

= หาที่อยู่อย่างไร? / How is the address found? =

ระบบค้นชื่อสถานที่ในที่อยู่ (คอนโด หมู่บ้าน อาคาร ซอย ถนน) ใกล้ร้านก่อน และต้องอยู่จังหวัดเดียวกับที่ลูกค้าเลือก ถ้าไม่เจอใช้จุดของเขต/อำเภอ (ระยะโดยประมาณ) · The named parts of the address (condo, village, building, soi, road) are searched near the shop and must be in the province the customer chose; otherwise the district is used (approximate).

= ลูกค้าอยู่นอกไทย/ลาว? / Outside Thailand and Laos? =

วิธีส่งนี้ไม่แสดง · The method is not offered.

== Changelog ==

= 0.2.0 =
* Delivery pin at checkout (classic + block checkout): the phone's location, or a pin on the map (MapLibre + web key, with place search). The fee uses the pin; the address search is the fallback.
* The pin is kept on the order: order screen and new-order e-mail show it with map + navigation links. Included in personal data export / erasure.
* Settings: "Delivery pin at checkout" and "Web key for the checkout map" (with a domain test).

= 0.1.0 =
* First version.
