尝试在结帐字段中传递自定义字段

时间:2016-10-19 08:46:06

标签: php wordpress woocommerce

在下面的代码中,您可以看到我想显示一个值,如果它已经存在于数据库中。

我得到了订单ID以及我有回音的值。 但是当我在价值中使用它时,它会导致空。

add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

    function my_custom_checkout_field( $checkout ) {
    $filters = array(
    'post_status' => 'any',
    'post_type' => 'shop_order',
    'paged' => 1,
    'orderby' => 'modified',
    'order' => 'DESC'
    );

     $loop = new WP_Query($filters);
     $order_id = $loop->post->ID;
     echo $key_1 = get_post_meta( $order_id, '_billing_my_dist', true );
     echo $key_2 = get_post_meta( $order_id, '_billing_my_street', true );
     echo $key_3 = get_post_meta( $order_id, '_billing_my_lane', true );
     echo $key_4 = get_post_meta( $order_id, '_billing_my_alley', true );
     echo $key_5 = get_post_meta( $order_id, '_billing_my_num', true );
     echo $key_6 = get_post_meta( $order_id, '_billing_my_floor', true );

    echo '<div id="my_custom_checkout_field"><h2>' . __('Delivery Address') . '</h2><p>' . __('Enter your address to ensure it is in the area of our delivery service') . '</p>';
     woocommerce_form_field( 'my_field_street', array(
            'type'          => 'text',
            'class'         => array('my-field-srt-class form-row-wide'),
            'label'         => __('Street'),
            'value' => get_post_meta( $order_id, '_billing_my_street', true ),
            'show'  => 'true',
            'placeholder'   => __('Street'),
         ), $checkout->get_value( 'my_field_street'));
    echo '</div>';
    }

1 个答案:

答案 0 :(得分:0)

你必须这样做:

 woocommerce_form_field( 'my_field_street', array(
        'type'          => 'text',
        'class'         => array('my-field-srt-class form-row-wide'),
        'label'         => __('Street'),
        'show'  => 'true',
        'placeholder'   => __('Street'),
     ), get_post_meta( $order_id, '_billing_my_street', true ));