docs: update Rajaongkir snippet and add generic shipping bridge pattern
RAJAONGKIR_INTEGRATION.md: - Hide country/state/city when Indonesia is the only allowed country - Make destination_id required for Indonesia-only stores - Force country to ID in session bridge - Added billing_destination_id fallback SHIPPING_BRIDGE_PATTERN.md: - New generic template for shipping provider integrations - Documents architecture, hooks, and field types - Provides copy-paste template for new providers - Includes checklist for new integrations
This commit is contained in:
@@ -88,8 +88,8 @@ function woonoow_rajaongkir_search_destinations($request) {
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 2. Add destination field to checkout fields (both billing and shipping)
|
||||
// Always add for Indonesia zone (no premature country check)
|
||||
// 2. Add destination field and hide redundant fields for Indonesia
|
||||
// The destination_id from Rajaongkir contains province/city/subdistrict
|
||||
// ============================================================
|
||||
add_filter('woocommerce_checkout_fields', function($fields) {
|
||||
// Check if Rajaongkir is active
|
||||
@@ -103,11 +103,47 @@ add_filter('woocommerce_checkout_fields', function($fields) {
|
||||
return $fields;
|
||||
}
|
||||
|
||||
// Check if Indonesia is the ONLY allowed country
|
||||
$indonesia_only = count($allowed) === 1 && isset($allowed['ID']);
|
||||
|
||||
// If Indonesia only, hide country/state/city fields (Rajaongkir destination has all this)
|
||||
if ($indonesia_only) {
|
||||
// Hide billing fields
|
||||
if (isset($fields['billing']['billing_country'])) {
|
||||
$fields['billing']['billing_country']['type'] = 'hidden';
|
||||
$fields['billing']['billing_country']['default'] = 'ID';
|
||||
$fields['billing']['billing_country']['required'] = false;
|
||||
}
|
||||
if (isset($fields['billing']['billing_state'])) {
|
||||
$fields['billing']['billing_state']['type'] = 'hidden';
|
||||
$fields['billing']['billing_state']['required'] = false;
|
||||
}
|
||||
if (isset($fields['billing']['billing_city'])) {
|
||||
$fields['billing']['billing_city']['type'] = 'hidden';
|
||||
$fields['billing']['billing_city']['required'] = false;
|
||||
}
|
||||
|
||||
// Hide shipping fields
|
||||
if (isset($fields['shipping']['shipping_country'])) {
|
||||
$fields['shipping']['shipping_country']['type'] = 'hidden';
|
||||
$fields['shipping']['shipping_country']['default'] = 'ID';
|
||||
$fields['shipping']['shipping_country']['required'] = false;
|
||||
}
|
||||
if (isset($fields['shipping']['shipping_state'])) {
|
||||
$fields['shipping']['shipping_state']['type'] = 'hidden';
|
||||
$fields['shipping']['shipping_state']['required'] = false;
|
||||
}
|
||||
if (isset($fields['shipping']['shipping_city'])) {
|
||||
$fields['shipping']['shipping_city']['type'] = 'hidden';
|
||||
$fields['shipping']['shipping_city']['required'] = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Destination field definition (reused for billing and shipping)
|
||||
$destination_field = [
|
||||
'type' => 'searchable_select',
|
||||
'label' => __('Destination (Province, City, Subdistrict)', 'woonoow'),
|
||||
'required' => false, // Frontend will manage this based on country
|
||||
'required' => $indonesia_only, // Required if Indonesia only
|
||||
'priority' => 85,
|
||||
'class' => ['form-row-wide'],
|
||||
'placeholder' => __('Search destination...', 'woonoow'),
|
||||
@@ -133,15 +169,27 @@ add_filter('woocommerce_checkout_fields', function($fields) {
|
||||
|
||||
// ============================================================
|
||||
// 3. Bridge WooNooW shipping data to Rajaongkir session
|
||||
// Sets destination_id in WC session for Rajaongkir to use
|
||||
// ============================================================
|
||||
add_action('woonoow/shipping/before_calculate', function($shipping, $items) {
|
||||
// Set country in WC customer
|
||||
if (!empty($shipping['country'])) {
|
||||
// Check if Rajaongkir is active
|
||||
if (!class_exists('Cekongkir_API')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// For Indonesia-only stores, always set country to ID
|
||||
$allowed = WC()->countries->get_allowed_countries();
|
||||
$indonesia_only = count($allowed) === 1 && isset($allowed['ID']);
|
||||
|
||||
if ($indonesia_only) {
|
||||
WC()->customer->set_shipping_country('ID');
|
||||
WC()->customer->set_billing_country('ID');
|
||||
} elseif (!empty($shipping['country'])) {
|
||||
WC()->customer->set_shipping_country($shipping['country']);
|
||||
WC()->customer->set_billing_country($shipping['country']);
|
||||
}
|
||||
|
||||
// Only process for Indonesia
|
||||
// Only process Rajaongkir for Indonesia
|
||||
$country = $shipping['country'] ?? WC()->customer->get_shipping_country();
|
||||
if ($country !== 'ID') {
|
||||
// Clear destination for non-Indonesia
|
||||
@@ -153,6 +201,7 @@ add_action('woonoow/shipping/before_calculate', function($shipping, $items) {
|
||||
// Get destination_id from shipping data (various possible keys)
|
||||
$destination_id = $shipping['destination_id']
|
||||
?? $shipping['shipping_destination_id']
|
||||
?? $shipping['billing_destination_id']
|
||||
?? null;
|
||||
|
||||
if (empty($destination_id)) {
|
||||
@@ -165,6 +214,7 @@ add_action('woonoow/shipping/before_calculate', function($shipping, $items) {
|
||||
// Also set label if provided
|
||||
$label = $shipping['destination_label']
|
||||
?? $shipping['shipping_destination_id_label']
|
||||
?? $shipping['billing_destination_id_label']
|
||||
?? '';
|
||||
if ($label) {
|
||||
WC()->session->set('selected_destination_label', sanitize_text_field($label));
|
||||
|
||||
Reference in New Issue
Block a user