Created
January 17, 2026 06:28
-
-
Save xlplugins/76b5ecf68a7ebd99bfc97aa6d4662bfd to your computer and use it in GitHub Desktop.
Funnelkit Compatibility class for Alegra WooCommerce Sync with FunnelKit Checkout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class WFACP_Compatibility_With_Alegra_WC_Sync { | |
| private $alegra_fields = []; | |
| private $registered_fields = []; | |
| public function __construct() { | |
| // Hook early to capture Alegra fields | |
| add_action( 'wfacp_after_checkout_page_found', [ $this, 'capture_alegra_fields' ], 5 ); | |
| // Ensure Alegra fields are properly registered | |
| add_action( 'after_setup_theme', [ $this, 'setup_alegra_fields' ], 9999 ); | |
| // Ensure fields are displayed in WFACP templates | |
| add_filter( 'wfacp_forms_field', [ $this, 'ensure_alegra_fields_display' ], 30, 2 ); | |
| // Handle field values and validation | |
| add_action( 'woocommerce_billing_fields', [ $this, 'billing_fields' ], 999999, 2 ); | |
| // Internal CSS and JS for styling and conditional logic | |
| add_action( 'wfacp_internal_css', [ $this, 'internal_css' ], 11 ); | |
| } | |
| /** | |
| * Capture Alegra fields after checkout page is found | |
| */ | |
| public function capture_alegra_fields() { | |
| // Get Alegra checkout handler | |
| $localized_handler = null; | |
| if ( class_exists( 'AlegraWCFactory' ) && method_exists( 'AlegraWCFactory', 'createResource' ) ) { | |
| $localized_handler = call_user_func( [ 'AlegraWCFactory', 'createResource' ], 'checkout' ); | |
| } | |
| if ( $localized_handler && method_exists( $localized_handler, 'get_checkout_fields' ) ) { | |
| // Get Alegra checkout fields from the handler | |
| $this->alegra_fields = $localized_handler->get_checkout_fields(); | |
| } | |
| // Also check via WooCommerce checkout fields to get all registered fields | |
| if ( function_exists( 'WC' ) && WC()->checkout() ) { | |
| $checkout_fields = WC()->checkout()->get_checkout_fields(); | |
| if ( isset( $checkout_fields['billing'] ) && is_array( $checkout_fields['billing'] ) ) { | |
| foreach ( $checkout_fields['billing'] as $key => $field ) { | |
| // Check if it's an Alegra field (starts with wc_alegra_) | |
| if ( strpos( $key, 'wc_alegra_' ) === 0 ) { | |
| $this->registered_fields[ $key ] = $field; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| /** | |
| * Setup Alegra fields for WFACP | |
| */ | |
| public function setup_alegra_fields() { | |
| if ( empty( $this->registered_fields ) && empty( $this->alegra_fields ) ) { | |
| return; | |
| } | |
| // Get registered WFACP fields (don't overwrite our stored fields) | |
| $wfacp_registered_fields = WFACP_Common::get_aero_registered_checkout_fields(); | |
| // Register Alegra fields if they exist | |
| if ( ! empty( $this->alegra_fields ) && class_exists( 'WFACP_Add_Address_Field' ) ) { | |
| foreach ( $this->alegra_fields as $field_id => $field_data ) { | |
| // Skip if already registered in WFACP | |
| if ( in_array( $field_id, $wfacp_registered_fields, true ) ) { | |
| continue; | |
| } | |
| // Prepare field for WFACP | |
| $wfacp_field = [ | |
| 'type' => $field_data['type'] ?? 'text', | |
| 'label' => $field_data['label'] ?? '', | |
| 'placeholder' => $field_data['placeholder'] ?? '', | |
| 'required' => ! empty( $field_data['required'] ), | |
| 'class' => $field_data['class'] ?? [], | |
| 'priority' => $field_data['priority'] ?? 100, | |
| 'default' => $field_data['default'] ?? '', | |
| 'description' => $field_data['description'] ?? '', | |
| ]; | |
| // Add options for select fields | |
| if ( isset( $field_data['options'] ) && is_array( $field_data['options'] ) ) { | |
| $wfacp_field['options'] = $field_data['options']; | |
| } | |
| // Add wrapper classes | |
| if ( ! empty( $field_data['wrapper_class'] ) ) { | |
| if ( is_array( $field_data['wrapper_class'] ) ) { | |
| $wfacp_field['class'] = array_merge( $wfacp_field['class'], $field_data['wrapper_class'] ); | |
| } else { | |
| $wfacp_field['class'][] = $field_data['wrapper_class']; | |
| } | |
| } | |
| // Ensure proper CSS classes | |
| if ( ! is_array( $wfacp_field['class'] ) ) { | |
| $wfacp_field['class'] = [ $wfacp_field['class'] ]; | |
| } | |
| $wfacp_field['class'][] = 'wfacp-col-full'; | |
| $wfacp_field['cssready'][] = 'wfacp-col-full'; | |
| $wfacp_field['custom'] = '1'; | |
| $wfacp_field['third_party'] = 'yes'; | |
| $wfacp_field['third_party_type'] = 'billing'; | |
| // Register the field | |
| new WFACP_Add_Address_Field( str_replace( 'billing_', '', $field_id ), $wfacp_field, 'billing' ); | |
| } | |
| } | |
| } | |
| /** | |
| * Ensure Alegra fields are displayed in WFACP templates | |
| */ | |
| public function ensure_alegra_fields_display( $field, $key ) { | |
| // Check if this is an Alegra field | |
| if ( strpos( $key, 'wc_alegra_' ) === 0 ) { | |
| // Ensure field is not empty | |
| if ( empty( $field ) ) { | |
| // Try to get from registered fields | |
| if ( isset( $this->registered_fields[ $key ] ) ) { | |
| $field = $this->registered_fields[ $key ]; | |
| } elseif ( isset( $this->alegra_fields[ $key ] ) ) { | |
| $field = $this->alegra_fields[ $key ]; | |
| } | |
| } | |
| // Ensure proper styling classes | |
| if ( ! empty( $field ) && is_array( $field ) ) { | |
| if ( ! isset( $field['class'] ) || ! is_array( $field['class'] ) ) { | |
| $field['class'] = []; | |
| } | |
| // Add WFACP styling classes | |
| if ( ! in_array( 'wfacp-col-full', $field['class'], true ) ) { | |
| $field['class'][] = 'wfacp-col-full'; | |
| } | |
| // Add Alegra-specific class for identification | |
| if ( ! in_array( 'wfacp-alegra-field', $field['class'], true ) ) { | |
| $field['class'][] = 'wfacp-alegra-field'; | |
| } | |
| // Handle conditional fields wrapper | |
| if ( isset( $field['wrapper_class'] ) ) { | |
| $wrapper_class = is_array( $field['wrapper_class'] ) | |
| ? $field['wrapper_class'] | |
| : [ $field['wrapper_class'] ]; | |
| $field['class'] = array_merge( $field['class'], $wrapper_class ); | |
| } | |
| } | |
| } | |
| return $field; | |
| } | |
| /** | |
| * Handle billing fields | |
| */ | |
| public function billing_fields( $fields, $country ) { | |
| // Ensure Alegra fields are preserved | |
| foreach ( $this->registered_fields as $key => $field ) { | |
| if ( ! isset( $fields[ $key ] ) ) { | |
| $fields[ $key ] = $field; | |
| } | |
| } | |
| return $fields; | |
| } | |
| /** | |
| * Internal CSS and JS for Alegra fields | |
| */ | |
| public function internal_css() { | |
| ?> | |
| <style> | |
| /* Alegra field styling */ | |
| #wfacp-sec-wrapper .wfacp-alegra-field { | |
| margin-bottom: 20px; | |
| } | |
| /* Conditional wrapper styling - hidden by default */ | |
| #wfacp-sec-wrapper .do-conditional-wrapper-wc_alegra_order_comprobante_fiscal { | |
| display: none; | |
| } | |
| #wfacp-sec-wrapper .do-conditional-wrapper-wc_alegra_order_comprobante_fiscal.show { | |
| display: block; | |
| } | |
| /* Select field styling */ | |
| #wfacp-sec-wrapper .wfacp-alegra-field select.alegra-select-field { | |
| width: 100%; | |
| padding: 12px; | |
| border: 1px solid #bfbfbf; | |
| border-radius: 4px; | |
| font-size: 14px; | |
| } | |
| /* Checkbox field styling */ | |
| #wfacp-sec-wrapper .wfacp-alegra-field input[type="checkbox"] { | |
| margin-right: 8px; | |
| } | |
| /* Ensure proper spacing for Alegra fields */ | |
| #wfacp-sec-wrapper .wfacp-alegra-field .woocommerce-input-wrapper { | |
| width: 100%; | |
| } | |
| /* Description styling */ | |
| #wfacp-sec-wrapper .wfacp-alegra-field .description { | |
| font-size: 12px; | |
| color: #666; | |
| margin-top: 5px; | |
| display: block; | |
| } | |
| /* Error styling */ | |
| #wfacp-sec-wrapper .wfacp-alegra-field.woocommerce-invalid .wfacp-form-control { | |
| border-color: #e2401c; | |
| } | |
| #wfacp-sec-wrapper .wfacp-alegra-field.woocommerce-invalid-required-field .wfacp-form-control-label::after { | |
| content: " *"; | |
| color: #e2401c; | |
| } | |
| /* Form row styling for Alegra fields */ | |
| #wfacp-sec-wrapper .wfacp-alegra-field.form-row-first, | |
| #wfacp-sec-wrapper .wfacp-alegra-field.form-row-last { | |
| width: 48%; | |
| float: left; | |
| margin-right: 4%; | |
| } | |
| #wfacp-sec-wrapper .wfacp-alegra-field.form-row-last { | |
| margin-right: 0; | |
| } | |
| #wfacp-sec-wrapper .wfacp-alegra-field.form-row-wide { | |
| width: 100%; | |
| clear: both; | |
| } | |
| </style> | |
| <script type="text/javascript"> | |
| (function($) { | |
| $(document).ready(function() { | |
| // Handle conditional display of Alegra fields | |
| function toggleAlegraConditionalFields() { | |
| var $comprobanteCheckbox = $('#wc_alegra_order_comprobante_fiscal'); | |
| if ($comprobanteCheckbox.length === 0) { | |
| // Try alternative selector | |
| $comprobanteCheckbox = $('input[name="wc_alegra_order_comprobante_fiscal"]'); | |
| } | |
| if ($comprobanteCheckbox.length > 0) { | |
| var isChecked = $comprobanteCheckbox.is(':checked'); | |
| var $conditionalFields = $('.do-conditional-wrapper-wc_alegra_order_comprobante_fiscal'); | |
| if (isChecked) { | |
| $conditionalFields.addClass('show').slideDown(300); | |
| // Make required fields actually required | |
| $conditionalFields.find('input[required], select[required]').prop('required', true); | |
| } else { | |
| $conditionalFields.removeClass('show').slideUp(300); | |
| // Remove required attribute when hidden | |
| $conditionalFields.find('input[required], select[required]').prop('required', false); | |
| // Clear values when hidden | |
| $conditionalFields.find('input, select').val(''); | |
| } | |
| } | |
| } | |
| // Initial check | |
| toggleAlegraConditionalFields(); | |
| // Listen for checkbox changes | |
| $(document).on('change', '#wc_alegra_order_comprobante_fiscal, input[name="wc_alegra_order_comprobante_fiscal"]', function() { | |
| toggleAlegraConditionalFields(); | |
| }); | |
| // Also handle when order review updates | |
| if (typeof wfacp_frontend !== 'undefined' && wfacp_frontend.hooks) { | |
| wfacp_frontend.hooks.addAction('wfacp_order_review_updated', function() { | |
| setTimeout(toggleAlegraConditionalFields, 100); | |
| }); | |
| } | |
| // Handle form updates | |
| $(document).on('updated_checkout', function() { | |
| setTimeout(toggleAlegraConditionalFields, 100); | |
| }); | |
| }); | |
| })(jQuery); | |
| </script> | |
| <?php | |
| } | |
| } | |
| new WFACP_Compatibility_With_Alegra_WC_Sync(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment