【问题标题】:How should I change the woocommerce_form_field HTML Structure?我应该如何更改 woocommerce_form_field HTML 结构?
【发布时间】:2021-01-07 13:11:27
【问题描述】:

我正在使用来自businessbloomer的WooCommerce: Add Checkout Fees Based on Custom Radio Button

我的问题是关于第 1 部分中使用的woocommerce_form_field()

// etc...
echo '<div id="checkout-radio">';
echo '<h3>Customize Your Order!</h3>';
woocommerce_form_field( 'radio_choice', $args, $chosen );
echo '</div>';
// etc...

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>

我想要得到的结构是这样的:

<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>

我搜索了一个解决方案,但我可以找到一个用于单选按钮的解决方案

【问题讨论】:

    标签: php wordpress woocommerce checkout custom-fields


    【解决方案1】:

    要重写 woocommerce_form_field 函数的输出 html,有 2 个过滤钩子可用:

    1. 按类型过滤:这里我们可以指定$args['type'],如textpassworddatetimeselectradio等...
    /**
     * Filter by type.
     */
    $field = apply_filters( 'woocommerce_form_field_' . $args['type'], $field, $key, $args, $value );
    
    1. 表单字段的通用过滤器:一个通用过滤器,我们可以在其中添加一个 if 条件以将其应用于特定类型的字段
    /**
     * General filter on form fields.
     *
     * @since 3.4.0
     */
    $field = apply_filters( 'woocommerce_form_field', $field, $key, $args, $value );
    

    第一个选项似乎最适合您的问题

    所以

    apply_filters( 'woocommerce_form_field_' . $args['type']...
    

    将是(其中$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 );
    

    回调函数使用$field,其中包含从&lt;p&gt;到结束&lt;/p&gt; HTML标签的所有HTML,因此我们可以通过我们的函数重写整个输出


    用于此答案的来源: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>';
    }
    

    所以回答你的问题:我们得到,基于$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 );
    

    输出:

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 2014-05-08
      • 2010-10-18
      • 2014-10-03
      相关资源
      最近更新 更多