Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Created October 17, 2025 14:00
Show Gist options
  • Save xlplugins/8d8753ea3b8de63effb487b19f394a62 to your computer and use it in GitHub Desktop.
Save xlplugins/8d8753ea3b8de63effb487b19f394a62 to your computer and use it in GitHub Desktop.
Funnelkit Checkout: Compatibility With Plugin Hezarfen Turkish Address
if ( ! class_exists( 'WFACP_Compatibility_With_Hezarfen_Turkish_Address' ) ) {
class WFACP_Compatibility_With_Hezarfen_Turkish_Address {
public function __construct() {
// Ensure proper field types in FunnelKit
add_filter( 'wfacp_checkout_fields', [ $this, 'modify_field_types' ], 999 );
// Add JavaScript for re-initialization
add_action( 'wfacp_internal_css', [ $this, 'add_internal_js_css' ] );
}
/**
* Check if plugin is active
*/
private function is_enabled() {
return defined( 'WC_HEZARFEN_VERSION' ) && class_exists( 'Hezarfen\Inc\Checkout' );
}
/**
* Modify field types to ensure select dropdowns work properly
*/
public function modify_field_types( $fields ) {
if ( ! $this->is_enabled() ) {
return $fields;
}
foreach ( array( 'billing', 'shipping' ) as $type ) {
if ( ! isset( $fields[ $type ] ) ) {
continue;
}
// Ensure city (district) field is select type
if ( isset( $fields[ $type ][ $type . '_city' ] ) ) {
$fields[ $type ][ $type . '_city' ]['type'] = 'select';
// Add FunnelKit form control class
if ( ! isset( $fields[ $type ][ $type . '_city' ]['input_class'] ) ) {
$fields[ $type ][ $type . '_city' ]['input_class'] = array();
}
if ( ! in_array( 'wfacp-form-control', $fields[ $type ][ $type . '_city' ]['input_class'] ) ) {
$fields[ $type ][ $type . '_city' ]['input_class'][] = 'wfacp-form-control';
}
}
// Ensure address_1 (neighborhood) field is select type
if ( isset( $fields[ $type ][ $type . '_address_1' ] ) ) {
$fields[ $type ][ $type . '_address_1' ]['type'] = 'select';
// Add FunnelKit form control class
if ( ! isset( $fields[ $type ][ $type . '_address_1' ]['input_class'] ) ) {
$fields[ $type ][ $type . '_address_1' ]['input_class'] = array();
}
if ( ! in_array( 'wfacp-form-control', $fields[ $type ][ $type . '_address_1' ]['input_class'] ) ) {
$fields[ $type ][ $type . '_address_1' ]['input_class'][] = 'wfacp-form-control';
}
}
}
return $fields;
}
/**
* Add internal CSS and JavaScript for compatibility
*/
public function add_internal_js_css() {
if ( ! $this->is_enabled() ) {
return;
}
?>
<style type="text/css">
/* Ensure Select2 dropdowns display properly in FunnelKit */
.wfacp_main_form .select2-container {
width: 100% !important;
max-width: 100%;
}
.wfacp_main_form .select2-container .select2-selection--single {
height: auto;
min-height: 45px;
border: 1px solid #ddd;
}
.wfacp_main_form .select2-container .select2-selection--single .select2-selection__rendered {
line-height: 45px;
padding-left: 12px;
}
.wfacp_main_form .select2-container .select2-selection--single .select2-selection__arrow {
height: 45px;
}
.wfacp_main_form .select2-container--default .select2-selection--single .select2-selection__arrow b {
margin-top: -2px;
}
/* Hide fields with Hezarfen's hide class */
.wfacp_main_form .hezarfen-hide-form-field {
display: none !important;
}
</style>
<script type="text/javascript">
jQuery(function($) {
// Add missing wrapper classes that Hezarfen expects
function wfacp_add_hezarfen_wrappers() {
// Find billing fields wrapper
var $billingWrapper = $('.wfacp_divider_billing, #billing_email_field').closest('.wfacp-comm-title-wrap, .wfacp-section, .wfacp_main_form');
if ($billingWrapper.length && !$billingWrapper.hasClass('woocommerce-billing-fields')) {
// Create wrapper if it doesn't exist
if (!$('.woocommerce-billing-fields').length) {
var $billingFields = $('#billing_email_field, #billing_first_name_field, #billing_last_name_field, [id^="billing_"]').closest('.wfacp-comm-title-wrap, .wfacp-section').first();
if ($billingFields.length) {
$billingFields.addClass('woocommerce-billing-fields');
} else {
// Wrap all billing fields
$('[id^="billing_"]').wrapAll('<div class="woocommerce-billing-fields"></div>');
}
}
}
// Find shipping fields wrapper
var $shippingWrapper = $('.wfacp_divider_shipping, #shipping_first_name_field').closest('.wfacp-comm-title-wrap, .wfacp-section, .wfacp_main_form');
if ($shippingWrapper.length && !$shippingWrapper.hasClass('woocommerce-shipping-fields')) {
if (!$('.woocommerce-shipping-fields').length) {
var $shippingFields = $('#shipping_first_name_field, #shipping_last_name_field, [id^="shipping_"]').closest('.wfacp-comm-title-wrap, .wfacp-section').first();
if ($shippingFields.length) {
$shippingFields.addClass('woocommerce-shipping-fields');
} else {
// Wrap all shipping fields
$('[id^="shipping_"]').wrapAll('<div class="woocommerce-shipping-fields"></div>');
}
}
}
}
// Add animation class when select fields have value
function wfacp_add_anim_class_to_selects() {
// Turkish address fields that Hezarfen converts to select
var turkishFields = ['#billing_state', '#billing_city', '#billing_address_1', '#shipping_state', '#shipping_city', '#shipping_address_1'];
$.each(turkishFields, function(i, fieldId) {
var $field = $(fieldId);
if ($field.length && $field.val() && $field.val() !== '') {
var $fieldWrapper = $field.closest('.wfacp-form-control-wrapper');
if ($fieldWrapper.length && !$fieldWrapper.hasClass('wfacp-anim-wrap')) {
$fieldWrapper.addClass('wfacp-anim-wrap');
}
}
});
}
// Destroy existing select2 on Turkish address fields
function wfacp_destroy_select2_on_turkish_fields() {
var turkishFields = ['#billing_state', '#billing_city', '#billing_address_1', '#shipping_state', '#shipping_city', '#shipping_address_1'];
$.each(turkishFields, function(i, fieldId) {
var $field = $(fieldId);
if ($field.length && $field.hasClass('select2-hidden-accessible')) {
try {
$field.select2('destroy');
} catch(e) {
// Ignore if already destroyed
}
}
});
}
// Initialize Hezarfen fields
function wfacp_init_hezarfen() {
if (typeof hezarfen_mahalle_helper === 'undefined') {
return;
}
// Add wrapper classes first
wfacp_add_hezarfen_wrappers();
// Destroy FunnelKit's select2 first to avoid conflicts
wfacp_destroy_select2_on_turkish_fields();
// Initialize for billing and shipping
$('.woocommerce-billing-fields, .woocommerce-shipping-fields').each(function() {
var wrapper = $(this);
var type = wrapper.hasClass('woocommerce-billing-fields') ? 'billing' : 'shipping';
try {
var mahalle_helper = new hezarfen_mahalle_helper(wrapper, type, 'checkout');
var current_country = mahalle_helper.get_country_field().val();
// Convert to select2 if Turkey
if (!current_country || current_country === 'TR') {
mahalle_helper.convert_fields_to_selectwoo();
}
// Add event handlers for Turkey
if (current_country === 'TR') {
mahalle_helper.add_event_handlers();
}
} catch(e) {
console.log('Hezarfen initialization error:', e);
}
});
// Add animation class after initialization
setTimeout(wfacp_add_anim_class_to_selects, 100);
}
// Handle select2 change to add animation class
$(document).on('select2:select change', '#billing_state, #billing_city, #billing_address_1, #shipping_state, #shipping_city, #shipping_address_1', function() {
var $field = $(this);
if ($field.val() && $field.val() !== '') {
$field.closest('.wfacp-form-control-wrapper').addClass('wfacp-anim-wrap');
}
});
// Initialize on page load
$(document).ready(function() {
setTimeout(wfacp_init_hezarfen, 100);
});
// Re-initialize after FunnelKit AJAX updates
$(document.body).on('updated_checkout wfacp_step_switching', function() {
setTimeout(wfacp_init_hezarfen, 300);
});
// Handle country changes
$(document.body).on('country_to_state_changing', function(event, country_code, wrapper) {
if (typeof hezarfen_mahalle_helper === 'undefined') {
return;
}
// Ensure wrapper has the correct class
if (!wrapper.hasClass('woocommerce-billing-fields') && !wrapper.hasClass('woocommerce-shipping-fields')) {
if ($('#billing_country', wrapper).length) {
wrapper.addClass('woocommerce-billing-fields');
} else if ($('#shipping_country', wrapper).length) {
wrapper.addClass('woocommerce-shipping-fields');
}
}
var type = wrapper.hasClass('woocommerce-billing-fields') ? 'billing' : 'shipping';
// Destroy select2 before reinitializing
var addressFields = ['#' + type + '_state', '#' + type + '_city', '#' + type + '_address_1'];
$.each(addressFields, function(i, fieldId) {
var $field = $(fieldId);
if ($field.length && $field.hasClass('select2-hidden-accessible')) {
try {
$field.select2('destroy');
} catch(e) {
// Ignore
}
}
});
try {
var mahalle_helper = new hezarfen_mahalle_helper(wrapper, type, 'checkout');
// Get additional classes if needed
var additional_classes = [];
if (type === 'billing' && typeof wc_hezarfen_ajax_object !== 'undefined') {
additional_classes = wc_hezarfen_ajax_object.billing_district_field_classes || [];
} else if (type === 'shipping' && typeof wc_hezarfen_ajax_object !== 'undefined') {
additional_classes = wc_hezarfen_ajax_object.shipping_district_field_classes || [];
}
mahalle_helper.on_country_change(country_code, additional_classes);
// Re-add animation class after country change
setTimeout(wfacp_add_anim_class_to_selects, 150);
} catch(e) {
console.log('Hezarfen country change error:', e);
}
});
});
</script>
<?php
}
}
new WFACP_Compatibility_With_Hezarfen_Turkish_Address();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment