Bootstrap 添加自定义CSS类到WooCommerce结帐字段

t1qtbnec  于 2023-09-28  发布在  Bootstrap
关注(0)|答案(5)|浏览(185)

我希望能够添加一个自定义的CSS类到我的WooCommerce结帐字段。我正在使用Twitter Bootstrap,我希望能够使用他们的.form-control类。
我在form-billing.php中的woocommerce templates文件夹中查找,但我不确定在哪里将.form-control类添加到每个文本字段。
下面是form-billing.php的代码

  1. <?php
  2. /**
  3. * Checkout billing information form
  4. *
  5. * @author WooThemes
  6. * @package WooCommerce/Templates
  7. * @version 2.1.2
  8. */
  9. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  10. ?>
  11. <div class="woocommerce-billing-fields">
  12. <?php if ( WC()->cart->ship_to_billing_address_only() && WC()->cart->needs_shipping() ) : ?>
  13. <h3><?php _e( 'Billing &amp; Shipping', 'woocommerce' ); ?></h3>
  14. <?php else : ?>
  15. <h3><?php _e( 'Billing Details', 'woocommerce' ); ?></h3>
  16. <?php endif; ?>
  17. <?php do_action( 'woocommerce_before_checkout_billing_form', $checkout ); ?>
  18. <?php foreach ( $checkout->checkout_fields['billing'] as $key => $field ) : ?>
  19. <?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
  20. <?php endforeach; ?>
  21. <?php do_action('woocommerce_after_checkout_billing_form', $checkout ); ?>
  22. <?php if ( ! is_user_logged_in() && $checkout->enable_signup ) : ?>
  23. <?php if ( $checkout->enable_guest_checkout ) : ?>
  24. <p class="form-row form-row-wide create-account">
  25. <input class="input-checkbox" id="createaccount" <?php checked( ( true === $checkout->get_value( 'createaccount' ) || ( true === apply_filters( 'woocommerce_create_account_default_checked', false ) ) ), true) ?> type="checkbox" name="createaccount" value="1" /> <label for="createaccount" class="checkbox"><?php _e( 'Create an account?', 'woocommerce' ); ?></label>
  26. </p>
  27. <?php endif; ?>
  28. <?php do_action( 'woocommerce_before_checkout_registration_form', $checkout ); ?>
  29. <?php if ( ! empty( $checkout->checkout_fields['account'] ) ) : ?>
  30. <div class="create-account">
  31. <p><?php _e( 'Create an account by entering the information below. If you are a returning customer please login at the top of the page.', 'woocommerce' ); ?></p>
  32. <?php foreach ( $checkout->checkout_fields['account'] as $key => $field ) : ?>
  33. <?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
  34. <?php endforeach; ?>
  35. <div class="clear"></div>
  36. </div>
  37. <?php endif; ?>
  38. <?php do_action( 'woocommerce_after_checkout_registration_form', $checkout ); ?>
  39. <?php endif; ?>
  40. </div>

我是否需要查看另一个模板文件?
谢谢

3qpi33ja

3qpi33ja1#

ICC97的答案几乎是存在的,但不起作用。
我把icc97的答案,并调试它:

  1. add_filter('woocommerce_checkout_fields', 'addBootstrapToCheckoutFields' );
  2. public function addBootstrapToCheckoutFields($fields) {
  3. foreach ($fields as &$fieldset) {
  4. foreach ($fieldset as &$field) {
  5. // if you want to add the form-group class around the label and the input
  6. $field['class'][] = 'form-group';
  7. // add form-control to the actual input
  8. $field['input_class'][] = 'form-control';
  9. }
  10. }
  11. return $fields;
  12. }
70gysomp

70gysomp2#

