Skip to content

Commit

Permalink
2.3.0
Browse files Browse the repository at this point in the history
- Added preselect for city and district dropdown on load
- Added sanitazion to user input
- Added optional class to checkout form to check whether to hide the original city field or not
  • Loading branch information
hrsetyono committed May 20, 2022
1 parent ffae673 commit d995619
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 91 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# WooCommerce Indonesia Shipping v2

> This plugin requires PRO License from RajaOngkir.com. We are not affiliated with them in any way.
![](https://raw.github.com/hrsetyono/cdn/master/woocommerce-indo-shipping/ongkir-banner.jpg)

Ultimate *Ongkos Kirim* Plugin for major Indonesian Shipping courier.

This plugin requires PRO License purchase from RajaOngkir.com. We are not affiliated with RajaOngkir in any way.

This plugin is free and provided as is. We are not responsible for any damage caused by bugs. If you found a bug, please submit it [here](https://github.com/hrsetyono/woocommerce-indo-shipping/issues).
This plugin is free and provided as is. If you found a bug, please submit it [here](https://github.com/hrsetyono/woocommerce-indo-shipping/issues).

**Supported Couriers:**

Expand Down
2 changes: 1 addition & 1 deletion dist/ongkir-public.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/ongkir-public.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 24 additions & 34 deletions includes/ongkir-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function get_cities_api($params) {
*/
function get_districts_api($params) {
$prov_id = Ongkir_Data::get_province_id($params['prov_code']);
$cities = Ongkir_Data::get_cities($prov_id, true);
$cities = Ongkir_Data::get_cities($prov_id);
$city = $cities[$params['city_id']] ?? null;

// abort if city not found
Expand All @@ -91,43 +91,32 @@ function get_districts_api($params) {
* @return array
*/
function get_fields_api($params) {
$city_id = 0;
$field_value = '';

// Convert District ID to City ID
if ($params['district_id'] != '0') {
$prov_id = Ongkir_Data::get_province_id($params['prov_code']);
$cities = Ongkir_Data::get_cities($prov_id, true);
$cities = Ongkir_Data::get_cities($prov_id);

foreach ($cities as $city_id => $c) {
foreach ($c['districts'] as $district_id => $name) {
foreach ($cities as $id => $c) {
foreach ($c['districts'] as $dist_id => $dist_name) {
// find district that has the same ID as the one passed on
if ($params['district_id'] == $district_id) {
$params['city_id'] = $city_id;
if ($params['district_id'] == $dist_id) {
$city_id = $id;
$field_value = $c['city_name'] . ", {$dist_name} [{$dist_id}]";
break;
}
}

if ($city_id > 0) {
break;
}
}
} else {
$params['city_id'] = '0';
}

$cities_field = $this->_get_cities_field($params['type'], $params['prov_code']);
$districts_field = $this->_get_districts_field($params['type'], $params['prov_code'], $params['city_id']);

// add selected attribute
if (isset($params['city_id'])) {
$cities_field = preg_replace(
'/(_city_field[\s\S]+)(\"' . $params['city_id'] . '\"\s)(\>)([\s\S]+\/p>)/Ui',
'$1$2selected$3$4',
$cities_field
);
}

if ($params['district_id'] != '0') {
$districts_field = preg_replace(
'/(_district_field[\s\S]+)(' . $params['district_id'] . '\]\"\s)(\>)([\s\S]+\/p>)/Ui',
'$1$2selected$3$4',
$districts_field
);
}
$cities_field = $this->_get_cities_field($params['type'], $params['prov_code'], $city_id);
$districts_field = $this->_get_districts_field($params['type'], $params['prov_code'], $city_id, $field_value);

return $cities_field . $districts_field;
}
Expand All @@ -139,18 +128,18 @@ function get_fields_api($params) {
*
* @return array - Currently only has one item: 'field' which contains the HTML form field.
*/
private function _get_cities_field($type, $prov_code = '0') {
private function _get_cities_field($type, $prov_code = '0', $city_id = 0) {
$field = '';

// if code is 0, show empty placeholder dropdown
if ($prov_code == '0') {
$field = woocommerce_form_field( "_{$type}_city", [
$field = woocommerce_form_field("_{$type}_city", [
'type' => 'select',
'label' => __('City', 'woocommerce'),
'options' => [0 => __('Pilih Provinsi terlebih dahulu...')],
'return' => true,
'required' => true,
] );
]);
}
// else
else {
Expand All @@ -162,7 +151,7 @@ private function _get_cities_field($type, $prov_code = '0') {
'options' => $cities,
'return' => true,
'required' => true,
] );
], $city_id);
}

return $field;
Expand All @@ -176,12 +165,13 @@ private function _get_cities_field($type, $prov_code = '0') {
private function _get_districts_field(
$type,
$prov_code,
$city_id = '0'
$city_id = 0,
$field_value = ''
) {
$field = '';

// If city ID is empty, show placeholder
if ($city_id == '0') {
if ($city_id === 0) {
$field = woocommerce_form_field("_{$type}_district", [
'type' => 'select',
'label' => __('Kecamatan'),
Expand All @@ -203,7 +193,7 @@ private function _get_districts_field(
'options' => $districts,
'return' => true,
'required' => true
]);
], $field_value);
}

return $field;
Expand Down
2 changes: 1 addition & 1 deletion includes/ongkir-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static function get_cities($prov_id, $raw = false) {
}

// if exists, scan for duplicate city name
if($data) {
if ($data) {
$data = self::_prefix_dupe_city_name($prov_id, $data);
return $data;
} else {
Expand Down
11 changes: 1 addition & 10 deletions includes/rajaongkir.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
class RajaOngkir {
private $api_key;
private $base_url;
private $base_url = 'https://pro.rajaongkir.com/api';

function __construct($key = null) {
// set API key
Expand All @@ -18,15 +18,6 @@ function __construct($key = null) {
$cached_license = get_transient('wcis_license');
$this->api_key = $cached_license['key'];
}

// set base URL
$urls = [
'starter' => 'https://api.rajaongkir.com/starter',
'pro' => 'https://pro.rajaongkir.com/api',
];

$license_type = 'pro';
$this->base_url = $urls[$license_type];
}


Expand Down
7 changes: 5 additions & 2 deletions public/assets/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
* api.get(url).then((result) => { .. });
* api.post(url, data).then((result) => { ... });
*/
const baseURL = ongkirLocalize.ONGKIR_API;
const { baseURL } = ongkirLocalize;

const api = {
get(endpoint) {
return window.fetch(`${baseURL}${endpoint}`, {
method: 'GET',
headers: { Accept: 'application/json' },
headers: {
Accept: 'application/json',
},
})
.then(this.handleError)
.then(this.handleContentType)
Expand Down
50 changes: 18 additions & 32 deletions public/assets/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import api from './api';
const $ = jQuery;

const checkoutFields = {
init() {
// fix the weird WooCommerce interaction where it initially trigger 'change' on State field
this.isFirstRun = true;
isFirstRun: true, // fix the weird WooCommerce interaction where it initially trigger 'change' on State field

init() {
const $body = document.querySelector('body');

// abort if not in Checkout page
Expand Down Expand Up @@ -45,44 +44,24 @@ const checkoutFields = {
// get custom fields and append it after the City field
const result = await api.get(`/fields/${type}/${provCode}/${districtID}`);
$($wrapper).after(result);
});

// hide the custom field if country not ID
$('#billing_country, #shipping_country').trigger('change');
this.toggleCityField({
currentTarget: document.querySelector('#billing_country, #shipping_country'),
});
},

/**
* Show or Hide the original City field depending on the Country selected
*/
toggleCityField(e) {
const $wrapper = e.currentTarget.closest('.woocommerce-billing-fields, .woocommerce-shipping-fields');
const $ogCityField = $wrapper.querySelector('#billing_city_field, #shipping_city_field');
const $form = e.currentTarget.closest('form');

// the custom dropdown
const $citiesField = $wrapper.querySelector('#_billing_city_field, #_shipping_city_field');
const $districtsField = $wrapper.querySelector('#_billing_district_field, #_shipping_district_field');

// If country is ID, hide the original City field
// If country is ID, add a class to hide the original city field
if (e.currentTarget.value === 'ID') {
$ogCityField.style.display = 'none';

if ($citiesField) {
$citiesField.style.display = 'block';
}

if ($districtsField) {
$districtsField.style.display = 'block';
}
} else { // Else, hide the dropdown and show the original field
$ogCityField.style.display = 'block';

if ($citiesField) {
$citiesField.style.display = 'none';
}

if ($districtsField) {
$districtsField.style.display = 'none';
}
$form.classList.add('has-ongkir-dropdown');
} else {
$form.classList.remove('has-ongkir-dropdown');
}
},

Expand All @@ -95,9 +74,10 @@ const checkoutFields = {
const provCode = $(e.currentTarget).val() || '0';
const $wrapper = $(e.currentTarget).closest('.woocommerce-billing-fields, .woocommerce-shipping-fields');

const $ogCityField = $wrapper.find('#billing_city_field, #shipping_city_field');

// if not first run, empty out the city field
if (!this.isFirstRun) {
const $ogCityField = $wrapper.find('#billing_city_field, #shipping_city_field');
$ogCityField.find('input').val('');
}

Expand All @@ -111,12 +91,15 @@ const checkoutFields = {

const result = await api.get(`/cities/${provCode}`);

// Create the <option>
let options = '';

Object.keys(result).forEach((id) => {
options += `<option value="${id}">${result[id]}</option>`;
});

$citiesSelect.html(options);
$citiesSelect.trigger('change');
this.isFirstRun = false;
},

Expand All @@ -130,6 +113,9 @@ const checkoutFields = {
const $wrapper = $(e.currentTarget).closest('.woocommerce-billing-fields, .woocommerce-shipping-fields');
const provCode = $wrapper.find('#billing_state, #shipping_state').val();

// Abort if province not selected
if (!provCode) { return; }

// add 'Loading' message to district field
const $districtsSelect = $wrapper.find('#_billing_district_field select, #_shipping_district_field select');
$districtsSelect.html('<option>Loading...</option>');
Expand Down
14 changes: 13 additions & 1 deletion public/assets/public.sass
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,16 @@
width: 100% !important

.ongkir-form-row-hidden
display: none !important
display: none !important

form.has-ongkir-dropdown
#billing_city_field,
#shipping_city_field
display: none !important

form:not(.has-ongkir-dropdown)
#_billing_city_field,
#_shipping_city_field,
#_billing_district_field,
#_shipping_district_field
display: none !important
Loading

0 comments on commit d995619

Please sign in to comment.