Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter ivenkatdev

    (@ivenkatdev)

    Sure @daianagan.!
    If you’re stuck with the same concept, the below code might help you.,

    // for assign custom field – here “Landmark” is my custom field
    add_filter( ‘woocommerce_default_address_fields’, ‘woo_new_default_address_fields’ );

    function woo_new_default_address_fields( $fields ) {
    $fields[‘landmark’] = array(
    ‘label’ => __( ‘Landmark’, ‘woocommerce’ ),
    ‘class’ => array( ‘form-row-wide’, ‘address-field’ ),
    );
    return $fields;
    }

    // custom field inclusion/declaration for billing address
    add_filter( ‘woocommerce_order_formatted_billing_address’ , ‘woo_custom_order_formatted_billing_address’ , 10, 2 );

    function woo_custom_order_formatted_billing_address( $address, $WC_Order ) {
    $address = array(
    ‘first_name’ => $WC_Order->billing_first_name,
    ‘last_name’ => $WC_Order->billing_last_name,
    ‘landmark’ => $WC_Order->billing_landmark,
    ‘company’ => $WC_Order->billing_company,
    ‘address_1’ => $WC_Order->billing_address_1,
    ‘address_2’ => $WC_Order->billing_address_2,
    ‘city’ => $WC_Order->billing_city,
    ‘state’ => $WC_Order->billing_state,
    ‘postcode’ => $WC_Order->billing_postcode,
    ‘country’ => $WC_Order->billing_country
    );
    return $address;
    }

    // custom field inclusion/declaration for billing address – as i need it for shipping address ultimately or if a user chose Ship to different address option – this might help
    add_filter( ‘woocommerce_order_formatted_shipping_address’ , ‘woo_custom_order_formatted_shipping_address’ , 10, 2 );

    function woo_custom_order_formatted_shipping_address( $address, $WC_Order ) {
    $address = array(
    ‘first_name’ => $WC_Order->shipping_first_name,
    ‘last_name’ => $WC_Order->shipping_last_name,
    ‘landmark’ => $WC_Order->shipping_landmark,
    ‘company’ => $WC_Order->shipping_company,
    ‘address_1’ => $WC_Order->shipping_address_1,
    ‘address_2’ => $WC_Order->shipping_address_2,
    ‘city’ => $WC_Order->shipping_city,
    ‘state’ => $WC_Order->shipping_state,
    ‘postcode’ => $WC_Order->shipping_postcode,
    ‘country’ => $WC_Order->shipping_country
    );
    return $address;
    }

    // custom field replacements
    add_filter( ‘woocommerce_formatted_address_replacements’, function( $replacements, $args ){
    $replacements[‘{landmark}’] = $args[‘landmark’];
    return $replacements;
    }, 10, 2 );

    // custom field reordering – I have set it up for my country IN – India — you can modify as per your wish.
    add_filter( ‘woocommerce_localisation_address_formats’, ‘woocommerce_custom_address_format’, 20 );
    function woocommerce_custom_address_format( $formats ) {
    $formats[ ‘IN’ ] = “{name}\n{address_1},\n,{address_2},\n{landmark},\n{city} – {postcode}\n,{state} \n{country}”;
    return $formats;
    }

    Incase, you don’t know – you have to place this in the functions.php file of your theme holder.

    By the way, it wasn’t a trouble.
    Happy Coding.!

    Thread Starter ivenkatdev

    (@ivenkatdev)

    I was able to figure out it. Thanks!

    To anyone met with the same issue – uncheck the below to avoid the presence of custom fields in respective pages/sections.
    ☐ Display in Emails
    ☐ Display in Order Detail Pages

    Thread Starter ivenkatdev

    (@ivenkatdev)

    Thanks for your quick reply. The plugin is awesome and you are doing a great work.!

    I referred the links and make it worked out, however i am facing issues on 2 things which requires your advise.

    The nelwy added custom field appears correclty on the address section and additionally keeps appearing on the order detail page as here: https://postimg.cc/5YbVgnvT
    Also on the email it shows as a separate section as indicated here: https://postimg.cc/94G2yv4y.

    Please advise how to avoid the custom fields on both email and order page without disturbing the custom-field present in the address section.

Viewing 3 replies - 1 through 3 (of 3 total)