Add additional fields to Admin User under Customer shipping section in Woocommerce - php

In woocommerce, after creating and registering some custom account fields in a custom php file using the following code:
<label for="reg_shipping_phone"> ... </label>
<input class="..." id="reg_shipping_phone" name="shipping_phone"
type="tel" pattern="[a-z0-9.-]{6,40}"
title=" ... " placeholder=" ... "
value="<?php esc_attr_e( $_POST['shipping_phone'] ); ?>" />
and in my theme's function.php file:
//save on admin/&frontend on change
add_action( 'personal_options_update', 'woo_save_extra_register_fields' );
add_action( 'woocommerce_save_account_details', 'woo_save_extra_register_fields' );
// Save extra registration fields data on from
add_action( 'woocommerce_created_customer', 'woo_save_extra_register_fields' );
function woo_save_extra_register_fields( $customer_id )
{
if( isset( $_POST['shipping_phone'] ) ) {
update_user_meta( $customer_id, 'shipping_phone', $_POST['shipping_phone'] );
}
}
I would like to implement this "Shipping phone" field in the correct position on Backend User profile pages inside the "Customer's shipping address" section table
How can I insert additional fields in the "Customer's shipping address" section table?
For example I have tried the following *(but 11,5 is wrong...)*:
add_action( 'show_user_profile', 'extra_profile_fields',***11,5*** );
add_action( 'edit_user_profile', 'extra_profile_fields',***11,5*** );
function extra_profile_fields( $user ) { ?>
<h3>NAME OF TABLE</h3>
<table class="form-table">
<tr>
<th><label for="shipping_phone">SHIPPING PHONE</label></th>
<td>
<input type="text" name="shipping_phone" id="shipping_phone" value="<?php echo esc_attr( $user->shipping_phone); ?>" class="regular-text" />
</td>
</tr>
>
</table>
<?php }
Any help is appreciated.

If you want to include your custom "Shipping phone" field in Backend User profile under "Customer's shipping address" section, you will use the following instead:
add_filter( 'woocommerce_customer_meta_fields', 'filter_add_customer_meta_fields', 10, 1 );
function filter_add_customer_meta_fields( $args ) {
$args['shipping']['fields']['shipping_phone'] = array(
'label' => __( 'Shipping phone', 'woocommerce' ),
'description' => '',
);
return $args;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
This little code snippet will do it all. If you change the value in this "Shipping phone" field, it will be updated without needing any additional code.

Related

Wordpress - add woocommerce billing_phone field into user-new.php and save it into database and my account

I'm facing a problem. I need to create some woocommerce customer role accounts manualy from the wp-admin. What I need to do is to add a woocommerce billing_phone number into the form which will be saved into the database and added to woocommerce my account phone field automatically. I've tried to do it with the code bellow but it did not help. Any ideas how could I make it?
I need to add it to user-new.php ...I know there is possibility to add the billing_phone after I create the user account, but I want to save time by searching for the account and doing things twice so I want to save time by adding this field directly into the account creation process from wp-admin.
Thank you in advance!
Code I used but did not help:
add_action( 'user_new_form', function( $type ){
if( 'add-new-user' !== $type )
return;
echo '<input type="text" name="billing_phone" id="billing_phone" value=""/>';
});
This is actually very easy to do. So the way WooCommerce autofills those fields is if by fetching the actually billing and shipping addresses for the user. If we manage so save the users contact number on regster hook we can make this work.
We will first create a field to save mobile data.
add_action( 'user_new_form', 'bks_add_mobile_field' );
add_action( 'edit_user_profile', 'bks_add_mobile_field' );
add_action( 'show_user_profile', 'bks_add_mobile_field' );
function bks_add_mobile_field( $user ) { ?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="mobile"><?php _e("Mobile Number"); ?></label></th>
<td>
<input type="text" name="mobile" id="mobile" value="<?php echo esc_attr( get_the_author_meta( 'mobile', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter mobile."); ?></span>
</td>
</tr>
</table>
<?php }
Now we would save this mobile in both the fields mobile and billing_mobile.
add_action( 'user_register', 'bks_save_mobile_field' );
function bks_save_mobile_field($user_id) {
// You can maybe add checks here whch would determine if the users role is customer
// or not or maybe validate the number.
if ( isset( $_POST['mobile'] ) ) {
update_user_meta($user_id, 'mobile', $_POST['mobile']);
update_user_meta($user_id, 'billing_phone', $_POST['mobile']);
}
}
So to put it all together add the following code in your functions.php file.
add_action( 'user_new_form', 'bks_add_mobile_field' );
add_action( 'show_user_profile', 'bks_add_mobile_field' );
add_action( 'edit_user_profile', 'bks_add_mobile_field' );
function bks_add_mobile_field( $user ) { ?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="mobile"><?php _e("Mobile Number"); ?></label></th>
<td>
<input type="text" name="mobile" id="mobile" value="<?php echo esc_attr( get_the_author_meta( 'mobile', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter mobile."); ?></span>
</td>
</tr>
</table>
<?php }
add_action( 'user_register', 'bks_save_mobile_field' );
function bks_save_mobile_field($user_id) {
// You can maybe add checks here whch would determine if the users role is customer
// or not or maybe validate the number.
if ( isset( $_POST['mobile'] ) ) {
update_user_meta($user_id, 'mobile', $_POST['mobile']);
update_user_meta($user_id, 'billing_phone', $_POST['mobile']);
}
}
This code is tested and it WORKS.

WooCommerce - Limit the zip code character limit on the CART page (NOT the checkout/account pages)

