【发布时间】:2019-04-05 14:49:55
【问题描述】:
我正在向我的 Woocommerce 帐单和送货地址添加一个“标题”字段。一切都适用于 PHP 7,但是当我升级到 7.1 或 7.2 时,WooCommerce 中的订单页面会崩溃并且无法正确显示订单。所以即使有 3 页订单,我也只能看到第一个订单,如果我点击那个订单,它只会显示部分详细信息,根本没有地址。
// Add Title field in billing address display
add_filter( 'woocommerce_order_formatted_billing_address',
'custom_add_title_formatted_billing_address', 1, 1 );
function custom_add_title_formatted_billing_address( $fields, $order ) {
$fields['title'] = $order->billing_title;
return $fields;
}
add_filter( 'woocommerce_my_account_my_address_formatted_address',
'custom_my_account_my_address_formatted_address', 1, 1 );
function custom_my_account_my_address_formatted_address( $fields, $customer_id,
$type ) {
if ( $type == 'billing' ) {
$fields['title'] = get_user_meta( $customer_id, 'billing_title', true );
}
return $fields;
}
add_filter( 'woocommerce_address_to_edit', 'custom_address_to_edit', 1 );
function custom_address_to_edit( $address ) {
global $wp_query;
if ( isset( $wp_query->query_vars['edit-address'] ) && $wp_query-
>query_vars['edit-address'] != 'billing' ) {
return $address;
}
if ( ! isset( $address['billing_title'] ) ) {
$address['billing_title'] = array(
'label' => __( 'Title', 'your-domain' ),
'placeholder' => _x( 'Mr', 'placeholder', 'your-domain' ),
'required' => false, //change to false if you do not need this field
to be required
'class' => array( 'form-row-first' ),
'value' => get_user_meta( get_current_user_id(), 'billing_title',
true )
);
}
return $address;
}
add_filter( 'woocommerce_formatted_address_replacements',
'custom_formatted_address_replacements' );
function custom_formatted_address_replacements( $address, $args ) {
$address['{title}'] = '';
if ( ! empty( $args['title'] ) ) {
$address['{title}'] = __( 'Title', 'your-domain' ) . ' ' . $args['title'];
}
return $address;
}
add_filter( 'woocommerce_localisation_address_formats',
'custom_localisation_address_format', 1 );
function custom_localisation_address_format( $formats ) {
$formats['IT'] .= "\n\n{title}";
return $formats;
}
add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields',
1 );
function custom_admin_billing_fields( $fields ) {
$fields['title'] = array(
'label' => __( 'Title', 'your-domain' ),
'show' => true
);
return $fields;
}
add_filter( 'woocommerce_found_customer_details',
'custom_found_customer_details' );
function custom_found_customer_details( $customer_data ) {
$customer_data['billing_title'] = get_user_meta( $_POST['user_id'],
'billing_title', true );
return $customer_data;
}
add_filter( 'woocommerce_customer_meta_fields', 'custom_customer_meta_fields'
);
function custom_customer_meta_fields( $fields ) {
$fields['billing']['fields']['billing_title'] = array(
'label' => __( 'Title', 'woocommerce' )
);
return $fields;
}
非常感谢任何想法。
【问题讨论】:
-
我建议先修正你的报价,比如这个
add_filter( 'woocommerce_order_formatted_billing_address', custom_add_title_formatted_billing_address',特别是这个custom_add_title_formatted_billing_address'你看到上面的代码块中的所有东西都颜色很奇怪.....
标签: php wordpress object methods woocommerce