【问题标题】:Merge custom fields in checkout page with shipping fields将结帐页面中的自定义字段与运输字段合并
【发布时间】:2021-02-01 14:44:01
【问题描述】:

我正在尝试将我在结帐页面上创建的自定义字段移动到运输块中。

我试过的是这个代码:thise are the new fields

// Add a new checkout field
function filter_checkout_fields($fields){
    
    $fields['extra_fields'] = array(
            'some_field' => array(
                'type' => 'text',
                'required'      => false,
                'label' => __( 'Field 1:' )
                ),
            'another_field' => array(
                'type' => 'text',
                'required'      => false,
                'label' => __( 'Field 2:' )
                ),

    return $fields;

    
}
add_filter( 'woocommerce_checkout_fields', 'filter_checkout_fields' );

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

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

    <div class="extra-fields">
    <h3><?php _e( 'Title' ); ?></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>

   <?php }
   add_action( 'woocommerce_checkout_after_customer_details' ,'extra_checkout_fields' );

这就是我试图移动它们的方式(替换当前字段)

add_filter( 'woocommerce_checkout_fields', 'another_group' );
 
function another_group( $checkout_fields ){
 
    // 1. We assign a field array to another group here
    $checkout_fields['some_field'] = $checkout_fields['shipping']['shipping_first_name'];
    $checkout_fields['another_field'] = $checkout_fields['shipping']['shipping_last_name'];
    
    // 2. Remove a field from a previous location
    unset( $checkout_fields['shipping']['shipping_first_name'] );
    unset( $checkout_fields['shipping']['shipping_last_name'] );
 
    return $checkout_fields;
 
}

发生的情况是 ['shipping_first_name']['shipping_last_name'] 被删除(未设置)但没有出现在它们的位置上。

这有可能发生吗?

【问题讨论】:

    标签: wordpress woocommerce customization checkout


    【解决方案1】:

    只需像以前一样取消设置不需要的字段,但不要尝试在此函数中覆盖它们

    add_filter( 'woocommerce_checkout_fields', 'another_group' );
     
    function another_group( $checkout_fields ){
             
        // 2. Remove a field from a previous location
        unset( $checkout_fields['shipping']['shipping_first_name'] );
        unset( $checkout_fields['shipping']['shipping_last_name'] );
    
         // ... more fields for unset here
     
        return $checkout_fields;
     
    }
    

    然后在您拥有[extra_fields] 的函数中将其更改为[shipping],这会将自定义字段放置在运输块中。

    // Add a new checkout field
    function filter_checkout_fields($fields){
        
        $fields['shipping'] = array(
            'some_field' => array(
                'type' => 'text',
                'required'      => false,
                'label' => __( 'Field 1:' )
                ),
                  ... so on
    

    【讨论】:

    • 嗯.. 这很容易。不知道怎么没想到,非常感谢。
    【解决方案2】:

    您可以使用旧字段优先级设置自定义字段的优先级,如下所示 $fields['shipping']['some_field']['priority'] = 10; $fields['shipping']['another_field']['priority'] = 20;

    【讨论】:

    • 那么,您的意思是我可以优先将它们移到那里(在运输块内),然后取消设置我不需要的字段?
    • 对不起,我昨天没有机会测试这个。今天会做,如果需要,会回来接受答案或获取更多信息。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 2018-07-01
    • 2021-11-18
    • 2016-04-25
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多