Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Last active March 24, 2025 14:05
Show Gist options
  • Save xlplugins/332de15be112966744578d5be3e24555 to your computer and use it in GitHub Desktop.
Save xlplugins/332de15be112966744578d5be3e24555 to your computer and use it in GitHub Desktop.
Show custom fields inside the gateway
class WFACP_CustomFields_Inside_Gateway {
private $hook = 'woocommerce_checkout_after_terms_and_conditions';
private $hook_priority = 10;
private $collected_fields = [];
public function __construct() {
add_action( $this->hook, [ $this, 'display_fields' ], $this->hook_priority );
add_filter( 'wfacp_forms_field', [ $this, 'remove_fields' ], 1000, 2 );
add_action( 'woocommerce_checkout_before_terms_and_conditions', [ $this, 'collected_fields' ] );
add_filter( 'wfacp_advanced_fields', [ $this, 'add_field' ], 20 );
add_filter( 'wfacp_internal_css', [ $this, 'internal_css' ] );
}
public function add_field( $fields ) {
$fields['wfacp_custom_html_field'] = [
'type' => 'wfacp_wysiwyg',
'class' => [ 'wfacp-col-full', 'wfacp-form-control-wrapper', 'wfacp_custom_html_field' ],
'id' => 'mycheckbox',
'field_type' => 'wfacp_custom_html_field',
'label' => __( 'Custom HTML', 'woofunnels-aero-checkout' ),
'default' => __( '', 'woofunnels-aero-checkout' ),
];
return $fields;
}
private function getFields() {
$fields = [
'single_test',
'checkbox',
'radio',
'html_fields',
'select_2_field',
'multi_select_fields',
'paragraph_field',
'number_fields',
'tel_field',
'password_feild',
'email_fields',
]; // Add your field IDs here with comma saperated for example 'myselect2', 'agree'
return $fields;
}
public function remove_fields( $field, $key ) {
$fields = $this->getFields();
if ( in_array( $key, $fields ) ) {
$temp = [];
return $temp;
}
return $field;
}
public function display_fields() {
$fields = $this->collected_fields;
$checkout = WC()->checkout();
foreach ( $fields as $key => $field ) {
$field_value = $checkout->get_value( $key );
woocommerce_form_field( $key, $field, $field_value );
}
}
public function collected_fields() {
$template = wfacp_template();
$checkout = WC()->checkout();
$customFields = $this->getFields();
$fields = $checkout->get_checkout_fields();
foreach ( $fields['billing'] as $key => $field ) {
if ( in_array( $key, $customFields ) ) {
$field = $template->merge_builder_data( $field, $key );
$this->collected_fields[ $key ] = $field;
}
}
if ( ! empty( $fields['shipping'] ) && count( $fields['shipping'] ) > 0 ) {
foreach ( $fields['shipping'] as $key => $field ) {
if ( in_array( $key, $customFields ) ) {
$field = $template->merge_builder_data( $field, $key );
$this->collected_fields[ $key ] = $field;
}
}
}
if ( ! empty( $fields['advanced'] ) && count( $fields['advanced'] ) > 0 ) {
foreach ( $fields['advanced'] as $key => $field ) {
if ( in_array( $key, $customFields ) ) {
$field = $template->merge_builder_data( $field, $key );
$this->collected_fields[ $key ] = $field;
}
}
}
}
public function internal_css() {
$instance = wfacp_template();
if ( ! $instance instanceof WFACP_Template_Common ) {
return;
}
$bodyClass = "body";
$px = $instance->get_template_type_px() . "px";
if ( 'pre_built' !== $instance->get_template_type() ) {
$px = "7px";
$bodyClass = "body #wfacp-e-form ";
}
$cssHtml = "<style>";
$cssHtml .= $bodyClass . ".wfacp_custom_field_wfacp_wysiwyg p {text-align:left;}";
$cssHtml .= "</style>";
echo $cssHtml;
}
}
new WFACP_CustomFields_Inside_Gateway();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment