隐藏特定的woocommerce帐单字段

时间:2017-08-19 18:12:55

标签: php woocommerce

我有一个woocommerce结帐的网址,变量会自动输入结算字段,因此不应在结帐页面显示。 我们怎么能这样做?

type="hidden"添加到输入字段?

感谢。

2 个答案:

答案 0 :(得分:0)

我们应该尽量减少用户参与"尽可能多(删除type=hidden是一件非常容易的事情),所以如果您有自动添加的数据,我建议您完全禁用这些字段并通过WooCommerce过滤器插入数据。

1。删除要隐藏的字段

您需要的第一个过滤器是woocommerce_billing_fields。使用该过滤器,您可以删除或添加字段。它接受一个参数,以及那个计费字段数组。例如:

/**
 * Used to remove WooCommerce billing fields
 * 
 * @param array $fields
 * 
 * @return array Modified billing fields
 */
function so_remove_billing_fields( $fields ){
    // FYI: These are all default WooCommerce billing fields
    // (remove those which need to stay)
    $fields_to_remove = array(
        'billing_first_name',
        'billing_last_name',
        'billing_company',
        'billing_address_1',
        'billing_address_2',
        'billing_city',
        'billing_postcode',
        'billing_country',
        'billing_state',
        'billing_email',
        'billing_phone',
    );

    foreach( $fields_to_remove as $field ){
        unset( $fields[$field] );
    }

    return $fields;
}
add_filter( 'woocommerce_billing_fields', 'so_remove_billing_fields' );

2。添加应保存的数据

我们现在应该将自定义数据保存到post(order)元,为此,我们将使用woocommerce_checkout_update_order_meta过滤器。它有一个参数,即整数订单ID。例如:

/**
 * Save custom data to the database
 * 
 * @param int $order_id
 *
 * @return void
 */
function so_add_billing_fields_data( $order_id ){
    $data_to_add = array(
        'field_name' => apply_filters( 'so_custom_field_name_data', null ),
    );

    foreach( $data_to_add as $field_name => $data ){
        if ( ! empty( $data ) )
            update_post_meta( $order_id, $field_name, sanitize_text_field( $data ) );
    }
}
add_filter( 'woocommerce_checkout_update_order_meta', 'so_add_billing_fields_data' );

这里可能没有必要sanitize_text_field(),但比抱歉更安全。我不知道您的数据来源。 ;)

顺便说一句,我在这个例子中选择了apply_filters来向数组中添加数据,但是如果你也可以在这个函数中收集数据并将它放入变量中。

查看更多:WooCommerce: Customizing checkout fields | WordPress: Filter API Reference | Validating Sanitizing and Escaping User Data

答案 1 :(得分:0)

是的,他们告诉您,结算字段中没有隐藏任何类型 但如果你要创造一个,那就有空间。

这样的事情会发生。

1。首先,让我们将字段类型更改为隐藏,具体取决于在URL上传递的内容。

function woocommerce_billing_fields( $fields ){
    // let's not do anything if not on checkout page.
    if ( !is_checkout() ) return $fields; 

    // these are expected fields. 
    // please remove those that are not needed.
    $url_param_fields = array(
        'first_name',
        'last_name',
        'company',
        'address_1',
        'address_2',
        'city',
        'postcode',
        'country',
        'state',
        'email',
        'phone',
    );

    foreach( $url_param_fields as $param ){
        $billing_key = 'billing_' . $param;
        if ( isset( $_GET[$param] ) && array_key_exists( $billing_key, $fields) ) {
            $fields[$billing_key]['type'] = 'hidden'; // let's change the type of this to hidden.
            $fields[$billing_key]['default'] = $_GET[$param]; // you need to sanitized $_GET[$param] here.
        }
    }

    return $fields;
}
add_filter( 'woocommerce_billing_fields', 'woocommerce_billing_fields' );

2。当然,默认的WooCommerce没有隐藏类型。

但是下面的代码会创建它。

function woocommerce_form_field_hidden( $field, $key, $args ){

    $field = '<input type="hidden" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="' . esc_attr( $args['default'] ) . '" />';

    return $field;
}
add_filter( 'woocommerce_form_field_hidden', 'woocommerce_form_field_hidden', 10, 3 );

预期的网址将类似于http://reigelgallarde.me/checkout/?company=reigelgallarde.me。在这种情况下,结算公司字段将更改为

 <input type="hidden" name="billing_company" id="billing_company" value="reigelgallarde.me">
相关问题