Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 75 additions & 1 deletion assets/wp/js/account-address.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,81 @@
return $("#" + addressType + "_kiriof_destination_area_name");
}

function postcodeField($field) {
var addressType = String($field.attr("id") || "").indexOf("shipping_") === 0
? "shipping"
: "billing";
return $("#" + addressType + "_postcode");
}

function extractPostcode(row) {
row = row || {};
var postcode = row.postcode
|| row.zipcode
|| row.zip_code
|| row.postal_code
|| row.kode_pos
|| row.kodepos
|| "";

if (!postcode && row.text) {
var match = String(row.text).match(/\b\d{5}\b/);
postcode = match ? match[0] : "";
}

return String(postcode || "").replace(/\s+/g, "").trim();
}

function setPostcodeFromDistrict($field, selected) {
var postcode = extractPostcode(selected);
var $postcode = postcodeField($field);

if (!postcode || !$postcode.length) {
return;
}

$postcode
.data("kiriofSettingPostcodeFromDistrict", true)
.data("kiriofInitialPostcode", postcode)
.val(postcode)
.trigger("input")
.trigger("change");

setTimeout(function () {
$postcode.data("kiriofSettingPostcodeFromDistrict", false);
}, 0);
}

function clearDistrict($field) {
$field.val("").trigger("change.select2");
districtNameField($field).val("");
}

function hideBlockMirrorDistrictFields() {
$("input, select, textarea").each(function () {
var fieldName = String(this.name || "");
var fieldId = String(this.id || "");
if (fieldName.indexOf("_wc_") === -1 && fieldId.indexOf("_wc_") === -1) {
return;
}
if (
fieldName.indexOf("kiriminaja-official/kiriof_destination_area") === -1
&& fieldId.indexOf("kiriminaja-official/kiriof_destination_area") === -1
) {
return;
}

$(this).closest(".form-row, p, div").first().hide();
});
}

$(function () {
var select = $.fn.selectWoo || $.fn.select2;
var $fields = districtFields();

hideBlockMirrorDistrictFields();
setTimeout(hideBlockMirrorDistrictFields, 300);

if (!$fields.length || !select || typeof kiriofAjax === "undefined") {
return;
}
Expand Down Expand Up @@ -56,7 +122,11 @@
: [];
return {
results: $.map(rows, function (row) {
return { id: row.id, text: row.text };
return {
id: row.id,
text: row.text,
postcode: extractPostcode(row),
};
}),
};
},
Expand All @@ -68,6 +138,7 @@
.on("select2:select.kiriofAccountDistrict", function (event) {
var selected = event.params && event.params.data ? event.params.data : {};
districtNameField($field).val(selected.text || "");
setPostcodeFromDistrict($field, selected);
})
.on("select2:clear.kiriofAccountDistrict", function () {
districtNameField($field).val("");
Expand All @@ -79,6 +150,9 @@
}).on(
"input.kiriofAccountDistrict change.kiriofAccountDistrict",
function () {
if ($(this).data("kiriofSettingPostcodeFromDistrict")) {
return;
}
var currentPostcode = String($(this).val() || "");
if (currentPostcode === String($(this).data("kiriofInitialPostcode") || "")) {
return;
Expand Down
46 changes: 46 additions & 0 deletions assets/wp/js/form-billing-address.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@
}

getSearchAreaKelurahan();
kiriofRestoreClassicDistrictSelections();
changeDistrict();
kiriofScheduleClassicShippingMethodSelectInit();
kiriofInitBlockCheckoutCompatibility();
if (kiriofBillingAddressConfig.isCheckout) {
setTimeout(kiriofRestoreClassicDistrictSelections, 300);
setTimeout(kiriofRestoreClassicDistrictSelections, 1500);
}

if (kiriofBillingAddressConfig.isCart) {

Expand Down Expand Up @@ -74,6 +79,7 @@
// update_checkout once so WooCommerce can render native fee rows. Calling
// it again from updated_checkout creates an endless loading loop.
jQuery(document.body).on( 'updated_checkout', function() {
kiriofRestoreClassicDistrictSelections();
kiriofChangeCodPayment();
kiriofChangeDifferentAddress();
});
Expand Down Expand Up @@ -1792,6 +1798,45 @@
}
}

function kiriofRestoreClassicDistrictSelections() {
kiriofRestoreClassicDistrictSelection(
jQuery('#kiriof_destination_area'),
kiriofBillingAddressConfig.billingDistrict || {},
jQuery('[name="kiriof_destination_area_name"]')
);
kiriofRestoreClassicDistrictSelection(
jQuery('#kiriof_shipping_destination_area'),
kiriofBillingAddressConfig.shippingDistrict || {},
jQuery('[name="kiriof_shipping_destination_area_name"]')
);
}

function kiriofRestoreClassicDistrictSelection($select, district, $nameField) {
if (!$select.length || String($select.val() || '')) {
return;
}

var districtId = String((district && district.id) || '');
var districtName = String((district && district.name) || $nameField.val() || '').trim();
if (!districtId || !districtName || kiriofIsPlaceholderDistrictText(districtName)) {
return;
}

var hasOption = false;
$select.find('option').each(function() {
if (String(jQuery(this).val()) === districtId) {
hasOption = true;
jQuery(this).text(districtName).prop('selected', true);
return false;
}
});
if (!hasOption) {
$select.append(new Option(districtName, districtId, true, true));
}
$select.val(districtId).data('kiriofSelectedDistrictText', districtName).trigger('change.select2');
$nameField.val(districtName);
}

function changeDistrict(){

let kelurahanArea = "select#" + (kiriofBillingAddressConfig.fieldKey || 'kiriof_destination_area') + ",select#kiriof_shipping_destination_area";
Expand Down Expand Up @@ -2017,6 +2062,7 @@
$el.trigger('change.select2');
}
});
kiriofRestoreClassicDistrictSelections();
}

