从用户元字段Wordpress / WooCommerce获取价值

时间:2017-02-07 13:17:02

标签: php wordpress

要创建新字段,我添加了以下代码:

add_filter( 'woocommerce_checkout_fields' , 'add_vat_billing_field', 1 );

function add_vat_billing_field( $fields ) {

    $fields['billing']['kvk_number'] = array(
        'label'         => 'Btw-nummer',
        'placeholder'   => 'Btw-nummer',
        'required'      => false,
        'class'         => array('form-row-wide'),
        'clear'         => true
    );

    return $fields;
}

在其他一些代码中,使用以下代码显示此字段:

<?php foreach ( $checkout->checkout_fields['billing'] as $key => $field ) : ?>
    // The problem probably lies here in the "$key => $field" 
    <?php 
        // Make sure when "Guest" is selected the form fields are emptied
        if ( empty( $_POST['one_page_customer_user'] ) )
            $val = '';
        // Or if the user has no billing info leave fields empty
        else if ( !empty( $_POST['one_page_customer_user'] ) && false == get_user_meta( absint( $_POST['one_page_customer_user'] ), 'billing_first_name', true ) )
            $val = '';
        else 
            $val = $checkout->get_value( $key );

        woocommerce_form_field( $key, $field, $val ); 
    ?>

<?php endforeach; ?>

即使用户在kvk_number中存储了元字段值,但在完全填写其他字段时,它也不会显示。

任何帮助将不胜感激!

修改

事实证明$val = $checkout->get_value( $key );正试图通过该字段的值返回字段的值。 $key这里已经是值了。但是,它不是返回值,而是在寻找具有该字段值的字段作为名称。

修改2

function example_callback( $input ) {
    return $input;
}
add_filter( 'woocommerce_checkout_get_value', 'example_callback' );

现在这样做。现在必须让它更安全,因为它完全破坏了原来的get_value( $key );功能。

0 个答案:

没有答案
相关问题