【问题标题】:woocommerce Checkout page Select Type not workingwoocommerce结帐页面选择类型不起作用
【发布时间】:2016-10-08 20:57:04
【问题描述】:

我正在向我的 woocommerce 结帐表单添加一些自定义字段,文本字段在订单摘要页面(针对用户/客户)上显示确切值,对于管理员也是如此。但是如果我使用“Selcet”类型,它不会显示实际值

这是代码

 // Add a new checkout field
function kia_filter_checkout_fields($fields){
$fields['extra_fields'] = array(
        'some_field' => array(
            'type' => 'text',
            'required'      => true,
            'label' => __( 'Some field' )
            ),
        'another_field' => array(
            'type' => 'select',
            'options' => array( 'a' => __( 'apple' ), 'b' => __( 'bacon' ), 'c' => __( 'chocolate' ) ),
            'required'      => true,
            'label' => __( 'Another field' )
            )
        );

return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'kia_filter_checkout_fields' );

// display the extra field on the checkout form
function kia_extra_checkout_fields(){ 

$checkout = WC()->checkout(); ?>

<div class="extra-fields">
<h3><?php _e( 'Additional Fields' ); ?></h3>

<?php 
// because of this foreach, everything added to the array in the previous                   function will display automagically
foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : ?>

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

    <?php endforeach; ?>
</div>

SAVE THE EXTRA DATA ON CHECKOUT
// save the extra field when checkout is processed
function kia_save_extra_checkout_fields( $order_id, $posted ){
// don't forget appropriate sanitization if you are using a different field   type
 if( isset( $posted['some_field'] ) ) {
    update_post_meta( $order_id, '_some_field', sanitize_text_field(   $posted['some_field'] ) );
}
if( isset( $posted['another_field'] ) && in_array( $posted['another_field'], array( 'a', 'b', 'c' ) ) ) {
    update_post_meta( $order_id, '_another_field', $posted['another_field'] );
}
}
add_action( 'woocommerce_checkout_update_order_meta',   'kia_save_extra_checkout_fields', 10, 2 );

DISPLAY THE EXTRA DATA TO USERS

// display the extra data on order recieved page and my-account order review
function kia_display_order_data( $order_id ){  ?>
<h2><?php _e( 'Additional Info' ); ?></h2>
<table class="shop_table shop_table_responsive additional_info">
    <tbody>
        <tr>
            <th><?php _e( 'Some Field:' ); ?></th>
            <td><?php echo get_post_meta( $order_id, '_some_field', true );  ?></td>
        </tr>
        <tr>
            <th><?php _e( 'Another Field:' ); ?></th>
            <td><?php echo get_post_meta( $order_id, '_another_field', true  ); ?></td>
        </tr>
    </tbody>
</table>
<?php }
add_action( 'woocommerce_thankyou', 'kia_display_order_data', 20 );
add_action( 'woocommerce_view_order', 'kia_display_order_data', 20 );

所以不是“Apple”(在我的情况下是性别),而是在结帐后的摘要页面上显示“a”,这是屏幕截图 Screenshot for the reference

这里是教程链接:http://www.kathyisawesome.com/woocommerce-customize-checkout-fields/,Woocommerce 版本是 2.5.5

请帮忙, 谢谢。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    尝试替换

     'options' => array( 
                        'a' => __( 'apple' ), 
                        'b' => __( 'bacon' ), 
                        'c' => __( 'chocolate' ) 
                        ),
    

    'options' => array(
    '' => _('Please Select An Option...', 'woocommerce'),
    'a' => _('Apple', 'woocommerce' ),
    'b' => _('Bacon', 'woocommerce' ),
    'c' => _('Chocolate', 'woocommerce' )
    ),
    

    'options' => array( 'a' => 'Apple',
                        'b'  => 'Bacon',
                        'c'  => 'Chocolate' ),
    

    【讨论】:

    • 嘿,谢谢 但是什么也没发生,输出和以前一样。
    【解决方案2】:

    试试这个:

    //* Add select field to the checkout page
    add_action('woocommerce_before_order_notes', 'wps_add_select_checkout_field');
    function wps_add_select_checkout_field( $checkout ) {
    
        echo '<h2>'.__('Next Day Delivery').'</h2>';
    
        woocommerce_form_field( 'daypart', array(
            'type'          => 'select',
            'class'         => array( 'wps-drop' ),
            'label'         => __( 'Delivery options' ),
            'options'       => array(
                'blank'     => __( 'Select a day part', 'wps' ),
                'morning'   => __( 'In the morning', 'wps' ),
                'afternoon' => __( 'In the afternoon', 'wps' ),
                'evening'   => __( 'In the evening', 'wps' )
            )
     ),
    
        $checkout->get_value( 'daypart' ));
    
    }
    

    发件人:https://www.wpstud.io/add-custom-select-field-woocommerce-checkout-page/

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 2015-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多