简体   繁体   中英

woocommerce Adding custom field under shipping table (my account-order view/ thank you)

With the help of these 2 posts ( WooCommerce editable custom checkout field and displayed in formatted address )( Add additional fields to Admin User under Customer shipping section in Woocommerce ), I added the custom field in the checkout, my account, single order, admin, and email. but there are 3 more area I would like to show this custom field and I couldn't find the hook needed to show them. I searched online but with no luck at all. I would love it if someone could guide me on this.

1- under shipping address table in thank you page 感谢页面中的送货地址表

2- under shipping address table in order view on my account page 订单视图中的送货地址表

3- I would like to show the field in the address on my account. but its only visible when I click edit在此处输入图像描述

this is the code I used to add the extra field and its working so far

// HOUSE NUMBER EXTRA FIELD
// display shipping House Number in checkout and my account edit shipping address
add_filter( 'woocommerce_shipping_fields', 'add_shipping_house_number_field' );
function add_shipping_house_number_field( $fields ) {
   $fields['shipping_house_number'] = array(
      'label' => __( 'House Number', 'woocommerce' ),
      //'required' => true,
      'class' => array( 'form-row-first' ),
      'priority' => 61,
   );
   return $fields;
}

// Display editable custom fields on admin single order pages (backend new order) edit pages inside edit shipping section
add_filter( 'woocommerce_admin_shipping_fields' , 'add_order_admin_edit_shipping_house_number' );
function add_order_admin_edit_shipping_house_number( $fields ) {
    // Include shipping house_number as editable field
    $fields['house_number'] = array( 'label' => __( 'House Number', 'woocommerce' ), 'show' => '0' );

    return $fields;
}

// Load Ajax custom field data as customer billing/shipping address fields in backend new order (data will be pulled from the database)
add_filter( 'woocommerce_ajax_get_customer_details' , 'add_custom_fields_to_ajax_customer_details', 10, 3 );
function add_custom_fields_to_ajax_customer_details( $data, $customer, $user_id ) {
    
    $data['shipping'][house_number] = $customer->get_meta('shipping_house_number');

    return $data;
}

// Display reordered editable custom fields on admin single User pages
add_filter( 'woocommerce_customer_meta_fields', 'house_number_customer_meta_fields', 10, 1 );
function house_number_customer_meta_fields( $fields ) {
    $fields['shipping']['fields']['shipping_house_number'] = array(
        'label'       => __( 'House Number', 'woocommerce' ),
    );

    return $fields;
}



// Adding custom placeholder to woocommerce formatted address only on Backend
add_filter( 'woocommerce_localisation_address_formats', 'admin_localisation_address_formats', 50, 1 );
function admin_localisation_address_formats( $address_formats ){
    // Only in backend (Admin)
    if( is_admin() || ! is_wc_endpoint_url() ) {
        foreach( $address_formats as $country_code => $address_format ) {
            $address_formats[$country_code] .= "\n{house_number}";
        }
    }
    return $address_formats;
}

// Custom placeholder replacement to woocommerce formatted address
add_filter( 'woocommerce_formatted_address_replacements', 'custom_formatted_address_replacements', 10, 2 );
function custom_formatted_address_replacements( $replacements, $args  ) {
    $replacements['{house_number}'] = ! empty($args['house_number']) ? $args['house_number'] : '';

    return $replacements;
}

// Add the shipping house number value to be displayed on email notifications under shipping address
add_filter( 'woocommerce_order_formatted_shipping_address', 'add_shipping_house_number_to_formatted_shipping_address', 100, 2 );
function add_shipping_house_number_to_formatted_shipping_address( $shipping_address, $order ) {
    global $pagenow, $post_type;

    // Not on admin order edit pages (as it's already displayed).
    if( ! ( $pagenow === 'post.php' && $post_type === 'shop_order' && isset($_GET['action']) && $_GET['action'] === 'edit' ) ) {
        // Include shipping phone on formatted shipping address
        $shipping_address['house_number'] = $order->get_meta('_shipping_house_number');
    }
    return $shipping_address;
}

For thank you page and my account view order you can use WC woocommerce_order_get_formatted_shipping_address filter hook.

function add_woocommerce_order_get_formatted_shipping_address( $address, $empty_content, $raw_address, $order ){
    $address .= '<br/> this is custom text';
    return $address;
}
add_filter( 'woocommerce_order_get_formatted_shipping_address', 'add_woocommerce_order_get_formatted_shipping_address', 10, 4 );

Thank you Page

在此处输入图像描述

My Account View Order.

在此处输入图像描述

My Account edit address

You need to override. woocommerce\templates\myaccount\my-address.php to your theme or plugin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM