Last active
August 24, 2023 16:54
-
-
Save xlplugins/e442ac82a764804681fb6971a76b2a43 to your computer and use it in GitHub Desktop.
Show custom fields below the gateway
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_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 = [ 'mycheckbox' ]; // 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