From 91b04484b0b506da7b870a95104531baecc3a7b4 Mon Sep 17 00:00:00 2001 From: Stef Kariotidis Date: Sat, 19 Nov 2016 11:46:50 +0200 Subject: [PATCH] make forms more bootstrap like --- inc/woocommerce.php | 97 ++++++++++++++++++++++++++- woocommerce/global/form-login.php | 62 +++++++++++++++++ woocommerce/global/quantity-input.php | 25 +++++++ 3 files changed, 181 insertions(+), 3 deletions(-) create mode 100755 woocommerce/global/form-login.php create mode 100755 woocommerce/global/quantity-input.php diff --git a/inc/woocommerce.php b/inc/woocommerce.php index 51ab159..b55cdc5 100644 --- a/inc/woocommerce.php +++ b/inc/woocommerce.php @@ -7,8 +7,99 @@ */ add_action( 'after_setup_theme', 'woocommerce_support' ); -if ( ! function_exists ( 'woocommerce_support' ) ) { +if ( ! function_exists( 'woocommerce_support' ) ) { function woocommerce_support() { - add_theme_support( 'woocommerce' ); + add_theme_support( 'woocommerce' ); + // hook in and customizer form fields + add_filter( 'woocommerce_form_field_args', 'wc_form_field_args', 10, 3 ); } -} \ No newline at end of file +} + +/** + * Filter hook function monkey patching form classes + * Author: Adriano Monecchi http://stackoverflow.com/a/36724593/307826 + * + * @param $args + * @param $key + * @param null $value + * + * @return mixed + */ +function wc_form_field_args( $args, $key, $value = null ) { + +// Start field type switch case + + switch ( $args['type'] ) { + + /* Targets all select input type elements, except the country and state select input types */ + case 'select' : + // Add a class to the field's html element wrapper - woocommerce + // input types (fields) are often wrapped within a

tag. + $args['class'][] = 'form-group'; + // Add a class to the form input itself. + $args['input_class'] = array( 'form-control', 'input-lg' ); + $args['label_class'] = array( 'control-label' ); + $args['custom_attributes'] = array( + 'data-plugin' => 'select2', + 'data-allow-clear' => 'true', + 'aria-hidden' => 'true', + // Add custom data attributes to the form input itself. + ); + break; + + // By default WooCommerce will populate a select with the country names - $args + // defined for this specific input type targets only the country select element. + case 'country' : + $args['class'][] = 'form-group single-country'; + $args['label_class'] = array( 'control-label' ); + break; + + // By default WooCommerce will populate a select with state names - $args defined + // for this specific input type targets only the country select element. + case 'state' : + // Add class to the field's html element wrapper. + $args['class'][] = 'form-group'; + // add class to the form input itself. + $args['input_class'] = array( '', 'input-lg' ); + $args['label_class'] = array( 'control-label' ); + $args['custom_attributes'] = array( + 'data-plugin' => 'select2', + 'data-allow-clear' => 'true', + 'aria-hidden' => 'true', + ); + break; + + case 'password' : + case 'text' : + case 'email' : + case 'tel' : + case 'number' : + $args['class'][] = 'form-group'; + $args['input_class'] = array( 'form-control', 'input-lg' ); + $args['label_class'] = array( 'control-label' ); + break; + + case 'textarea' : + $args['input_class'] = array( 'form-control', 'input-lg' ); + $args['label_class'] = array( 'control-label' ); + break; + + case 'checkbox' : + $args['label_class'] = array( 'custom-control custom-checkbox' ); + $args['input_class'] = array( 'custom-control-input', 'input-lg' ); + break; + + case 'radio' : + $args['label_class'] = array( 'custom-control custom-radio' ); + $args['input_class'] = array( 'custom-control-input', 'input-lg' ); + break; + + default : + $args['class'][] = 'form-group'; + $args['input_class'] = array( 'form-control', 'input-lg' ); + $args['label_class'] = array( 'control-label' ); + break; + } + + return $args; +} diff --git a/woocommerce/global/form-login.php b/woocommerce/global/form-login.php new file mode 100755 index 0000000..41f2f30 --- /dev/null +++ b/woocommerce/global/form-login.php @@ -0,0 +1,62 @@ + +
> + + + + + +

+ + +

+

+ + +

+
+ + + +

+ + + + +

+

+ +

+ +
+ + + +
diff --git a/woocommerce/global/quantity-input.php b/woocommerce/global/quantity-input.php new file mode 100755 index 0000000..ccb361a --- /dev/null +++ b/woocommerce/global/quantity-input.php @@ -0,0 +1,25 @@ + +
+ +