正如@Peanuts所指出的,如果我们只想将CSS类添加到某些输入类型,该怎么办?
在试用了到目前为止发布的解决方案之后,(感谢大家!),我提出了一个使用简单的“开关情况”的mod,其中逻辑取自/woocommerce/includes/wc-template-functions.php。该函数允许我们一次定位所有输入类型,无论是默认输入类型还是自定义输入类型。
不需要重写woocommerce_form_field函数来简单地更改输入类型的$defaults参数。值得一提的是,该函数还允许我们向字段中添加不仅仅是css类的内容。
这就是函数

  1. /*********************************************************************************************/
  2. /** WooCommerce - Modify each individual input type $args defaults /**
  3. /*********************************************************************************************/
  4. add_filter('woocommerce_form_field_args','wc_form_field_args',10,3);
  5. function wc_form_field_args( $args, $key, $value = null ) {
  6. /*********************************************************************************************/
  7. /** This is not meant to be here, but it serves as a reference
  8. /** of what is possible to be changed. /**
  9. $defaults = array(
  10. 'type' => 'text',
  11. 'label' => '',
  12. 'description' => '',
  13. 'placeholder' => '',
  14. 'maxlength' => false,
  15. 'required' => false,
  16. 'id' => $key,
  17. 'class' => array(),
  18. 'label_class' => array(),
  19. 'input_class' => array(),
  20. 'return' => false,
  21. 'options' => array(),
  22. 'custom_attributes' => array(),
  23. 'validate' => array(),
  24. 'default' => '',
  25. );
  26. /*********************************************************************************************/
  27. // Start field type switch case
  28. switch ( $args['type'] ) {
  29. case "select" : /* Targets all select input type elements, except the country and state select input types */
  30. $args['class'][] = 'form-group'; // Add a class to the field's html element wrapper - woocommerce input types (fields) are often wrapped within a <p></p> tag
  31. $args['input_class'] = array('form-control', 'input-lg'); // Add a class to the form input itself
  32. //$args['custom_attributes']['data-plugin'] = 'select2';
  33. $args['label_class'] = array('control-label');
  34. $args['custom_attributes'] = array( 'data-plugin' => 'select2', 'data-allow-clear' => 'true', 'aria-hidden' => 'true', ); // Add custom data attributes to the form input itself
  35. break;
  36. case 'country' : /* By default WooCommerce will populate a select with the country names - $args defined for this specific input type targets only the country select element */
  37. $args['class'][] = 'form-group single-country';
  38. $args['label_class'] = array('control-label');
  39. break;
  40. case "state" : /* By default WooCommerce will populate a select with state names - $args defined for this specific input type targets only the country select element */
  41. $args['class'][] = 'form-group'; // Add class to the field's html element wrapper
  42. $args['input_class'] = array('form-control', 'input-lg'); // add class to the form input itself
  43. //$args['custom_attributes']['data-plugin'] = 'select2';
  44. $args['label_class'] = array('control-label');
  45. $args['custom_attributes'] = array( 'data-plugin' => 'select2', 'data-allow-clear' => 'true', 'aria-hidden' => 'true', );
  46. break;
  47. case "password" :
  48. case "text" :
  49. case "email" :
  50. case "tel" :
  51. case "number" :
  52. $args['class'][] = 'form-group';
  53. //$args['input_class'][] = 'form-control input-lg'; // will return an array of classes, the same as bellow
  54. $args['input_class'] = array('form-control', 'input-lg');
  55. $args['label_class'] = array('control-label');
  56. break;
  57. case 'textarea' :
  58. $args['input_class'] = array('form-control', 'input-lg');
  59. $args['label_class'] = array('control-label');
  60. break;
  61. case 'checkbox' :
  62. break;
  63. case 'radio' :
  64. break;
  65. default :
  66. $args['class'][] = 'form-group';
  67. $args['input_class'] = array('form-control', 'input-lg');
  68. $args['label_class'] = array('control-label');
  69. break;
  70. }
  71. return $args;
  72. }

上面的函数完全解决了一次性定位结帐表单输入的问题,这对于结帐表单的默认输入类型甚至是自定义的新输入类型来说都是非常直接的。然而,似乎不可能在不创建新函数的情况下打印每个输入类型的html输出,正如@abhisek在他的答案中所示。

奖励

