Kal*_*lov 2 php wordpress checkout custom-fields woocommerce
我正在使用WooCommerce:根据businessbloomer 的自定义单选按钮添加结帐费用
我的问题是woocommerce_form_field()在第 1 部分中使用了哪个
// etc...
echo '<div id="checkout-radio">';
echo '<h3>Customize Your Order!</h3>';
woocommerce_form_field( 'radio_choice', $args, $chosen );
echo '</div>';
// etc...
Run Code Online (Sandbox Code Playgroud)
woocommerce_form_field( 'radio_choice', $args, $chosen );输出的 HTML 的结构是
<p class="form-row form-row-wide update_totals_on_change" id="radio_choice_field" data-priority="">
<span class="woocommerce-input-wrapper">
<input type="radio" class="input-radio " value="0" name="radio_choice" id="radio_choice_0">
<label for="radio_choice_0" class="radio ">No Option</label>
<input type="radio" class="input-radio " value="10" name="radio_choice" id="radio_choice_10">
<label for="radio_choice_10" class="radio ">Option 1 ($10)</label>
<input type="radio" class="input-radio " value="30" name="radio_choice" id="radio_choice_30" checked="checked">
<label for="radio_choice_30" class="radio ">Option 2 ($30)</label>
</span>
</p>
Run Code Online (Sandbox Code Playgroud)
我试图得到的结构是这样的:
<div>
<ul>
<!-- here every element which is gonna be type radio needs to be with It's label in a <li> element -->
<li>
<input type='radio'> </input>
<label>No option</label>
</li>
<li>
<input type='radio'> </input>
<label>Option 2</label>
</li>
<li>
<input type='radio'> </input>
<label>Option 2</label>
</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
我搜索了一个解决方案,但我可以找到一个单选按钮
要重写woocommerce_form_field函数的输出 html,有 2 个过滤器钩子可用:
$args['type']像text,password,datetime,select,radio,等.../**
* Filter by type.
*/
$field = apply_filters( 'woocommerce_form_field_' . $args['type'], $field, $key, $args, $value );
Run Code Online (Sandbox Code Playgroud)
/**
* General filter on form fields.
*
* @since 3.4.0
*/
$field = apply_filters( 'woocommerce_form_field', $field, $key, $args, $value );
Run Code Online (Sandbox Code Playgroud)
第一个选项似乎最适合您的问题
所以
apply_filters( 'woocommerce_form_field_' . $args['type']...
Run Code Online (Sandbox Code Playgroud)
将会是(哪里$args['type']等于radio)
function filter_woocommerce_form_field_radio( $field, $key, $args, $value ) {
// Do something
return $field;
}
add_filter( 'woocommerce_form_field_radio', 'filter_woocommerce_form_field_radio', 10, 4 );
Run Code Online (Sandbox Code Playgroud)
回调函数使用$fieldwhich 包含从<p>结束</p>HTML 标记的所有 HTML ,因此我们有能力通过我们的函数重写 this 的整个输出
用于此答案的来源:wc-template-functions.php#L2847-L2850,请注意$args可以动态构造代码的用法。
(此代码是从wc-template-functions.php第 2847 - 2850 行复制的)
foreach ( $args['options'] as $option_key => $option_text ) {
$field .= '<input type="radio" class="input-radio ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" value="' . esc_attr( $option_key ) . '" name="' . esc_attr( $key ) . '" ' . implode( ' ', $custom_attributes ) . ' id="' . esc_attr( $args['id'] ) . '_' . esc_attr( $option_key ) . '"' . checked( $value, $option_key, false ) . ' />';
$field .= '<label for="' . esc_attr( $args['id'] ) . '_' . esc_attr( $option_key ) . '" class="radio ' . implode( ' ', $args['label_class'] ) . '">' . esc_html( $option_text ) . '</label>';
}
Run Code Online (Sandbox Code Playgroud)
所以回答你的问题:我们得到,基于$key这样它只适用于那个特定的作品
function filter_woocommerce_form_field_radio( $field, $key, $args, $value ) {
// Based on key
if ( $key == 'radio_choice' ) {
if ( ! empty( $args['options'] ) ) {
$field = '<div><ul>';
foreach ( $args['options'] as $option_key => $option_text ) {
$field .= '<li>';
$field .= '<input type="radio" value="' . esc_attr( $option_key ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '_' . esc_attr( $option_key ) . '" />';
$field .= '<label>' . esc_html( $option_text ) . '</label>';
$field .= '</li>';
}
$field .= '</ul></div>';
}
}
return $field;
}
add_filter( 'woocommerce_form_field_radio', 'filter_woocommerce_form_field_radio', 10, 4 );
Run Code Online (Sandbox Code Playgroud)
输出:
<div>
<ul>
<li>
<input type="radio" value="0" name="radio_choice" id="radio_choice_0">
<label>No Option</label>
</li>
<li>
<input type="radio" value="10" name="radio_choice" id="radio_choice_10">
<label>Option 1 ($10)</label>
</li>
<li>
<input type="radio" value="30" name="radio_choice" id="radio_choice_30">
<label>Option 2 ($30)</label>
</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3918 次 |
| 最近记录: |