Nat*_*teW 12 wordpress twitter-bootstrap woocommerce woothemes
我希望能够在我的WooCommerce结帐字段中添加自定义CSS类.我正在使用twitter Bootstrap,我希望能够使用他们的.form-control类.
我查看了woocommerce模板文件夹,form-billing.php
但我不确定将.form-control
类添加到每个文本字段的位置.
这是代码 form-billing.php
<?php
/**
* Checkout billing information form
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.1.2
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
?>
<div class="woocommerce-billing-fields">
<?php if ( WC()->cart->ship_to_billing_address_only() && WC()->cart->needs_shipping() ) : ?>
<h3><?php _e( 'Billing & Shipping', 'woocommerce' ); ?></h3>
<?php else : ?>
<h3><?php _e( 'Billing Details', 'woocommerce' ); ?></h3>
<?php endif; ?>
<?php do_action( 'woocommerce_before_checkout_billing_form', $checkout ); ?>
<?php foreach ( $checkout->checkout_fields['billing'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
<?php do_action('woocommerce_after_checkout_billing_form', $checkout ); ?>
<?php if ( ! is_user_logged_in() && $checkout->enable_signup ) : ?>
<?php if ( $checkout->enable_guest_checkout ) : ?>
<p class="form-row form-row-wide create-account">
<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>
</p>
<?php endif; ?>
<?php do_action( 'woocommerce_before_checkout_registration_form', $checkout ); ?>
<?php if ( ! empty( $checkout->checkout_fields['account'] ) ) : ?>
<div class="create-account">
<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>
<?php foreach ( $checkout->checkout_fields['account'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
<div class="clear"></div>
</div>
<?php endif; ?>
<?php do_action( 'woocommerce_after_checkout_registration_form', $checkout ); ?>
<?php endif; ?>
</div>
Run Code Online (Sandbox Code Playgroud)
我是否需要查看另一个模板文件?
谢谢
Dan*_*art 34
icc97的答案几乎就在那里,但不起作用.
我拿了icc97的答案,并调试了它:
add_filter('woocommerce_checkout_fields', 'addBootstrapToCheckoutFields' );
public function addBootstrapToCheckoutFields($fields) {
foreach ($fields as &$fieldset) {
foreach ($fieldset as &$field) {
// if you want to add the form-group class around the label and the input
$field['class'][] = 'form-group';
// add form-control to the actual input
$field['input_class'][] = 'form-control';
}
}
return $fields;
}
Run Code Online (Sandbox Code Playgroud)
Adr*_*chi 19
正如@Peanuts指出的那样,如果我们只想将CSS类添加到某些输入类型呢?
在试验到目前为止发布的解决方案之后,(感谢大家!),我想出了一个使用简单"开关盒"的mod,其中逻辑取自/woocommerce/includes/wc-template-functions.php
.该函数允许我们一次定位所有输入类型,无论是默认输入类型还是自定义输入类型.
没有必要重写woocommerce_form_field
函数来简单地更改$defaults
输入类型的args.有必要提一下,该函数还允许我们向字段添加不仅仅是css类.
- 所以这是功能 -
/*********************************************************************************************/
/** WooCommerce - Modify each individual input type $args defaults /**
/*********************************************************************************************/
add_filter('woocommerce_form_field_args','wc_form_field_args',10,3);
function wc_form_field_args( $args, $key, $value = null ) {
/*********************************************************************************************/
/** This is not meant to be here, but it serves as a reference
/** of what is possible to be changed. /**
$defaults = array(
'type' => 'text',
'label' => '',
'description' => '',
'placeholder' => '',
'maxlength' => false,
'required' => false,
'id' => $key,
'class' => array(),
'label_class' => array(),
'input_class' => array(),
'return' => false,
'options' => array(),
'custom_attributes' => array(),
'validate' => array(),
'default' => '',
);
/*********************************************************************************************/
// Start field type switch case
switch ( $args['type'] ) {
case "select" : /* Targets all select input type elements, except the country and state select input types */
$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
$args['input_class'] = array('form-control', 'input-lg'); // Add a class to the form input itself
//$args['custom_attributes']['data-plugin'] = 'select2';
$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;
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 */
$args['class'][] = 'form-group single-country';
$args['label_class'] = array('control-label');
break;
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 */
$args['class'][] = 'form-group'; // Add class to the field's html element wrapper
$args['input_class'] = array('form-control', 'input-lg'); // add class to the form input itself
//$args['custom_attributes']['data-plugin'] = 'select2';
$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'][] = 'form-control input-lg'; // will return an array of classes, the same as bellow
$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' :
break;
case 'radio' :
break;
default :
$args['class'][] = 'form-group';
$args['input_class'] = array('form-control', 'input-lg');
$args['label_class'] = array('control-label');
break;
}
return $args;
}
Run Code Online (Sandbox Code Playgroud)
上面的函数完全解决了同时定位结帐表单输入的问题,这对于结帐表单默认输入类型甚至是自定义新输入类型来说非常简单.似乎不可能打印每个输入类型的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);
function wc_form_field_args( $args, $key, $value = null ) {
...
// Right after
return $args;
// Place the following
if ( !is_page('checkout') ) {
add_filter('woocommerce_form_field_args','wc_form_field_args', 10, 3);
} else {
remove_filter('woocommerce_form_field_args','wc_form_field_args', 10, 3);
}
}
Run Code Online (Sandbox Code Playgroud)
之后,该功能只会影响结帐页面.
@Chetan 的链接和运行答案确实给了你你想要的东西,但一如既往,它们从来都不是很好的答案。
最好的资源是 WooCommerce Codex 页面上的“使用操作和过滤器自定义结帐字段”。
在 Chetan 页面的“自定义 WooCommerce 结账字段标签和占位符文本”中,您需要将以下代码添加到您的functions.php中,我已经对其进行了修改,使其可以执行您想要的操作,但是我没有测试过代码:
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'my_theme_custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function my_theme_custom_override_checkout_fields( $fields ) {
foreach ($fields as $fieldset) {
foreach ($fieldset as $field) {
$field['class'] = array('form-control');
}
}
return $fields;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
28756 次 |
最近记录: |