似乎该函数也可能影响WooCommerce的函数或结帐页面之外的模板打印的其他表单字段。通过使用is_page()函数,我设法有条件地仅在结帐页面上应用该函数。您的结帐页面可能会有不同的slug,因此请相应地进行更改以反映它。
如果您只需要将该函数应用于结帐页面,请执行以下操作:
add_filter()
//add_filter('woocommerce_form_field_args','wc_form_field_args', 10, 3);
并改用add_action
add_action('woocommerce_form_field_args', 'wc_form_field_args', 10, 3);

  1. function wc_form_field_args( $args, $key, $value = null ) {
  2. ...
  3. // Right after
  4. return $args;
  5. // Place the following
  6. if ( !is_page('checkout') ) {
  7. add_filter('woocommerce_form_field_args','wc_form_field_args', 10, 3);
  8. } else {
  9. remove_filter('woocommerce_form_field_args','wc_form_field_args', 10, 3);
  10. }
  11. }

之后,该函数只会影响结帐页面表单。

展开查看全部
nhhxz33t

nhhxz33t3#

@Chetan的link and run答案确实给予了你想要的,但和以往一样,它们从来都不是很好的答案。
最好的资源是Customizing checkout fields using actions and filters上的WooCommerce Codex页面。
在Chetan的页面中的“自定义WooCommerce Checkout字段标签和占位符文本”中,您需要将以下代码添加到functions.php中,我已经以这样的方式修改了它,它应该做你想要的,但我还没有测试代码:

  1. // Hook in
  2. add_filter( 'woocommerce_checkout_fields' , 'my_theme_custom_override_checkout_fields' );
  3. // Our hooked in function - $fields is passed via the filter!
  4. function my_theme_custom_override_checkout_fields( $fields ) {
  5. foreach ($fields as $fieldset) {
  6. foreach ($fieldset as $field) {
  7. $field['class'] = array('form-control');
  8. }
  9. }
  10. return $fields;
  11. }
sh7euo9m

sh7euo9m4#