So I was able to limit the zip code length limit to 5 on the user's shipping/billing info on their account page as well as the checkout page using this code in my functions.php:
function my_wc_custom_billing_fields( $fields ) {
$fields['billing_postcode']['maxlength'] = 5;
return $fields;
}
add_filter( 'woocommerce_billing_fields', 'my_wc_custom_billing_fields' );
function my_wc_custom_shipping_fields( $fields ) {
$fields['shipping_postcode']['maxlength'] = 5;
return $fields;
}
add_filter( 'woocommerce_shipping_fields', 'my_wc_custom_shipping_fields' );
However, I still am able to enter past the limit in the zip code box on the CART page. You are able to change your address on the cart page and if you click "go to checkout" after typing more than 5 digits, it will allow the form to fill in with those additional digits on the checkout page. I want the zip code box on the CART page to work the same as it does on the other pages.
Thank you for taking to the time to read this. I hope to hear from someone soon!
------- >EDIT: FIGURED IT OUT!
Instead of creating a filter inside my functions.php, I directly edited the woocommerce/templates/cart/shipping-calculator.php file.
In this piece of code:
<?php if ( apply_filters( 'woocommerce_shipping_calculator_enable_postcode', true ) ) : ?>
<p class="form-row form-row-wide" id="calc_shipping_postcode_field">
<input type="text" class="input-text" value="<?php echo esc_attr( WC()->customer->get_shipping_postcode() ); ?>" placeholder="<?php esc_attr_e( 'Postcode / ZIP', 'woocommerce' ); ?>" name="calc_shipping_postcode" id="calc_shipping_postcode" />
</p>
<?php endif; ?>
I simply added "maxlength="5" in the line.
<input type="text" class="input-text" maxlength="5" value="
I hope this helps someone along the way! :)

add field in wordpress user-new.php through php

On the Users.php page I need an automatic filter to list only one type of user, without having to select an accurate filter that opens the page and is already with this filter
Try this inside your functions file:
Create a custom field, "Company Name". It is shown on both Add/Update user screens.
Hook “user_new_form”, will display the field on Add New User screen.
function custom_user_profile_fields($user){
if(is_object($user))
$company = esc_attr( get_the_author_meta( 'company', $user->ID ) );
else
$company = null;
?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="company">Company Name</label></th>
<td>
<input type="text" class="regular-text" name="company" value="<?php echo $company; ?>" id="company" /><br />
<span class="description">Where are you?</span>
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( "user_new_form", "custom_user_profile_fields" );
Save the custom field in database:
function save_custom_user_profile_fields($user_id){
# again do this only if you can
if(!current_user_can('manage_options'))
return false;
# save my custom field
update_user_meta($user_id, 'company', $_POST['company']);
}
add_action('user_register', 'save_custom_user_profile_fields');
add_action('profile_update', 'save_custom_user_profile_fields');

Add custom checkout fields below the terms and conditions in Woocommerce

I have built an e-commerce site using Woocommerce. I would like to add two more check boxes below the terms and conditions. I have searched everywhere for a working solution and the only thing that I found is a commercial plugin.
How to add custom checkout fields (2 checkboxes) below the terms and conditions programmatically?
Location of the terms and condition screenshot:
The 1st hooked function displays the 2 additional checkout fields
The 2nd hooked function will check that both checkboxes are "selected" to allow checkout, displaying a custom error notice if not…
The code:
add_action('woocommerce_checkout_before_terms_and_conditions', 'checkout_additional_checkboxes');
function checkout_additional_checkboxes( ){
$checkbox1_text = __( "My first checkbox text", "woocommerce" );
$checkbox2_text = __( "My Second checkbox text", "woocommerce" );
?>
<p class="form-row custom-checkboxes">
<label class="woocommerce-form__label checkbox custom-one">
<input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="custom_one" > <span><?php echo $checkbox1_text; ?></span> <span class="required">*</span>
</label>
<label class="woocommerce-form__label checkbox custom-two">
<input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="custom_two" > <span><?php echo $checkbox2_text; ?></span> <span class="required">*</span>
</label>
</p>
<?php
}
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['custom_one'] )
wc_add_notice( __( 'You must accept "My first checkbox".' ), 'error' );
if ( ! $_POST['custom_two'] )
wc_add_notice( __( 'You must accept "My second checkbox".' ), 'error' );
}
Code goes in function.php file of your active child theme (active theme).
Tested and works.

Saving the value of a custom field phone number in WooCommerce My account > Account details

Trying to add a field for Woocommerce billing_phone to the my-account/edit-account/. It is present within the update address pages but when adding to the form-edit-account.php it does not update.
Currently adding by following the same code as the other fields, such as first_name:
<input type="text" class="form-control woocommerce-Input woocommerce-Input--phone input-text" name="billing_phone" id="billing_phone" value="<?php echo esc_attr( $user->billing_phone ); ?>" />
Image of form with billing_phone being shown as a value:
But upon updating with new number it is not changed…
How can I make this being saved/updated?
You need to add a custom function hooked in woocommerce_save_account_details action hook:
add_action( 'woocommerce_save_account_details', 'my_account_saving_billing_phone', 10, 1 );
function my_account_saving_billing_phone( $user_id ) {
$billing_phone = $_POST['billing_phone'];
if( ! empty( $billing_phone ) )
update_user_meta( $user_id, 'billing_phone', sanitize_text_field( $billing_phone ) );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested on WooCommerce version 3+ and works. The data will be updated and displayed correctly.
Your complete billing phone field code in form-edit-account.php should be something like:
<?php function field_phone(){
$user = wp_get_current_user();
?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--phone input-text" name="billing_phone" id="billing_phone" value="<?php echo esc_attr( $user->billing_phone ); ?>" />
</p> <?php } ?>

Categories