将结帐页面中的自定义字段与运输字段合并
Merge custom fields in checkout page with shipping fields
我正在尝试将我在结帐页面上创建的自定义字段移动到运输块中。
我试过的是这段代码: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']
被移除(未设置)但它们的位置上什么也没有出现。
这有可能发生吗?
您可以使用旧字段优先级设置自定义字段的优先级,如下所示
$字段['shipping']['some_field']['priority'] = 10;
$字段['shipping']['another_field']['priority'] = 20;
只需取消设置您不需要的字段,但不要尝试在此函数中覆盖它们
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
我正在尝试将我在结帐页面上创建的自定义字段移动到运输块中。
我试过的是这段代码: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']
被移除(未设置)但它们的位置上什么也没有出现。
这有可能发生吗?
您可以使用旧字段优先级设置自定义字段的优先级,如下所示 $字段['shipping']['some_field']['priority'] = 10; $字段['shipping']['another_field']['priority'] = 20;
只需取消设置您不需要的字段,但不要尝试在此函数中覆盖它们
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