我通过定义一个自定义函数解决了这个问题。逻辑直接取自wc-template-functions.php(我不确定这是否是正确的方法,但它确实解决了问题)。

  1. /*
  2. * Custom form field function for Bootstrap 3
  3. */
  4. function bootstrap_woocommerce_form_field( $key, $args, $value = null ) {
  5. $defaults = array(
  6. 'type' => 'text',
  7. 'label' => '',
  8. 'placeholder' => '',
  9. 'maxlength' => false,
  10. 'required' => false,
  11. 'class' => array(),
  12. 'label_class' => array(),
  13. 'input_class' => array(),
  14. 'return' => false,
  15. 'options' => array(),
  16. 'custom_attributes' => array(),
  17. 'validate' => array(),
  18. 'default' => '',
  19. );
  20. $args = wp_parse_args( $args, $defaults );
  21. if ( ( ! empty( $args['clear'] ) ) ) $after = '<div class="clear"></div>'; else $after = '';
  22. if ( $args['required'] ) {
  23. $args['class'][] = 'validate-required';
  24. $required = ' <abbr class="required" title="' . esc_attr__( 'required', 'woocommerce' ) . '">*</abbr>';
  25. } else {
  26. $required = '';
  27. }
  28. $args['maxlength'] = ( $args['maxlength'] ) ? 'maxlength="' . absint( $args['maxlength'] ) . '"' : '';
  29. if ( is_string( $args['label_class'] ) )
  30. $args['label_class'] = array( $args['label_class'] );
  31. if ( is_null( $value ) )
  32. $value = $args['default'];
  33. // Custom attribute handling
  34. $custom_attributes = array();
  35. if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) )
  36. foreach ( $args['custom_attributes'] as $attribute => $attribute_value )
  37. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
  38. if ( ! empty( $args['validate'] ) )
  39. foreach( $args['validate'] as $validate )
  40. $args['class'][] = 'validate-' . $validate;
  41. switch ( $args['type'] ) {
  42. case "country" :
  43. $countries = $key == 'shipping_country' ? WC()->countries->get_shipping_countries() : WC()->countries->get_allowed_countries();
  44. if ( sizeof( $countries ) == 1 ) {
  45. $field = '<div class="form-group form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field">';
  46. if ( $args['label'] )
  47. $field .= '<label class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label'] . '</label>';
  48. $field .= '<strong>' . current( array_values( $countries ) ) . '</strong>';
  49. $field .= '<input type="hidden" name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" value="' . current( array_keys($countries ) ) . '" ' . implode( ' ', $custom_attributes ) . ' class="country_to_state" />';
  50. $field .= '</div>' . $after;
  51. } else {
  52. $field = '<div class="form-group form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field">'
  53. . '<label for="' . esc_attr( $key ) . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label'] . $required . '</label>'
  54. . '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" class="country_to_state form-control country_select" ' . implode( ' ', $custom_attributes ) . '>'
  55. . '<option value="">'.__( 'Select a country&hellip;', 'woocommerce' ) .'</option>';
  56. foreach ( $countries as $ckey => $cvalue )
  57. $field .= '<option value="' . esc_attr( $ckey ) . '" '.selected( $value, $ckey, false ) .'>'.__( $cvalue, 'woocommerce' ) .'</option>';
  58. $field .= '</select>';
  59. $field .= '<noscript><input type="submit" name="woocommerce_checkout_update_totals" value="' . __( 'Update country', 'woocommerce' ) . '" /></noscript>';
  60. $field .= '</div>' . $after;
  61. }
  62. break;
  63. case "state" :
  64. /* Get Country */
  65. $country_key = $key == 'billing_state'? 'billing_country' : 'shipping_country';
  66. if ( isset( $_POST[ $country_key ] ) ) {
  67. $current_cc = wc_clean( $_POST[ $country_key ] );
  68. } elseif ( is_user_logged_in() ) {
  69. $current_cc = get_user_meta( get_current_user_id() , $country_key, true );
  70. if ( ! $current_cc) {
  71. $current_cc = apply_filters('default_checkout_country', (WC()->customer->get_country()) ? WC()->customer->get_country() : WC()->countries->get_base_country());
  72. }
  73. } elseif ( $country_key == 'billing_country' ) {
  74. $current_cc = apply_filters('default_checkout_country', (WC()->customer->get_country()) ? WC()->customer->get_country() : WC()->countries->get_base_country());
  75. } else {
  76. $current_cc = apply_filters('default_checkout_country', (WC()->customer->get_shipping_country()) ? WC()->customer->get_shipping_country() : WC()->countries->get_base_country());
  77. }
  78. $states = WC()->countries->get_states( $current_cc );
  79. if ( is_array( $states ) && empty( $states ) ) {
  80. $field = '<div class="form-group form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field" style="display: none">';
  81. if ( $args['label'] )
  82. $field .= '<label for="' . esc_attr( $key ) . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label'] . $required . '</label>';
  83. $field .= '<input type="hidden" class="hidden" name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" value="" ' . implode( ' ', $custom_attributes ) . ' placeholder="' . esc_attr( $args['placeholder'] ) . '" />';
  84. $field .= '</div>' . $after;
  85. } elseif ( is_array( $states ) ) {
  86. $field = '<div class="form-group form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field">';
  87. if ( $args['label'] )
  88. $field .= '<label for="' . esc_attr( $key ) . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label']. $required . '</label>';
  89. $field .= '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" class="state_select form-control " ' . implode( ' ', $custom_attributes ) . ' placeholder="' . esc_attr( $args['placeholder'] ) . '">
  90. <option value="">'.__( 'Select a state&hellip;', 'woocommerce' ) .'</option>';
  91. foreach ( $states as $ckey => $cvalue )
  92. $field .= '<option value="' . esc_attr( $ckey ) . '" '.selected( $value, $ckey, false ) .'>'.__( $cvalue, 'woocommerce' ) .'</option>';
  93. $field .= '</select>';
  94. $field .= '</div>' . $after;
  95. } else {
  96. $field = '<div class="form-group form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field">';
  97. if ( $args['label'] )
  98. $field .= '<label for="' . esc_attr( $key ) . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label']. $required . '</label>';
  99. $field .= '<input type="text" class="form-control input-text ' . implode( ' ', $args['input_class'] ) .'" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" ' . implode( ' ', $custom_attributes ) . ' />';
  100. $field .= '</div>' . $after;
  101. }
  102. break;
  103. case "textarea" :
  104. $field = '<div class="form-group form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field">';
  105. if ( $args['label'] )
  106. $field .= '<label for="' . esc_attr( $key ) . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label']. $required . '</label>';
  107. $field .= '<textarea name="' . esc_attr( $key ) . '" class="form-control input-text ' . implode( ' ', $args['input_class'] ) .'" id="' . esc_attr( $key ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '"' . ( empty( $args['custom_attributes']['rows'] ) ? ' rows="2"' : '' ) . ( empty( $args['custom_attributes']['cols'] ) ? ' cols="5"' : '' ) . implode( ' ', $custom_attributes ) . '>'. esc_textarea( $value ) .'</textarea>
  108. </div>' . $after;
  109. break;
  110. case "checkbox" :
  111. $field = '<div class="checkbox form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field">
  112. <label for="' . esc_attr( $key ) . '" class="checkbox ' . implode( ' ', $args['label_class'] ) .'" ' . implode( ' ', $custom_attributes ) . '>
  113. <input type="' . esc_attr( $args['type'] ) . '" class="input-checkbox" name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" value="1" '.checked( $value, 1, false ) .' />'
  114. . $args['label'] . $required . '</label>
  115. </div>' . $after;
  116. break;
  117. case "password" :
  118. $field = '<div class="form-group form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field">';
  119. if ( $args['label'] )
  120. $field .= '<label for="' . esc_attr( $key ) . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label']. $required . '</label>';
  121. $field .= '<input type="password" class="form-control input-text ' . implode( ' ', $args['input_class'] ) .'" name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" value="' . esc_attr( $value ) . '" ' . implode( ' ', $custom_attributes ) . ' />
  122. </div>' . $after;
  123. break;
  124. case "text" :
  125. $field = '<div class="form-group form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field">';
  126. if ( $args['label'] )
  127. $field .= '<label for="' . esc_attr( $key ) . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label'] . $required . '</label>';
  128. $field .= '<input type="text" class="form-control input-text ' . implode( ' ', $args['input_class'] ) .'" name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" '.$args['maxlength'].' value="' . esc_attr( $value ) . '" ' . implode( ' ', $custom_attributes ) . ' />
  129. </div>' . $after;
  130. break;
  131. case "select" :
  132. $options = '';
  133. if ( ! empty( $args['options'] ) )
  134. foreach ( $args['options'] as $option_key => $option_text )
  135. $options .= '<option value="' . esc_attr( $option_key ) . '" '. selected( $value, $option_key, false ) . '>' . esc_attr( $option_text ) .'</option>';
  136. $field = '<div class="form-group form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field">';
  137. if ( $args['label'] )
  138. $field .= '<label for="' . esc_attr( $key ) . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label']. $required . '</label>';
  139. $field .= '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" class="select form-control" ' . implode( ' ', $custom_attributes ) . '>
  140. ' . $options . '
  141. </select>
  142. </div>' . $after;
  143. break;
  144. default :
  145. $field = apply_filters( 'woocommerce_form_field_' . $args['type'], '', $key, $args, $value );
  146. break;
  147. }
  148. if ( $args['return'] ) return $field; else echo $field;
  149. }

不要忘记将所有出现的woocommerce_form_field替换为bootstrap_woocommerce_form_field

展开查看全部
liwlm1x9

liwlm1x95#

  1. /* New Checkout fields */
  2. add_filter('woocommerce_form_field_args', 'custom_wc_form_field_args', 20, 3);
  3. function custom_wc_form_field_args($args, $key, $value)
  4. {
  5. if (is_checkout()) {
  6. if ($key != 'billing_phone' && $key != 'billing_email' && $key != 'billing_postcode' && $key != 'billing_country') {
  7. $args['class'] = array('inline-input');
  8. }elseif($key == 'billing_country'){
  9. $args['class'] = array('inline-input update_totals_on_change');
  10. }
  11. else {
  12. $args['class'] = array();
  13. }
  14. // if ($key == 'billing_country' || $key == 'shipping_country') {
  15. // $args['class'] = array('inline-input', 'hidden');
  16. // }
  17. // $args['input_class'] = array('inline-text');
  18. if ($args['type'] == 'checkbox') {
  19. $args['label_class'] = array('custom-checkbox');
  20. } else {
  21. $args['label_class'] = array('sr-only');
  22. }
  23. }
  24. return $args;

}

展开查看全部

相关问题