diff --git a/RAJAONGKIR_INTEGRATION.md b/RAJAONGKIR_INTEGRATION.md index 4c6e300..312d199 100644 --- a/RAJAONGKIR_INTEGRATION.md +++ b/RAJAONGKIR_INTEGRATION.md @@ -17,24 +17,6 @@ Before using this integration: --- -## How It Works - -``` -┌─────────────────┐ ┌───────────────────┐ ┌──────────────────┐ -│ Customer types │ → │ /rajaongkir/ │ → │ SearchableSelect │ -│ "Bandung" │ │ destinations API │ │ shows results │ -└─────────────────┘ └───────────────────┘ └──────────────────┘ - │ - ▼ -┌─────────────────┐ ┌───────────────────┐ ┌──────────────────┐ -│ Rajaongkir uses │ ← │ Hook sets WC │ ← │ Customer selects │ -│ session to get │ │ session data │ │ destination │ -│ shipping rates │ │ │ │ │ -└─────────────────┘ └───────────────────┘ └──────────────────┘ -``` - ---- - ## Code Snippet Add this to **Code Snippets** or **WPCodebox**: @@ -78,10 +60,17 @@ function woonoow_rajaongkir_search_destinations($request) { } // Use Rajaongkir's API class for the search + // NOTE: Method is search_destination_api() not search_destination() $api = Cekongkir_API::get_instance(); - $results = $api->search_destination(['keyword' => $search]); + $results = $api->search_destination_api($search); if (is_wp_error($results)) { + error_log('Rajaongkir search error: ' . $results->get_error_message()); + return []; + } + + if (!is_array($results)) { + error_log('Rajaongkir search returned non-array: ' . print_r($results, true)); return []; } @@ -89,8 +78,8 @@ function woonoow_rajaongkir_search_destinations($request) { $formatted = []; foreach ($results as $r) { $formatted[] = [ - 'value' => (string) $r['id'], - 'label' => $r['label'], // "Province, City, District" + 'value' => (string) ($r['id'] ?? ''), + 'label' => $r['label'] ?? $r['text'] ?? '', ]; } @@ -99,7 +88,8 @@ function woonoow_rajaongkir_search_destinations($request) { } // ============================================================ -// 2. Add destination field to checkout fields (SPA-aware) +// 2. Add destination field to checkout fields +// Always add for Indonesia zone (no premature country check) // ============================================================ add_filter('woocommerce_checkout_fields', function($fields) { // Check if Rajaongkir is active @@ -107,42 +97,29 @@ add_filter('woocommerce_checkout_fields', function($fields) { return $fields; } - // Get country from various sources - $country = ''; - - // From WC customer object - if (WC()->customer) { - $country = WC()->customer->get_shipping_country(); - } - - // From REST API request body (for SPA context) - if (defined('REST_REQUEST') && REST_REQUEST) { - $json = file_get_contents('php://input'); - if ($json) { - $data = json_decode($json, true); - if (isset($data['shipping']['country'])) { - $country = $data['shipping']['country']; - } - } - } - - // Only add field for Indonesia or when country not yet determined - if ($country && $country !== 'ID') { + // Check if store sells to Indonesia (check allowed countries) + $allowed = WC()->countries->get_allowed_countries(); + if (!isset($allowed['ID'])) { return $fields; } // Add searchable destination field + // The frontend will show/hide based on selected country $fields['shipping']['shipping_destination_id'] = [ 'type' => 'searchable_select', 'label' => __('Destination (Province, City, Subdistrict)', 'woonoow'), - 'required' => true, - 'priority' => 85, // After country/state, before postcode + 'required' => false, // Not required initially, SPA will manage this + 'priority' => 85, 'class' => ['form-row-wide'], 'placeholder' => __('Search destination...', 'woonoow'), // WooNooW-specific: API endpoint configuration 'search_endpoint' => '/woonoow/v1/rajaongkir/destinations', 'search_param' => 'search', 'min_chars' => 2, + // Custom attribute to indicate this is for Indonesia only + 'custom_attributes' => [ + 'data-show-for-country' => 'ID', + ], ]; return $fields; @@ -173,14 +150,13 @@ add_action('woonoow/shipping/before_calculate', function($shipping, $items) { ?? null; if (empty($destination_id)) { - // No destination selected yet return; } // Set session for Rajaongkir WC()->session->set('selected_destination_id', intval($destination_id)); - // Also set label if provided (for display purposes) + // Also set label if provided $label = $shipping['destination_label'] ?? $shipping['shipping_destination_id_label'] ?? ''; @@ -196,33 +172,20 @@ add_action('woonoow/shipping/before_calculate', function($shipping, $items) { --- -## Field Configuration Options - -The `searchable_select` field type supports: - -| Option | Description | -|--------|-------------| -| `type` | `'searchable_select'` for API-backed search | -| `search_endpoint` | REST API endpoint path | -| `search_param` | Query parameter name (default: 'search') | -| `min_chars` | Minimum characters before search (default: 2) | -| `placeholder` | Input placeholder text | - ---- - ## Testing ### 1. Test the API Endpoint -```bash -curl "https://yoursite.com/wp-json/woonoow/v1/rajaongkir/destinations?search=bandung" +After adding the snippet: +``` +GET /wp-json/woonoow/v1/rajaongkir/destinations?search=bandung ``` Should return: ```json [ {"value": "1234", "label": "Jawa Barat, Bandung, Kota"}, - {"value": "1235", "label": "Jawa Barat, Bandung, Kabupaten"} + ... ] ``` @@ -234,49 +197,49 @@ Should return: 4. "Destination" field should appear 5. Type 2+ characters to search 6. Select a destination -7. Click "Calculate Shipping" or proceed -8. Rajaongkir rates should appear +7. Rajaongkir rates should appear --- ## Troubleshooting -### Destination field not appearing? +### API returns empty? -Check if: -- Country is set to Indonesia -- Rajaongkir plugin is active -- Snippet has no syntax errors - -Debug in PHP: +Check `debug.log` for errors: ```php -add_action('woocommerce_checkout_init', function() { - $fields = WC()->checkout()->get_checkout_fields(); - error_log('Shipping fields: ' . print_r(array_keys($fields['shipping'] ?? []), true)); -}); +// Added logging in the search function +error_log('Rajaongkir search error: ...'); ``` -### Search returns empty? +Common issues: +- Invalid Rajaongkir API key +- Rajaongkir plugin not active +- API quota exceeded -```php -// Add to the search endpoint -error_log('Rajaongkir search: ' . $search); -error_log('Results: ' . print_r($results, true)); -``` +### Field not appearing? -### Rajaongkir rates not appearing? +1. Ensure snippet is active +2. Check if store sells to Indonesia +3. Check browser console for JS errors + +### Rajaongkir rates not showing? 1. Check session is set: ```php add_action('woonoow/shipping/before_calculate', function($shipping) { - error_log('Destination ID in shipping data: ' . print_r($shipping, true)); - error_log('Session destination: ' . WC()->session->get('selected_destination_id')); + error_log('Shipping data: ' . print_r($shipping, true)); }, 5); ``` -2. Check Rajaongkir debug: -- Enable debug in Rajaongkir settings -- Check `debug.log` for Rajaongkir messages +2. Check Rajaongkir is enabled in shipping zone + +--- + +## Known Limitations + +1. **Field visibility**: Currently field always shows for checkout. Future improvement: hide in React when country ≠ ID. + +2. **Session timing**: Must select destination before calculating shipping. ---