jQuery(document.body).on('updated_checkout', function() {
Expand Down
80 changes: 78 additions & 2 deletions inc/Controllers/AccountAddressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public function addDistrictFields( array $fields, string $address_type ): array
$options[ $district['id'] ] = $district['name'];
}

$address_2_key = $address_type . '_address_2';
if ( isset( $fields[ $address_2_key ]['value'] ) && '' !== $district['id'] && (string) $fields[ $address_2_key ]['value'] === $district['id'] ) {
$fields[ $address_2_key ]['value'] = '';
}

$fields = $this->hideBlockMirrorDistrictFields( $fields, $address_type, $district );

$field = array(
'label' => __( 'District', 'kiriminaja-official' ),
'required' => true,
Expand Down Expand Up @@ -57,6 +64,7 @@ public function validateDistrict( int $user_id, string $address_type, array $add

$district_id = $this->postedDistrictId( $address_type );
$district_name = $this->postedDistrictName( $address_type );
$this->clearPollutedAddress2Post( $address_type, $district_id );
if ( $district_id < 1 || '' === $district_name ) {
wc_add_notice( __( 'Please select a District.', 'kiriminaja-official' ), 'error' );
}
Expand All @@ -67,12 +75,19 @@ public function saveDistrict( int $user_id, string $address_type ): void {
return;
}

$district_id = $this->postedDistrictId( $address_type );
$district_name = $this->postedDistrictName( $address_type );
if ( $district_id > 0 && '' === $district_name ) {
$saved_district = ( new CustomerDistrictService() )->get( $user_id, $address_type );
$district_name = $saved_district['name'];
}
( new CustomerDistrictService() )->save(
$user_id,
$address_type,
$this->postedDistrictId( $address_type ),
$this->postedDistrictName( $address_type )
$district_id,
$district_name
);
$this->syncCheckoutSession( $address_type, $district_id, $district_name );
}

private function isEditAddressRequest( string $address_type = '' ): bool {
Expand All @@ -99,6 +114,67 @@ private function postedDistrictName( string $address_type ): string {
return isset( $_POST[ $key ] ) ? sanitize_text_field( wp_unslash( $_POST[ $key ] ) ) : '';
}

private function clearPollutedAddress2Post( string $address_type, int $district_id ): void {
if ( $district_id < 1 ) {
return;
}

$key = $address_type . '_address_2';
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- WooCommerce verifies the account address nonce before this hook.
$address_2 = isset( $_POST[ $key ] ) ? sanitize_text_field( wp_unslash( $_POST[ $key ] ) ) : '';
if ( (string) $district_id === $address_2 ) {
$_POST[ $key ] = '';
}
}

private function hideBlockMirrorDistrictFields( array $fields, string $address_type, array $district ): array {
$prefix = '_wc_' . $address_type . '/kiriminaja-official/kiriof_destination_area';

foreach ( $fields as $key => $field ) {
if ( ! is_string( $key ) || 0 !== strpos( $key, $prefix ) ) {
continue;
}

$fields[ $key ]['type'] = 'hidden';
$fields[ $key ]['required'] = false;
$fields[ $key ]['label'] = '';
$fields[ $key ]['class'] = array( 'kiriof-hidden-district-mirror' );
$fields[ $key ]['value'] = false !== strpos( $key, '_name' ) ? $district['name'] : $district['id'];
}

return $fields;
}

private function syncCheckoutSession( string $address_type, int $district_id, string $district_name ): void {
if ( ! function_exists( 'WC' ) || ! WC()->session ) {
return;
}

$id_key = 'shipping' === $address_type ? 'shipping_destination_id' : 'destination_id';
$name_key = 'shipping' === $address_type ? 'shipping_destination_name' : 'destination_name';
WC()->session->set( $id_key, $district_id > 0 ? $district_id : '' );
WC()->session->set( $name_key, $district_name );

$postcode_key = $address_type . '_postcode';
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- WooCommerce verifies the account address nonce before this hook.
$postcode = isset( $_POST[ $postcode_key ] ) ? sanitize_text_field( wp_unslash( $_POST[ $postcode_key ] ) ) : '';
$postcode = trim( preg_replace( '/\s+/', '', $postcode ) );
if ( '' === $postcode ) {
return;
}

$saved_map = (array) WC()->session->get( 'kiriof_destination_postcode_map', array() );
if ( $district_id > 0 ) {
$saved_map[ $postcode ] = array(
'destination_id' => (string) $district_id,
'destination_name' => $district_name,
);
} else {
unset( $saved_map[ $postcode ] );
}
WC()->session->set( 'kiriof_destination_postcode_map', $saved_map );
}

private function insertAfterPostcode( array $fields, string $field_key, array $field ): array {
$postcode_key = 0 === strpos( $field_key, 'shipping_' ) ? 'shipping_postcode' : 'billing_postcode';
return $this->insertAfterKey( $fields, $postcode_key, $field_key, $field );
Expand Down
Loading
Loading