php - 如何在 woocommerce 结账表单字段中重新排列/自定义 html

标签 php wordpress woocommerce

我想在我的 woocommerce 结账表单中完成两件事: 1. 在一些字段组之间放置一些文本,例如(h3):

<h3>my custom heading</h3>
<p class="form-row form-row validate-required">
  <input type="email">...
</p>

<h3>my other custom heading</h3>
<p class="form-row form-row validate-required">
  <input type="text">...
</p>
<p class="form-row form-row validate-required">
  <input type="text">...
</p>

2.在html中首先显示输入标签,然后显示标签标签(woocommerce默认在输入之前显示标签)

我发现此代码显示结账表单(用于计费):

<?php foreach ( $checkout->checkout_fields['billing'] as $key => $field ) : ?>

    <?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>

<?php endforeach; ?>

如何按照我描述的方式自定义它?

最佳答案

我不确定第 1 部分,但对于第 2 部分,您可以通过过滤 woocommerce_form_field_$type 来修改 woocommerce_form_field() 的输出.

所以你的第 2 部分可以这样解决:

function so_39267627_form_field( $field, $key, $args, $value ){

    if ( $args['required'] ) {
        $args['class'][] = 'validate-required';
        $required = ' <abbr class="required" title="' . esc_attr__( 'required', 'woocommerce'  ) . '">*</abbr>';
    } else {
        $required = '';
    }

    $args['maxlength'] = ( $args['maxlength'] ) ? 'maxlength="' . absint( $args['maxlength'] ) . '"' : '';

    $args['autocomplete'] = ( $args['autocomplete'] ) ? 'autocomplete="' . esc_attr( $args['autocomplete'] ) . '"' : '';

    if ( is_string( $args['label_class'] ) ) {
        $args['label_class'] = array( $args['label_class'] );
    }

    if ( is_null( $value ) ) {
        $value = $args['default'];
    }

    // Custom attribute handling
    $custom_attributes = array();

    // Custom attribute handling
    $custom_attributes = array();

    if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) ) {
        foreach ( $args['custom_attributes'] as $attribute => $attribute_value ) {
            $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
        }
    }

    $field = '';
    $label_id = $args['id'];
    $field_container = '<p class="form-row %1$s" id="%2$s">%3$s</p>';

    $field .= '<input type="' . esc_attr( $args['type'] ) . '" class="input-text ' . esc_attr( implode( ' ', $args['input_class'] ) ) .'" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" ' . $args['maxlength'] . ' ' . $args['autocomplete'] . ' value="' . esc_attr( $value ) . '" ' . implode( ' ', $custom_attributes ) . ' />';

    if ( ! empty( $field ) ) {
        $field_html = '';

        $field_html .= $field;

        if ( $args['description'] ) {
            $field_html .= '<span class="description">' . esc_html( $args['description'] ) . '</span>';
        }

        if ( $args['label'] && 'checkbox' != $args['type'] ) {
            $field_html .= '<label for="' . esc_attr( $label_id ) . '" class="' . esc_attr( implode( ' ', $args['label_class'] ) ) .'">' . $args['label'] . $required . '</label>';
        }

        $container_class = 'form-row ' . esc_attr( implode( ' ', $args['class'] ) );
        $container_id = esc_attr( $args['id'] ) . '_field';

        $after = ! empty( $args['clear'] ) ? '<div class="clear"></div>' : '';

        $field = sprintf( $field_container, $container_class, $container_id, $field_html ) . $after;
    }
    return $field;
}
add_filter( 'woocommerce_form_field_password', 'so_39267627_form_field', 10, 4 );
add_filter( 'woocommerce_form_field_text', 'so_39267627_form_field', 10, 4 );
add_filter( 'woocommerce_form_field_email', 'so_39267627_form_field', 10, 4 );
add_filter( 'woocommerce_form_field_tel', 'so_39267627_form_field', 10, 4 );
add_filter( 'woocommerce_form_field_number', 'so_39267627_form_field', 10, 4 );

您需要为其他字段类型编写更多函数(主要是我从 WooCommerce 复制并粘贴了整段代码,然后只是交换了标签部分),但这应该作为示例。

关于php - 如何在 woocommerce 结账表单字段中重新排列/自定义 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39267627/

相关文章:

php - header.php 中的 Wordpress Logo 图像

html - 垂直图像在某些手机上呈水平状态

wordpress - 如何将密件抄送收件人添加到 WooCommerce 发送的所有电子邮件中?

php - 将 Woocommerce 商店变成地理定位国家/地区的目录?

php - 将新值保存到 Laravel session 数组

PHP:使用类 java Comparable 对自定义类进行排序?

php - php循环中的ajax在按钮单击时执行操作

php - 如何使用源和图像获取 wp_posts 详细信息

mysql - 带有 WooCommerce 插件的 WordPress 数据库未显示所有数据

PHP - SimpleXML 解析错误