Fix RajaOngkir address integration bugs and styling issues

This commit is contained in:
Dwindi Ramadhana
2026-06-03 22:56:02 +07:00
parent 21ece27b9b
commit fb1a6c40ef
7 changed files with 263 additions and 21 deletions

View File

@@ -538,6 +538,9 @@ class CheckoutController
header('Server-Timing: app;dur=' . round((microtime(true) - $__t0) * 1000, 1));
}
// Auto-save checkout addresses to user account
$this->auto_save_checkout_addresses($order, $payload);
// Clear WooCommerce cart after successful order placement
// This ensures the cart page won't re-populate from server session
if (function_exists('WC') && WC()->cart) {
@@ -1141,4 +1144,105 @@ class CheckoutController
}
return $results;
}
/**
* Auto-save checkout addresses to the user's address book if they are new.
*/
private function auto_save_checkout_addresses(\WC_Order $order, array $payload): void
{
$user_id = $order->get_customer_id();
if (!$user_id) {
return;
}
$addresses = get_user_meta($user_id, 'woonoow_addresses', true);
if (!is_array($addresses)) {
$addresses = [];
}
// Helper to check if address matches existing
$is_duplicate = function ($new_addr, $type) use ($addresses) {
foreach ($addresses as $addr) {
if ($addr['type'] !== $type && $addr['type'] !== 'both') {
continue;
}
// Compare essential fields
$match = true;
$check_fields = ['first_name', 'last_name', 'address_1', 'city', 'country'];
foreach ($check_fields as $f) {
$v1 = trim(strtolower((string)($addr[$f] ?? '')));
$v2 = trim(strtolower((string)($new_addr[$f] ?? '')));
if ($v1 !== $v2) {
$match = false;
break;
}
}
if ($match) return true;
}
return false;
};
// Helper to build address array
$build_address = function ($type, $data, $custom_fields, $addresses) {
$new_id = empty($addresses) ? 1 : max(array_column($addresses, 'id')) + 1;
$addr = [
'id' => $new_id,
'label' => ucfirst($type) . ' ' . $new_id,
'type' => $type,
'is_default' => empty($addresses), // default if it's the first one
];
$standard_fields = ['first_name', 'last_name', 'company', 'address_1', 'address_2', 'city', 'state', 'postcode', 'country', 'email', 'phone'];
foreach ($standard_fields as $f) {
if (isset($data[$f])) {
$addr[$f] = sanitize_text_field($data[$f]);
}
}
// Add custom fields matching prefix
if (is_array($custom_fields)) {
foreach ($custom_fields as $k => $v) {
if (strpos($k, $type . '_') === 0) {
$addr[$k] = sanitize_text_field($v);
} elseif (!isset($addr[$type . '_' . $k]) && !isset($addr[$k])) {
// Some custom fields might not have the prefix if they apply to both
// Or they are sent without prefix by frontend in payload[type]
}
}
}
// Also, payload[type] can contain custom fields directly because frontend sends them without prefix!
foreach ($data as $k => $v) {
if (!in_array($k, $standard_fields) && !in_array($k, ['ship_to_different'])) {
$addr[$type . '_' . $k] = sanitize_text_field($v);
}
}
return $addr;
};
$changed = false;
// Check billing
if (!empty($payload['billing'])) {
if (!$is_duplicate($payload['billing'], 'billing')) {
$billing_addr = $build_address('billing', $payload['billing'], $payload['custom_fields'] ?? [], $addresses);
$addresses[] = $billing_addr;
$changed = true;
}
}
// Check shipping
$ship_to_different = !empty($payload['shipping']['ship_to_different']);
if ($ship_to_different && !empty($payload['shipping'])) {
if (!$is_duplicate($payload['shipping'], 'shipping')) {
$shipping_addr = $build_address('shipping', $payload['shipping'], $payload['custom_fields'] ?? [], $addresses);
$addresses[] = $shipping_addr;
$changed = true;
}
}
if ($changed) {
update_user_meta($user_id, 'woonoow_addresses', array_values($addresses));
}
}
}