How to make all fields optional in WooCommerce My account edit addresses only?
How to disable the required notice:
And the required html:
How to remove required? Can you help me?
To make all My account > addresses fields optional use the following:
// Make all My account addresses fields optional
add_filter( 'woocommerce_default_address_fields' , 'filter_my_account_addresses_fields', 999 );
add_filter( 'woocommerce_billing_fields', 'filter_my_account_addresses_fields', 999 );
function filter_my_account_addresses_fields( $fields ) {
// Only on My account edit addresses
if ( is_wc_endpoint_url( 'edit-address' ) ) {
// Loop through existing fields
foreach( $fields as $field_key => $field_data ) {
// if they are required
if( $fields[$field_key]['required'] ) {
// Make them optional
$fields[$field_key]['required'] = false;
}
}
}
return $fields;
}
// Optionaly remove ("optional)" text from My account addresses fields
add_filter( 'woocommerce_form_field' , 'remove_account_addresses_optional_fields_label', 10, 4 );
function remove_account_addresses_optional_fields_label( $field, $key, $args, $value ) {
// Only on My account edit addresses
if ( is_wc_endpoint_url( 'edit-address' ) ) {
$optional = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
$field = str_replace( $optional, '', $field );
}
return $field;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
works for option on checkout page "kurumsal"
works for option on checkout page "bireysel"
my account > addresses - does not select and hide option and does not work
my account > addresses - does not select and hide option and does not work
you can understand in the picture
Related
Need to make an company name input read only in the checkout of WooCommerce and also in the My Account addresses (/my-account/edit-address/billing/) read only. This code below is working to make the email address read only and the company name on checkout but is not creating a read only field in Company Name in (/my-account/edit-address/billing/). I have this code in funtions.php. I am not sure if there is simpler way of doing these things with the code provided.
function wc_remove_checkout_fields( $fields ) {
// Billing fields
// unset( $fields['billing']['billing_company'] );
unset( $fields['billing']['billing_email'] );
// Shipping fields
unset( $fields['shipping']['shipping_company'] );
// Order fields
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'wc_remove_checkout_fields' );
add_filter( 'woocommerce_checkout_fields' , 'custom_checkout_fields' );
function custom_checkout_fields( $fields ) {
$fields['billing']['billing_company']['custom_attributes']['readonly'] = 'readonly';
return $fields;
}
add_action( 'woocommerce_after_edit_account_form', 'disable_edit_email_address' );
function disable_edit_email_address( ) {
$script = '<script type="text/javascript">'.
'var account_email = document.getElementById("account_email");'.
'if(account_email) { '.
' account_email.readOnly = true; '.
' account_email.className += " disable-input";'.
'}'.
'</script>';
echo $script;
}
add_action( 'woocommerce_save_account_details_errors', 'prevent_user_update_email', 10, 2 );
function prevent_user_update_email( &$error, &$user ){
$current_user = get_user_by( 'id', $user->ID );
$current_email = $current_user->user_email;
if( $current_email !== $user->user_email){
$error->add( 'error', 'E-mail cannot be updated.');
}
add_action( 'wp_footer' , 'make_billing_company_field_readonly' );
function make_billing_company_field_readonly(){
// Only for account fields
if( is_account_page() ): ?>
<script type='text/javascript'>
jQuery(function($){
$('form.edit-account input#billing_company').prop('readonly', true );
});
</script>
<?php endif;
}
}
wc-address-i18n-js will be enqueued in the my-account edit address page. We can add inline JS using the JS id as shown below. The WP function wp_add_inline_script() can be used in this context.
function wc_myaccount_edit_address() {
if (is_account_page()):
if (!wp_script_is('jquery', 'done')) {
wp_enqueue_script('jquery');
}
wp_add_inline_script('wc-address-i18n', 'jQuery(document).ready(function($){$("#billing_company").prop("readonly", true );});');
endif;
}
add_action('wp_enqueue_scripts', 'wc_myaccount_edit_address');
I am currently using the following code in the functions.php file.
add_action( 'woocommerce_after_shipping_rate', 'action_after_shipping_rate', 20, 2 );
function action_after_shipping_rate ( $method, $index ) {
// Targeting checkout page only:
if( is_cart() ) return; // Exit on cart page
if( 'flat_rate:3' ) {
echo __("<p>Delivery will take place tomorrow</br> morning between 8-12</p>");
}
}
Now I would like to get the customer postcode, to then add it to my pre-existing code.
Someone who can help me with this?
You could use WC()->customer->get_shipping_postcode() to add the postcode
So you get:
function action_woocommerce_after_shipping_rate( $method, $index ) {
// Targeting checkout page only
if ( is_cart() ) return;
// Get shipping postcode
$shipping_postcode = WC()->customer->get_shipping_postcode();
// Compare
if ( $method->get_id() === 'flat_rate:3' ) {
// Output
echo '<p>' . __( 'My text + zipcode = ', 'woocommerce') . $shipping_postcode . '</p>';
}
}
add_action( 'woocommerce_after_shipping_rate', 'action_woocommerce_after_shipping_rate', 10, 2 );
I want to add some meta data to the order item in WooCommerce.
These meta fields are for internal use only and shouldn't be visible.
We have some extra fields in the product like an extra fee. I want to use that fee later to work with after I export the orders.
I found a very good answer here: https://stackoverflow.com/a/41988701/1788961
add_action('woocommerce_checkout_create_order_line_item', 'add_custom_hiden_order_item_meta_data', 20, 4 );
function add_custom_hiden_order_item_meta_data( $item, $cart_item_key, $values, $order ) {
// Set user meta custom field as order item meta
if( $meta_value = get_user_meta( $order->get_user_id(), 'billing_enumber', true ) )
$item->update_meta_data( 'pa_billing-e-number', $meta_value );
}
But with this example, the content from the meta fields will appear in the order details for the customer.
Is there a way to make these fields only visible in the backend and usable for internal functions?
Updated
The simple way set any meta value as hidden order item meta data only visible on admin Order edit pages is to add an underscore at the beginning of the meta key like:
add_action('woocommerce_checkout_create_order_line_item', 'add_custom_hiden_order_item_meta_data', 20, 4 );
function add_custom_hiden_order_item_meta_data( $item, $cart_item_key, $values, $order ) {
// Set user 'billing_enumber' custom field as admin order item meta (hidden from customer)
if( $meta_value = get_user_meta( $order->get_user_id(), 'billing_enumber', true ) )
$item->update_meta_data( '_billing_enumber', $meta_value );
}
Then to have a clean label name for this meta key on admin order items, you can use:
add_filter('woocommerce_order_item_display_meta_key', 'filter_wc_order_item_display_meta_key', 20, 3 );
function filter_wc_order_item_display_meta_key( $display_key, $meta, $item ) {
// Set user meta custom field as order item meta
if( $meta->key === '_billing_enumber' && is_admin() )
$display_key = __("Billing E Number", "woocommerce" );
return $display_key;
}
This code goes in function.php file of your active child theme (or cative theme). Tested and works.
add_action('woocommerce_add_order_item_meta','mau_add_values_to_order_item_meta',1,2);
if(!function_exists('mau_add_values_to_order_item_meta'))
{
function mau_add_values_to_order_item_meta($item_id, $values)
{
global $woocommerce,$wpdb;
$postcode = $values['postcode'];
$extend_date_delivery = $values['extend_date_delivery'];
$extend_date_collection = $values['extend_date_collection'];
$pro_rental_type_choose = $values['pro_rental_type_choose'];
if(!empty($postcode))
{
wc_add_order_item_meta($item_id,'postcode',$postcode);
}
if(!empty($extend_date_delivery))
{
wc_add_order_item_meta($item_id,'extend_date_delivery',$extend_date_delivery);
}
if(!empty($extend_date_collection))
{
wc_add_order_item_meta($item_id,'extend_date_collection',$extend_date_collection);
}
if(!empty($pro_rental_type_choose))
{
wc_add_order_item_meta($item_id,'pro_rental_type_choose',$pro_rental_type_choose);
}
}
}
add_filter( 'woocommerce_order_item_display_meta_key', 'mau_change_shipping_note_title', 20, 3 );
function mau_change_shipping_note_title( $key, $meta, $item ) {
if ( 'postcode' === $meta->key ) { $key = __( 'Postcode', 'your_textdomain'); }
if ( 'extend_date_delivery' === $meta->key ) { $key = __( 'Delivery Date', 'your_textdomain'); }
if ( 'extend_date_collection' === $meta->key ) { $key = __( 'Collection Date', 'your_textdomain'); }
if ( 'pro_rental_type_choose' === $meta->key ) { $key = __( 'Rental Type', 'your_textdomain'); }
return $key;
}
I am adding mandatory shipping phone to woocommerce checkout page with
add_filter( 'woocommerce_checkout_fields', 'add_shipping_phone_to_checkout_page' );
function add_shipping_phone_to_checkout_page( $fields ) {
$fields['shipping']['shipping_phone'] = array(
'label' => 'Phone',
'required' => true,
'class' => array( 'form-row-wide' ),
'priority' => 25,
);
return $fields;
}
then display it in admin order panel
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'shipping_phone_checkout_display_in_order_panel' );
function shipping_phone_checkout_display_in_order_panel( $order ){
echo '<p><b>Phone :</b> ' . get_post_meta( $order->get_id(), '_shipping_phone', true ) . '</p>';
}
and finally print it in email
add_action('woocommerce_email_customer_details','shipping_phone_display_in_order_email', 25, 4 );
function shipping_phone_display_in_order_email( $order, $sent_to_admin, $plain_text, $email ) {
$output = '';
$shipping_phone = get_post_meta( $order->id, '_shipping_phone', true );
if ( !empty($shipping_phone) )
$output = '<p><strong>' . __( "Phone:", "woocommerce" ) . '</strong> ' . $shipping_phone . '</p>';
echo $output;
}
All works as it should. I'd like to achieve 2 enhancements but I am unable to do:
Make the custom phone field editable in admin panel
In email, move the custom phone field value in shipping address block
Any help would be appreciated
You need to make some changes in your codeā¦ The following code will display the shipping phone field in:
Checkout
My Account > Address > Edit shipping address
Admin order edit pages
The code will also add the shipping phone to formatted displayed shipping address on emails shipping address section.
// display shipping phone in checkout and my account edit shipping address
add_filter( 'woocommerce_shipping_fields', 'add_shipping_phone_field' );
function add_shipping_phone_field( $fields ) {
$fields['shipping_phone'] = array(
'label' => __('Phone (Shipping)'),
'required' => true,
'class' => array( 'form-row-wide' ),
'priority' => 25,
);
return $fields;
}
// Editable field on admin order edit pages inside edit shipping section
add_filter( 'woocommerce_admin_shipping_fields' , 'add_order_admin_edit_shipping_phone' );
function add_order_admin_edit_shipping_phone( $fields ) {
// Include shipping phone as editable field
$fields['phone'] = array( 'label' => __("Shipping phone"), 'show' => '0' );
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{phone}";
}
}
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['{phone}'] = ! empty($args['phone']) ? $args['phone'] : '';
return $replacements;
}
// Add the shipping phone value to be displayed on email notifications under shipping address
add_filter( 'woocommerce_order_formatted_shipping_address', 'add_shipping_phone_to_formatted_shipping_address', 100, 2 );
function add_shipping_phone_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['phone'] = $order->get_meta('_shipping_phone');
}
return $shipping_address;
}
// Remove double billing phone from email notifications (and admin) under billing address
add_filter( 'woocommerce_order_formatted_billing_address', 'remove_billing_phone_from_formatted_billing_address', 100, 2 );
function remove_billing_phone_from_formatted_billing_address( $billing_address, $order ) {
unset($billing_address['phone']);
return $billing_address;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
For billing custom fields, you will replace the hooks:
woocommerce_shipping_fields by woocommerce_billing_fields
woocommerce_admin_shipping_fields by woocommerce_admin_billing_fields
woocommerce_order_formatted_shipping_address by woocommerce_order_formatted_billing_address
(don't use the last function).
For the front endpoints:
On order received (thank you), order-pay, and myaccount / order-view, you will have to override via your active theme the template order/order-details-customer.php.
You will add inside the html tag <address> after line 52 the following:
<?php if ( $shipping_phone = $order->get_meta('_shipping_phone') ) : ?>
<p class="woocommerce-customer-details--phone"><?php echo esc_html( $shipping_phone ); ?></p>
<?php endif; ?>
On admin side, the shipping phone is displayed and editable:
On order-view, order-received and email notifications, the shipping phone is displayed at the end of the shipping address section:
Updated:
I'm building a WooCommerce site where the user selects a series of options from dropdowns in the single product page which then display in the cart page and, thanks to help I've received on here, in the checkout as well. The options selected also influence the price of the product. I start with two product variations: 'Print' and 'Original' (the site is selling antique maps).
Everything works fine up till the checkout where all the order details display correctly but, after having placed the order, the details don't appear on the 'order received' screen under 'order details' nor do they appear in the customer confirmation email.
To give some background, the different variations are selected using jQuery, and added to hidden fields a per example below:
$( ":root" ).find("#mapchest-custom-fields").append("<input type='hidden'
name='test' value='wibble'>");
...and these hidden fields are then referenced to add the details to the cart in the following manner:
add_filter('woocommerce_add_cart_item_data','add_custom_field_data', 20,3);
function add_custom_field_data($cart_item_data, $product_id, $variation_id)
{
if(isset($_REQUEST['test']) && ! empty( 'test' )) { // not
$mc_test = sanitize_text_field($_POST['test']);
$cart_item_data['custom_data']['test'] = array(
'label' => 'Test',
'value' => $mc_test
);
}
if(isset($_REQUEST['original_map_vendor_details']) && ! empty(
'original_map_vendor_details' )) {
$mc_original_map_size =
sanitize_text_field($_REQUEST['original_map_vendor_details']);
$cart_item_data['custom_data']['original_map_vendor_details'] =
array(
'label' => 'Vendor',
'value' => $mc_original_map_size
);
}
// process above repeated for other fields
return $cart_item_data;
}
The details are displayed in the cart and checkout using the following function:
add_filter('woocommerce_get_item_data','wdm_add_item_meta',10,2);
function wdm_add_item_meta($cart_data, $cart_item)
{
$custom_items = array();
if( !empty( $cart_data ) )
$custom_items = $cart_data;
if( isset( $cart_item['custom_data'] ) ) {
foreach( $cart_item['custom_data'] as $key => $custom_data ){
if( $key != 'key' ){
$custom_items[] = array(
'name' => $custom_data['label'],
'value' => $custom_data['value'],
);
}
}
}
return $custom_items;
}
What I want to do, as I say is have the details display in the Order Received page and the emails but I can't make it work. I know that for emails I need to hook it to one of the email hooks but I don't know how to access the data sent to the cart in the function above.
I've tried adding using the woocommerce_checkout_create_order_line_item hook to along these lines:
add_action( 'woocommerce_checkout_create_order_line_item',
'add_custom_order_line_item_meta', 20,4 );
function add_custom_order_line_item_meta($item, $cart_item_key, $values,
$order)
{
if( array_key_exists('test', $values['custom_data']) ){
$item->update_meta_data( 'Test', $values['custom_data']['test'] );
}
}
...but whilst I can see the data if I var_dump it in the email like this:
add_action('woocommerce_email_customer_details',
'add_custom_checkout_field_to_emails_notifications', 25, 4 );
function add_custom_checkout_field_to_emails_notifications( $order,
$sent_to_admin, $plain_text, $email ) {
var_dump($order);
}
So, in summary, I have the data working and displaying up to the point of the checkout. After that, I want it to display in customer confirmation emails and on the 'order received' page but I'm having trouble accessing the data. Having looked through other questions on the same subject i would have thought that this would happen automatically regarding the order received page but it doesn't. I suspect there's a stage missing in the code but I can't work out what it should be.
Any tips as to what I'm doing wrong here?
Thanks in advance.
ps. I've now managed to display the fields in the confirmation email (after a fashion) using the following functions:
add_action( 'woocommerce_checkout_create_order_line_item',
'add_custom_order_line_item_meta', 20,4 );
function add_custom_order_line_item_meta($item, $cart_item_key, $values,
$order)
{
if ( isset( $values['custom_data'] ) ) {
$item->update_meta_data( __('The Custom Data', 'woocommerce'),
$values['custom_data'] );
}
}
and
add_action('woocommerce_email_customer_details',
'add_custom_checkout_field_to_emails_notifications', 25, 4 );
function add_custom_checkout_field_to_emails_notifications( $order,
$sent_to_admin, $plain_text, $email ) {
// var_dump($order);
foreach( $order->get_items() as $item_id => $item ){
$custom_data = $item->get_meta( 'The Custom Data' );
foreach( $custom_data as $key => $value ){
foreach( $value as $key1 => $value1 ){
$output = '';
$output .= '<span class="text">' . $value1 . '</span>';
echo $output;
}
echo "<br>";
}
echo "<br><br>";
// var_dump($custom_data );
}
'</strong> <span class="text">' . $order->get_data() . '</span></div>';
}
but this is a hacky solution and doesn't address the underlying problem of why the information isn't appearing in the order received page or directly in the order line items in the email.
Okay, I've worked this out. I'm putting the answer here for the benefit of anyone else having the same problem. Basically, my process was missing a stage. In order to achieve the above you do as follows:
Define the value you wish to pass as meta data. In my own case I used a hidden field but this can equally be set with a text input, a dropdown or other input field. In my case I used jquery to append this to an empty div with id 'mapchest-custom-fields' which I hooked into the process before the cart button. It can equally be set with a static value.
<?php
function define_container_div() {
?>
<div id="mapchest-custom-fields"></div>
<?php
}
add_action( 'woocommerce_before_add_to_cart_button', 'define_container_div', 20 );
?>
...jQuery code to append the value. Value can be dynamic as well:
$( ":root" ).find("#mapchest-custom-fields").append("<input type='hidden' name='test' value='wibble'>");
Next you add the value to your cart item data:
function add_values_to_cart_item_data( $cart_item_data, $product_id, $variation_id )
{
if(isset($_POST['test']) && ! empty( 'test' )) {
$test = filter_input( INPUT_POST, 'test' );
$cart_item_data['test'] = $test;
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'add_values_to_cart_item_data', 10, 3);
Next, you display the value in your cart:
function display_data_in_cart( $item_data, $cart_item ) {
$item_data[] = array(
'key' => __( 'Test', 'mapchest' ),
'value' => wc_clean( $cart_item['test'] ),
);
return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'display_data_in_cart', 10, 2 );
And finally, you add the data to your order items:
function add_data_to_order_items( $item, $cart_item_key, $values, $order ) {
$item->add_meta_data( __( 'Test', 'mapchest' ), $values['test'] );
}
add_action( 'woocommerce_checkout_create_order_line_item', 'add_data_to_order_items', 10, 4 );
The above process works for me. It displays the custom data in the cart and in the checkout and persists it through to the 'Order Received' page archived orders and the confirmation email (not checked other emails yet).
Thanks to https://iconicwp.com/blog/add-custom-cart-item-data-woocommerce/ for explaining this process to me.