Эта проблема может возникать из-за первой подключенной функции , где $order->billing_title;
неверно, так как начиная с Woocommerce 3, большинство свойств объекта не могут быть доступны напрямую. Это относится к $order
экземпляру WC_Order
Object. Вместо этого вы можете использовать унаследованный WC_Data
метод get_meta()
.
Есть также некоторые ошибки в приоритете хуков и аргументах.
Попробуйте следующий повторный код:
// Add Title field in billing address display
add_filter( 'woocommerce_order_formatted_billing_address', 'custom_add_title_formatted_billing_address', 10, 2 );
function custom_add_title_formatted_billing_address( $billing_address, $order ) {
$billing_address['title'] = $order->get_meta( '_billing_title');
return $billing_address;
}
add_filter( 'woocommerce_my_account_my_address_formatted_address', 'custom_my_account_my_address_formatted_address', 10, 3 );
function custom_my_account_my_address_formatted_address( $address, $customer_id, $address_type ) {
if ( $address_type == 'billing' ) {
$address['title'] = get_user_meta( $customer_id, 'billing_title', true );
}
return $address;
}
add_filter( 'woocommerce_address_to_edit', 'custom_address_to_edit', 10, 2 );
function custom_address_to_edit( $address, $load_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', 10, 2 );
function custom_formatted_address_replacements( $replacements, $args ) {
$replacements['{title}'] = '';
if ( ! empty( $args['title'] ) ) {
$replacements['{title}'] = __( 'Title', 'your-domain' ) . ' ' . $args['title'];
}
return $replacements;
}
add_filter( 'woocommerce_localisation_address_formats', 'custom_localisation_address_format', 10, 1 );
function custom_localisation_address_format( $formats ) {
$formats['IT'] .= "\n\n{title}";
return $formats;
}
add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields', 10, 1 );
function custom_admin_billing_fields( $billing_fields ) {
$billing_fields['title'] = array(
'label' => __( 'Title', 'your-domain' ),
'show' => true
);
return $billing_fields;
}
add_filter( 'woocommerce_found_customer_details', 'custom_found_customer_details', 10, 1 );
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', 10, 1 );
function custom_customer_meta_fields( $show_fields ) {
$show_fields['billing']['fields']['billing_title']['label'] = __( 'Title', 'woocommerce' );
return $show_fields;
}
Я не вижу ничего, что могло бы создать проблему в PHP 7.2…