Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Created December 1, 2025 12:10
Show Gist options
  • Select an option

  • Save xlplugins/2444a4c412af5f13834169c6f39d1a39 to your computer and use it in GitHub Desktop.

Select an option

Save xlplugins/2444a4c412af5f13834169c6f39d1a39 to your computer and use it in GitHub Desktop.
FunnelKit Checkout - Aruba Fatturazione Elettronica Compatibility
if ( ! class_exists( 'WFACP_Compatibility_Aruba_Fatturazione_Elettronica' ) ) {
#[AllowDynamicProperties]
class WFACP_Compatibility_Aruba_Fatturazione_Elettronica {
/**
* Aruba field keys in correct display order
* Customer type and invoice choice first, then fiscal data heading, then tax fields
*
* @var array
*/
private $aruba_fields = array(
'billing_customer_type_aruba_fe',
'billing_need_invoice_aruba_fe',
'billing_send_choice_invoice_aruba_fe',
'billing_codice_fiscale_aruba_fe',
'billing_partita_iva_aruba_fe',
'billing_sdi_aruba_fe',
'billing_pec_aruba_fe',
);
/**
* Captured fields from Aruba plugin
*
* @var array
*/
private $captured_fields = array();
/**
* Initialize the compatibility class
*/
public function __construct() {
// Only run if Aruba plugin is active
if ( ! $this->is_aruba_active() ) {
return;
}
// Register custom field in FunnelKit backend
if ( class_exists( 'WFACP_Common' ) && WFACP_Common::is_funnel_builder_3() ) {
add_action( 'wffn_rest_checkout_form_actions', array( $this, 'register_aruba_field_group' ) );
} else {
add_action( 'init', array( $this, 'register_aruba_field_group' ), 20 );
}
// Prevent default HTML rendering for our custom field
add_filter( 'wfacp_html_fields_billing_wfacp_aruba_fe_fields', '__return_false' );
// Capture Aruba fields from WooCommerce billing fields
add_action( 'woocommerce_billing_fields', array( $this, 'capture_aruba_fields' ), 999999 );
// Display Aruba fields when our custom field is rendered
add_action( 'process_wfacp_html', array( $this, 'display_aruba_fields' ), 50, 2 );
// Add FunnelKit styling to Aruba fields
add_filter( 'woocommerce_form_field_args', array( $this, 'add_funnelkit_styling' ), 999, 2 );
// Save Aruba fields to order meta (only if needed)
add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'save_aruba_fields_to_order' ), 99, 2 );
// Prevent third-party billing fields wrapper (avoid duplicate rendering)
add_filter( 'wfacp_third_party_billing_fields', array( $this, 'remove_aruba_from_third_party' ) );
// Add custom CSS for proper field display
add_action( 'wfacp_internal_css', array( $this, 'add_custom_css' ) );
}
/**
* Check if Aruba Fatturazione Elettronica plugin is active
*
* @return bool
*/
private function is_aruba_active() {
return class_exists( 'ArubaFe\Admin\Checkout\ArubaFeCheckout' );
}
/**
* Register custom field group in FunnelKit backend BILLING tab
* This creates a draggable field in the billing section
*/
public function register_aruba_field_group() {
if ( ! class_exists( 'WFACP_Add_Address_Field' ) ) {
return;
}
new WFACP_Add_Address_Field( 'wfacp_aruba_fe_fields', array(
'type' => 'wfacp_html',
'label' => __( 'Aruba Italian Invoice Fields', 'woofunnels-aero-checkout' ),
'placeholder' => __( 'Italian Invoice Fields', 'woofunnels-aero-checkout' ),
'cssready' => array( 'wfacp-col-full' ),
'class' => array( 'form-row-wide', 'wfacp-col-full' ),
'required' => false,
'priority' => 70,
) );
}
/**
* Capture Aruba fields from WooCommerce billing fields
* This intercepts the fields added by Aruba plugin
*
* @param array $fields Billing fields
* @return array
*/
public function capture_aruba_fields( $fields ) {
if ( ! is_array( $fields ) || empty( $fields ) ) {
return $fields;
}
// Capture each Aruba field
foreach ( $this->aruba_fields as $field_key ) {
if ( isset( $fields[ $field_key ] ) ) {
$this->captured_fields[ $field_key ] = $fields[ $field_key ];
}
}
return $fields;
}
/**
* Display Aruba fields when our custom field is rendered
* All fields displayed at one position with WooCommerce classes for jQuery compatibility
*
* @param mixed $field Field data
* @param string $key Field key
*/
public function display_aruba_fields( $field, $key ) {
// Only process our custom field
if ( empty( $key ) || 'billing_wfacp_aruba_fe_fields' !== $key ) {
return;
}
// If fields not captured yet, try to get them directly
if ( empty( $this->captured_fields ) ) {
// Get fields directly from Aruba helper class
// phpcs:ignore Squiz.Commenting.InlineComment.InvalidEndChar
if ( class_exists( 'ArubaFe\Admin\Checkout\Helper\ArubaFeCheckoutHelper' ) ) {
// phpcs:ignore PHPCompatibility.LanguageConstructs.NewLanguageConstructs.t_ns_separatorFound
$aruba_fields = \ArubaFe\Admin\Checkout\Helper\ArubaFeCheckoutHelper::getField();
if ( is_array( $aruba_fields ) && ! empty( $aruba_fields ) ) {
$this->captured_fields = $aruba_fields;
}
}
}
// Check if we have captured fields
if ( empty( $this->captured_fields ) ) {
return;
}
// Add WooCommerce billing fields wrapper (class only, no hooks)
echo '<div class="woocommerce-billing-fields">';
// Add field wrapper (class only for jQuery selectors)
echo '<div class="woocommerce-billing-fields__field-wrapper">';
// Output all Aruba fields in specific order (customer_type first, others follow)
// This ensures proper field visibility behavior
foreach ( $this->aruba_fields as $field_key ) {
if ( isset( $this->captured_fields[ $field_key ] ) ) {
woocommerce_form_field( $field_key, $this->captured_fields[ $field_key ] );
}
}
// Close field wrapper
echo '</div>';
// Close billing fields wrapper
echo '</div>';
}
/**
* Add FunnelKit styling classes to Aruba fields
*
* @param array $args Field arguments
* @param string $key Field key
* @return array
*/
public function add_funnelkit_styling( $args, $key ) {
// Only style Aruba fields
if ( ! in_array( $key, $this->aruba_fields, true ) ) {
return $args;
}
// Add FunnelKit wrapper classes
$wrapper_classes = array_merge(
array( 'wfacp-form-control-wrapper', 'wfacp-col-full' ),
isset( $args['class'] ) ? (array) $args['class'] : array()
);
$args['class'] = $wrapper_classes;
$args['cssready'] = array( 'wfacp-col-full' );
// Add input classes for non-checkbox fields
if ( isset( $args['type'] ) && 'checkbox' !== $args['type'] && 'hidden' !== $args['type'] ) {
$input_classes = array_merge(
array( 'wfacp-form-control' ),
isset( $args['input_class'] ) ? (array) $args['input_class'] : array()
);
$args['input_class'] = $input_classes;
$label_classes = array_merge(
array( 'wfacp-form-control-label' ),
isset( $args['label_class'] ) ? (array) $args['label_class'] : array()
);
$args['label_class'] = $label_classes;
}
return $args;
}
/**
* Save Aruba fields to order meta
* Only runs on FunnelKit checkout to ensure data is saved
*
* @param int $order_id Order ID
* @param array $data Posted data
*/
public function save_aruba_fields_to_order( $order_id, $data ) {
// Only process if this is a FunnelKit checkout
if ( ! isset( $_POST['_wfacp_post_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- WooCommerce handles nonce verification
return;
}
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
}
// Save each Aruba field to order meta
foreach ( $this->aruba_fields as $field_key ) {
if ( isset( $_POST[ $field_key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- WooCommerce handles nonce verification
$value = sanitize_text_field( wp_unslash( $_POST[ $field_key ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
$order->update_meta_data( '_' . $field_key, $value );
}
}
$order->save();
}
/**
* Remove Aruba fields from third-party billing fields
* This prevents FunnelKit from rendering them twice
*
* @param array $fields Third-party billing fields
* @return array
*/
public function remove_aruba_from_third_party( $fields ) {
if ( ! is_array( $fields ) || empty( $this->captured_fields ) ) {
return $fields;
}
foreach ( $this->captured_fields as $field_key => $field_data ) {
if ( isset( $fields[ $field_key ] ) ) {
unset( $fields[ $field_key ] );
}
}
return $fields;
}
/**
* Add custom CSS for proper field display
*/
public function add_custom_css() {
if ( ! is_checkout() ) {
return;
}
?>
<style>
/* Aruba Fatturazione Elettronica fields styling */
#wfacp-e-form .wfacp-form-control-wrapper p#billing_customer_type_aruba_fe_field,
#wfacp-e-form .wfacp-form-control-wrapper p#billing_send_choice_invoice_aruba_fe_field,
#wfacp-e-form .wfacp-form-control-wrapper p#billing_need_invoice_aruba_fe_field,
#wfacp-e-form .wfacp-form-control-wrapper p#billing_codice_fiscale_aruba_fe_field,
#wfacp-e-form .wfacp-form-control-wrapper p#billing_partita_iva_aruba_fe_field,
#wfacp-e-form .wfacp-form-control-wrapper p#billing_sdi_aruba_fe_field,
#wfacp-e-form .wfacp-form-control-wrapper p#billing_pec_aruba_fe_field {
margin-bottom: 15px;
}
/* Ensure proper spacing */
body #wfacp-e-form .wfacp-form-control-wrapper.wfacp-col-full {
width: 100% !important;
float: none !important;
clear: both !important;
}
/* WooCommerce billing fields wrapper */
#wfacp-e-form .woocommerce-billing-fields {
width: 100%;
clear: both;
}
/* WooCommerce billing fields field wrapper - VERTICAL layout to prevent reordering issues */
#wfacp-e-form .woocommerce-billing-fields__field-wrapper {
width: 100%;
display: block !important;
clear: both;
}
/* Force all Aruba fields to display as block and take full width */
#wfacp-e-form .woocommerce-billing-fields__field-wrapper > p {
width: 100% !important;
float: none !important;
display: block !important;
clear: both !important;
}
/* Customer type dropdown - always visible at top */
p#billing_customer_type_aruba_fe_field {
order: 1 !important;
}
/* Need invoice checkbox - shows for person type */
p#billing_need_invoice_aruba_fe_field {
order: 2 !important;
}
/* Send choice invoice dropdown - shows for company type (before fiscal data heading) */
p#billing_send_choice_invoice_aruba_fe_field {
order: 3 !important;
}
/* Tax code field - appears after fiscal data heading */
p#billing_codice_fiscale_aruba_fe_field {
order: 4 !important;
}
/* VAT number field */
p#billing_partita_iva_aruba_fe_field {
order: 5 !important;
}
/* SDI field */
p#billing_sdi_aruba_fe_field {
order: 6 !important;
}
/* PEC field */
p#billing_pec_aruba_fe_field {
order: 7 !important;
}
/* Hidden fields should not take space or affect layout */
p#billing_need_invoice_aruba_fe_field[style*="display: none"],
p#billing_send_choice_invoice_aruba_fe_field[style*="display: none"],
p#billing_codice_fiscale_aruba_fe_field[style*="display: none"],
p#billing_partita_iva_aruba_fe_field[style*="display: none"],
p#billing_sdi_aruba_fe_field[style*="display: none"],
p#billing_pec_aruba_fe_field[style*="display: none"] {
display: none !important;
margin: 0 !important;
padding: 0 !important;
height: 0 !important;
visibility: hidden !important;
order: 999 !important;
}
/* Make sure select fields are properly styled */
#billing_customer_type_aruba_fe,
#billing_send_choice_invoice_aruba_fe {
width: 100%;
}
/* Responsive adjustments */
@media (max-width: 768px) {
#wfacp-e-form .wfacp-form-control-wrapper p#billing_customer_type_aruba_fe_field,
#wfacp-e-form .wfacp-form-control-wrapper p#billing_send_choice_invoice_aruba_fe_field,
#wfacp-e-form .wfacp-form-control-wrapper p#billing_need_invoice_aruba_fe_field,
#wfacp-e-form .wfacp-form-control-wrapper p#billing_codice_fiscale_aruba_fe_field,
#wfacp-e-form .wfacp-form-control-wrapper p#billing_partita_iva_aruba_fe_field,
#wfacp-e-form .wfacp-form-control-wrapper p#billing_sdi_aruba_fe_field,
#wfacp-e-form .wfacp-form-control-wrapper p#billing_pec_aruba_fe_field {
width: 100% !important;
margin-right: 0 !important;
}
}
</style>
<?php
}
}
// Initialize compatibility only if FunnelKit is active
if ( class_exists( 'WFACP_Core' ) ) {
new WFACP_Compatibility_Aruba_Fatturazione_Elettronica();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment