feat: migrate shipping to form-level and integrate flags.json as single source of truth

Shipping Migration:
- Move shipping configuration from product-level to form-level
- Add form shipping tab in form settings (no_shipping, flat_rate, free_shipping)
- Update FlatRate to register at form level instead of product level
- Update checkout logic to read from form settings
- Support percentage-based flat rate calculation
- Simplify shipping method IDs (flat_rate, free_shipping)

Currency Flags Integration:
- Add formipay_get_all_currency_flags() to read from admin/assets/json/flags.json
- Remove hardcoded CURRENCY_FLAGS emoji map from VariationField.js
- Create CurrencyFlag component to render base64 flag images
- Localize currency_flags to window.formipayProductDetails
- Update shipping info display in admin order details

Benefits:
- Form-level shipping prevents multiplying shipping costs per product
- Single source of truth for currency flags (flags.json)
- Better support for future cart system
- Consistent with e-commerce standards
This commit is contained in:
dwindown
2026-04-23 08:12:40 +07:00
parent 0094a3571c
commit 008188b790
13 changed files with 2819 additions and 797 deletions

View File

@@ -64,6 +64,28 @@ jQuery(function($){
$('#order-total').html(res.total_formatted);
$('#order_status').val(res.status);
// Populate shipping info if available
var shippingInfo = [];
if(res.form_data){
$.each(res.form_data, function(key, data){
if(data.name === 'shipping_country' || data.name === 'shipping_method'){
shippingInfo.push(data);
}
});
}
if(shippingInfo.length > 0){
var source = $("#shipping-info-template").html();
var template = Handlebars.compile(source);
var context = {
datas: shippingInfo
};
var html = template(context);
$("#shipping-info-list").html(html);
} else {
$("#shipping-info-list").addClass('d-none');
$('#no-shipping-info').removeClass('d-none');
}
var source = $("#form-data-item-template").html();
var template = Handlebars.compile(source);
var context = {

View File

@@ -122,15 +122,21 @@ function get_global_currency_array() {
if(false === $ifSingleCurrency){
// $currency_sort = [];
$default_sort_key = null;
// Extract currency code from default_currency for comparison (handles case where default has symbol but multicurrencies don't)
$default_currency_code = explode(':::', $default_currency)[0];
foreach($global_currencies as $key => $currency){
$currency_value = $currency['currency'];
if($currency_value === $default_currency){
// Compare by currency code only (before first :::)
$currency_code = explode(':::', $currency_value)[0];
if($currency_code === $default_currency_code){
$default_sort_key = $key;
}
}
$currency_sort = [$default_sort_key => $global_currencies[$default_sort_key]];
unset($global_currencies[$default_sort_key]);
$global_currencies = $currency_sort + $global_currencies;
// Convert associative array to indexed array for JavaScript
$global_currencies = array_values($global_currencies);
}else{
if(false === boolval($multicurrency)){
$global_currencies = [
@@ -176,6 +182,31 @@ function formipay_get_flag_by_currency($currency) {
}
/**
* Get all currency flags from flags.json
* Returns an array mapping currency codes to base64 flag images
* This is the single source of truth for currency flags - never duplicate this data
*
* @return array Array of currency code => flag image mapping
*/
function formipay_get_all_currency_flags() {
static $currency_flags = null;
if ($currency_flags !== null) {
return $currency_flags;
}
$json = file_get_contents(FORMIPAY_PATH . 'admin/assets/json/flags.json');
$flags = json_decode($json, true);
$currency_flags = [];
foreach ($flags as $item) {
$currency_flags[$item['code']] = $item['flag'];
}
return $currency_flags;
}
function formipay_price_format($num = 0, $post_id = 0){
$decimal_digits = 2;
@@ -361,7 +392,15 @@ function formipay_get_order($order_id) {
case 'payment_gateway':
$label = esc_html__( 'Payment Gateway', 'formipay' );
break;
case 'shipping_country':
$label = esc_html__( 'Shipping Country', 'formipay' );
break;
case 'shipping_method':
$label = esc_html__( 'Shipping Method', 'formipay' );
break;
default:
if(!empty($all_fields[$name.'_config'])){
$label = $all_fields[$name.'_config']['label'];

View File

@@ -55,6 +55,26 @@
</div>
</div>
</div>
<div class="order-detail-card shipping-info-card">
<div class="card-title mt-3 mb-0"><?php echo esc_html__( 'Shipping Information', 'formipay' ); ?></div>
<div class="card mt-1 border-0 rounded-4 shadow-sm">
<div class="card-body p-0 placeholder-glow">
<ul class="list-group list-group-flush" id="shipping-info-list">
<li class="list-group-item">
<b><span class="placeholder col-3"></span></b>
<p class="mb-0"><span class="placeholder col-8"></span></p>
</li>
<li class="list-group-item">
<b><span class="placeholder col-3"></span></b>
<p class="mb-0"><span class="placeholder col-8"></span></p>
</li>
</ul>
<div class="d-none" id="no-shipping-info">
<p class="text-center text-muted my-3"><?php echo esc_html__( 'No shipping information available', 'formipay' ); ?></p>
</div>
</div>
</div>
</div>
<div class="order-detail-card form-data-card">
<div class="card-title mt-3 w-100 mb-0 d-flex justify-content-between align-items-center">
<?php echo esc_html__( 'Form Data', 'formipay' ); ?>
@@ -273,4 +293,12 @@
<b><?php echo esc_html__('Access Password', 'formipay'); ?></b>
<p class="mb-0">******</p>
</li>
</script>
<script id="shipping-info-template" type="text/x-handlebars-template">
{{#each datas as |data|}}
<li class="list-group-item px-0">
<b class="field-name">{{data.label}}</b>
<p class="field-value mt-1 mb-0">{{data.value}}</p>
</li>
{{/each}}
</script>