Fix RajaOngkir address integration bugs and styling issues
This commit is contained in:
@@ -91,6 +91,11 @@ add_filter('woocommerce_checkout_fields', function ($fields) {
|
||||
$fields['billing']['billing_last_name']['type'] = 'hidden';
|
||||
$fields['billing']['billing_last_name']['default'] = 'ID';
|
||||
$fields['billing']['billing_last_name']['required'] = false;
|
||||
|
||||
// Make first_name take full width since last_name is hidden
|
||||
if (isset($fields['billing']['billing_first_name'])) {
|
||||
$fields['billing']['billing_first_name']['class'] = ['form-row-wide'];
|
||||
}
|
||||
}
|
||||
if (isset($fields['billing']['billing_country'])) {
|
||||
$fields['billing']['billing_country']['type'] = 'hidden';
|
||||
@@ -115,6 +120,11 @@ add_filter('woocommerce_checkout_fields', function ($fields) {
|
||||
$fields['shipping']['shipping_last_name']['type'] = 'hidden';
|
||||
$fields['shipping']['shipping_last_name']['default'] = 'ID';
|
||||
$fields['shipping']['shipping_last_name']['required'] = false;
|
||||
|
||||
// Make first_name take full width since last_name is hidden
|
||||
if (isset($fields['shipping']['shipping_first_name'])) {
|
||||
$fields['shipping']['shipping_first_name']['class'] = ['form-row-wide'];
|
||||
}
|
||||
}
|
||||
if (isset($fields['shipping']['shipping_country'])) {
|
||||
$fields['shipping']['shipping_country']['type'] = 'hidden';
|
||||
@@ -137,7 +147,8 @@ add_filter('woocommerce_checkout_fields', function ($fields) {
|
||||
|
||||
// Check if cart needs shipping
|
||||
$needs_shipping = true;
|
||||
if (function_exists('WC') && WC()->cart) {
|
||||
// If cart is empty, we assume it's for Address Book in My Account where we want fields visible
|
||||
if (function_exists('WC') && WC()->cart && !WC()->cart->is_empty()) {
|
||||
$needs_shipping = WC()->cart->needs_shipping();
|
||||
}
|
||||
|
||||
@@ -225,3 +236,79 @@ add_action('woonoow/shipping/before_calculate', function ($shipping, $items) {
|
||||
// Clear shipping cache to force recalculation
|
||||
WC()->session->set('shipping_for_package_0', false);
|
||||
}, 10, 2);
|
||||
|
||||
// ============================================================
|
||||
// 4. Save destination_id to Order Meta on SPA Checkout
|
||||
// ============================================================
|
||||
add_action('woocommerce_checkout_order_processed', function ($order_id, $payload) {
|
||||
// Extract and save destination_id from shipping payload
|
||||
if (!empty($payload['shipping']['destination_id'])) {
|
||||
update_post_meta($order_id, '_shipping_destination_id', sanitize_text_field($payload['shipping']['destination_id']));
|
||||
}
|
||||
|
||||
// Extract and save destination_id from billing payload
|
||||
if (!empty($payload['billing']['destination_id'])) {
|
||||
update_post_meta($order_id, '_billing_destination_id', sanitize_text_field($payload['billing']['destination_id']));
|
||||
}
|
||||
|
||||
// Fallback to custom_fields array if present
|
||||
if (!empty($payload['custom_fields']['shipping_destination_id'])) {
|
||||
update_post_meta($order_id, '_shipping_destination_id', sanitize_text_field($payload['custom_fields']['shipping_destination_id']));
|
||||
}
|
||||
if (!empty($payload['custom_fields']['billing_destination_id'])) {
|
||||
update_post_meta($order_id, '_billing_destination_id', sanitize_text_field($payload['custom_fields']['billing_destination_id']));
|
||||
}
|
||||
|
||||
// Save labels too if they exist, useful for backend viewing
|
||||
if (!empty($payload['custom_fields']['shipping_destination_id_label'])) {
|
||||
update_post_meta($order_id, '_shipping_destination_id_label', sanitize_text_field($payload['custom_fields']['shipping_destination_id_label']));
|
||||
}
|
||||
if (!empty($payload['custom_fields']['billing_destination_id_label'])) {
|
||||
update_post_meta($order_id, '_billing_destination_id_label', sanitize_text_field($payload['custom_fields']['billing_destination_id_label']));
|
||||
}
|
||||
}, 10, 2);
|
||||
|
||||
// ============================================================
|
||||
// 5. Format address display for SPA saved addresses
|
||||
// ============================================================
|
||||
add_filter('woonoow_format_address', function ($formatted, $address) {
|
||||
// If a snippet has already formatted it, skip
|
||||
if (!empty($formatted)) {
|
||||
return $formatted;
|
||||
}
|
||||
|
||||
$type = $address['type'] ?? 'billing';
|
||||
$is_billing = $type === 'billing' || $type === 'both';
|
||||
|
||||
// Look for destination_id_label
|
||||
$label = '';
|
||||
if (!empty($address['destination_id_label'])) {
|
||||
$label = $address['destination_id_label'];
|
||||
} elseif ($is_billing && !empty($address['billing_destination_id_label'])) {
|
||||
$label = $address['billing_destination_id_label'];
|
||||
} elseif (!$is_billing && !empty($address['shipping_destination_id_label'])) {
|
||||
$label = $address['shipping_destination_id_label'];
|
||||
}
|
||||
|
||||
// If we have a Rajaongkir label, construct a clean Indonesian address
|
||||
if ($label) {
|
||||
$parts = [];
|
||||
if (!empty($address['address_1'])) {
|
||||
$parts[] = $address['address_1'];
|
||||
}
|
||||
if (!empty($address['address_2'])) {
|
||||
$parts[] = $address['address_2'];
|
||||
}
|
||||
// Append the Rajaongkir province/city/subdistrict string
|
||||
$parts[] = $label;
|
||||
|
||||
if (!empty($address['postcode'])) {
|
||||
$parts[] = $address['postcode'];
|
||||
}
|
||||
|
||||
// The SPA uses whitespace-pre-wrap so newline \n works for visual separation
|
||||
return implode("\n", $parts);
|
||||
}
|
||||
|
||||
return $formatted;
|
||||
}, 10, 2);
|
||||
|
||||
Reference in New Issue
Block a user