Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Created June 7, 2025 14:30
Show Gist options
  • Save xlplugins/30aa29035fcdc3357dcd8c1bc4fc3602 to your computer and use it in GitHub Desktop.
Save xlplugins/30aa29035fcdc3357dcd8c1bc4fc3602 to your computer and use it in GitHub Desktop.
FunnelKit - ACF integration
class WFACP_ACF_Custom_Fields {
private static $ins = null;
private $fields_label = [
'field_663d53424eeb3' => [
'label' => 'How did you hear about us?',
'id' => 'how_did_you_hear_about_us_copy',
],
'field_663d5100cb98f' => [
'label' => 'What is your role?',
'id' => 'user_role_copy2',
],
'field_663d5144be5b5' => [
'label' => 'Where will you use Bad Wolf products?',
'id' => 'where_will_your_purchase_be_used_copy',
],
'field_663d516a53a0f' => [
'label' => 'Would you like to receive emails from us?',
'id' => 'join_mailing_list_copy',
],
];
public function __construct() {
add_filter( 'wfacp_advanced_fields', [ $this, 'add_field' ], 20 );
add_filter( 'wfacp_html_fields_wfacp_acf_fields', '__return_false' );
add_action( 'wfacp_after_checkout_page_found', [ $this, 'action' ] );
add_action( 'process_wfacp_html', [ $this, 'display_field' ], 999, 2 );
add_action( 'woocommerce_checkout_process', [ $this, 'validate_custom_checkout_fields' ] );
add_action( 'woocommerce_checkout_order_processed', [ $this, 'save_custom_checkout_fields_to_user_meta' ], 10, 3 );
add_action( 'woocommerce_checkout_update_user_meta', [ $this, 'woocommerce_checkout_update_user_meta' ], 10, 2 );
add_action( 'wcccm_new_user_created_from_guest', [ $this, 'save_custom_checkout_fields_to_user_meta_from_guest' ], 10, 2 );
add_action( 'wfacp_internal_css', [ $this, 'add_css' ] );
add_filter( 'acf/load_field/type=select', function ( $field ) {
// Add class to select field
if ( ! isset( $field['class'] ) ) {
$field['class'] = '';
}
$field['class'] .= ' wfacp-form-control';
return $field;
} );
add_filter( 'acf/load_field/type=hidden', function ( $field ) {
// Add class to select field
if ( ! isset( $field['class'] ) ) {
$field['class'] = '';
}
$field['class'] .= ' wfacp-form-control';
return $field;
} );
add_filter( 'acf/load_field/type=input', function ( $field ) {
// Add class to select field
if ( ! isset( $field['class'] ) ) {
$field['class'] = '';
}
$field['class'] .= ' wfacp-form-control';
return $field;
} );
}
public function add_field( $fields ) {
$fields['wfacp_acf_fields'] = [
'type' => 'wfacp_html',
'class' => [ 'wfacp-col-full', 'wfacp-form-control-wrapper', 'wfacp_acf_fields' ],
'id' => 'wfacp_acf_fields',
'field_type' => 'wfacp_acf_fields',
'label' => __( 'ACF Fields', 'woofunnels-aero-checkout' ),
];
return $fields;
}
public function add_css() {
?>
<style>
body #wfacp-e-form ul.acf-checkbox-list.acf-bl input[type="checkbox"] {
position: relative;
margin: 0 !important;
}
#wfacp-e-form .wfacp_main_form.woocommerce .woocommerce-invalid-required-field .wfacp-form-control {
border-color: #D0011B;
box-shadow: 0 0 0 1px #D0011B;
}
</style>
<script>
(function ($) {
$(document).ready(function () {
$(document.body).on('wfacp_step_switching', function (e, v) {
console.log(e, v); // This will log the event and the step object
if (v.current_step === 'two_step') {
wfacp_frontend.hooks.addAction('wfacp_fields_validation', validation);
}
});
function isAcfField(fieldId) {
return fieldId.startsWith('acf-field');
}
function validation(el) {
$('select.wfacp-form-control, input.wfacp-form-control').each(function () {
let fieldId = $(this).attr('id');
if (fieldId && isAcfField(fieldId)) {
// Add class only to ACF fields
if (!$(this).val()) {
$('#' + fieldId).parents('.is-required').addClass('woocommerce-invalid woocommerce-invalid-required-field');
} else {
$('#' + fieldId).parents('.is-required').removeClass('woocommerce-invalid woocommerce-invalid-required-field');
}
}
});
$('.acf-field-checkbox').each(function () {
let isAnyChecked = $(this).find('input[type="checkbox"]:checked').length > 0;
if (!isAnyChecked) {
$('.acf-field-663d5144be5b5').addClass('woocommerce-invalid woocommerce-invalid-required-field');
} else {
$('.acf-field-663d5144be5b5').removeClass('woocommerce-invalid woocommerce-invalid-required-field');
}
});
}
});
})(jQuery);
</script>
<?php
}
public function action() {
self::get_instance();
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new ACF_For_WooCommerce();
}
return self::$ins;
}
public function display_field( $field, $key ) {
if ( empty( $key ) || 'wfacp_acf_fields' !== $key ) {
return;
}
if ( self::$ins instanceof ACF_For_WooCommerce ) {
if ( is_user_logged_in() ) {
self::$ins->acf_woocommerce_edit_account_form();
} else {
self::$ins->acf_woocommerce_register_form();
}
}
}
public function validate_custom_checkout_fields() {
$tmp_fields = [];
if ( isset( $_POST['acf'] ) && is_array( $_POST['acf'] ) && count( $_POST['acf'] ) > 0 ) {
foreach ( $_POST['acf'] as $key => $value ) {
if ( false !== strpos( $key, 'field_' ) ) {
if ( empty( $value ) ) {
$tmp_fields[] = $key;
}
}
}
}
if ( is_array( $tmp_fields ) && count( $tmp_fields ) > 0 ) {
foreach ( $tmp_fields as $tkey => $tvalue ) {
if ( $this->fields_label[ $tvalue ] ) {
wc_add_notice( __( 'Please fill in the custom field ' . $this->fields_label[ $tvalue ]['label'] ), 'error' );
}
}
}
}
public function woocommerce_checkout_update_user_meta( $user_id ) {
$tmp = [];
if ( isset( $_POST['acf'] ) && is_array( $_POST['acf'] ) && count( $_POST['acf'] ) > 0 ) {
foreach ( $_POST['acf'] as $key => $value ) {
if ( isset( $this->fields_label[ $key ] ) ) {
$id = $this->fields_label[ $key ]['id'];
if ( $id === 'where_will_your_purchase_be_used_copy' ) {
$for_saving = $value;
} else {
$for_saving = sanitize_text_field( $value );
}
if ( ! empty( $id ) && ! empty( $for_saving ) ) {
update_user_meta( $user_id, $id, $for_saving );
}
}
}
}
}
public function save_custom_checkout_fields_to_user_meta( $order_id, $posted_data, $order ) {
if ( isset( $_POST['acf'] ) && is_array( $_POST['acf'] ) && count( $_POST['acf'] ) > 0 ) {
foreach ( $_POST['acf'] as $key => $value ) {
// $order->update_meta_data( $key, $value );
if ( isset( $this->fields_label[ $key ] ) ) {
$id = $this->fields_label[ $key ]['id'];
if ( $id === 'where_will_your_purchase_be_used_copy' ) {
$for_saving = $value;
} else {
$for_saving = sanitize_text_field( $value );
}
$order->update_meta_data( $id, $for_saving );
}
}
}
if ( ! empty( $order_id ) ) {
$order->update_meta_data( '_acf_fields', $_POST['acf'] );
$order->save();
}
}
public function save_custom_checkout_fields_to_user_meta_from_guest( $user_id, $data_source ) {
if ( ! empty( $user_id ) && isset( $_POST['acf'] ) && is_array( $_POST['acf'] ) && count( $_POST['acf'] ) > 0 ) {
foreach ( $_POST['acf'] as $key => $value ) {
if ( isset( $this->fields_label[ $key ]['id'] ) && ! is_array( $this->fields_label[ $key ]['id'] ) ) {
if ( $this->fields_label[ $key ]['id'] == 'where_will_your_purchase_be_used_copy' ) {
$tmp = $value;
} else {
$tmp = sanitize_text_field( $value );
}
if ( ! empty( $this->fields_label[ $key ]['id'] ) && ! empty( $tmp ) ) {
update_user_meta( $user_id, $this->fields_label[ $key ]['id'], $tmp );
}
}
}
}
}
}
new WFACP_ACF_Custom_Fields();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment