Make Fields Required or Optional on the WooCommerce Checkout Page

Hello Fiends, Today I received a message from my client she want to customized (Billing and Shipping) Checkout Page. She have some requirements as bellow mentioned.

Shipping Address Checked Automatic

When we use Default Shipping Address in Woo commerce setting then shipping address automatic checked then customer need to unchecked it otherwise filled their address two time. Here I am writing a line of code it will fixed this problem.

Goto –> Appearance –> Theme File Editor 

open function file add this code

/*Remove CHecked Automatic Shipping different address While you select default shipping address*/
add_filter( ‘woocommerce_ship_to_different_address_checked’, ‘__return_false’ );

Optional Email Address

By default email address is mandatory in Woo Commerce Billing form But My client needed its optional. Here I am also writing code.

Goto –> Appearance –> Theme File Editor 

open function file add this code

/*optional Email in Billing*/
add_filter( ‘woocommerce_billing_fields’, ‘ts_require_wc_company_field’);
function ts_require_wc_company_field( $fields ) {
$fields[‘billing_email’][‘required’] = false;
return $fields;
}

The code snippet below  for Phone enables you to do the same:

add_filter( ‘woocommerce_billing_fields’, ‘ts_unrequire_wc_phone_field’);
function ts_unrequire_wc_phone_field( $fields ) {
$fields[‘billing_phone’][‘required’] = false;
return $fields;
}

You can edit filed name which you want to make required or optional. filed name list is here

Below are the HTML names of all the fields on the Checkout page. The names are indicative of which field they correspond to. You can refer to the snippets above to change any of these fields into an optional or a required field, depending on the use case.

Billing Fields

Shipping Fields

Order Fields

With just a few lines of code, you can change any field on your Checkout page into an optional or a mandatory field.

 

Display Back Button on Product Page in WooCommerce

My client call me he want to show Back button on product page on their Woocommerce website I use a simple code to fixed this problem.

Goto Function.php file in Theme File Editor and paste below mentioned code.

add_action(‘woocommerce_before_main_content’, ‘backbutton’, 5);
function backbutton() {
echo ‘ <button type=”button” onclick=”history.back();”> Back </button> ‘;
}

Then click on save button. Now check Single product page. You will see a Back button on this page. It is also working on mobile also.

Use HTML code to make Back button

You can try below code to display back button on any web page.

<p> <INPUT TYPE=”button” VALUE=”Back” onClick=”history.go(-1);”></p>

Or use Java Script code that supported by Browsers.

<a href = "javascript:history.back()">Back to previous page</a>

 

How to show a different homepage on mobile devices

Today I have received call from my client he want to display different layout of website home page on Mobile. I have created this function for him.

If you want to show different page as home page of mobile. use blow mentioned code in function.php 

//* Redirect homepage on mobile*/
add_action( ‘wp_head’, ‘wps_params’, 10 );
function wps_params() {
?>

<script>
if (window.location.pathname == ‘/’ && jQuery(window).width() <= 480) {
window.location = “/about-us/”;
}
</script>
<?php
}

Note:- If wordpress theme editor display any error use FTP or cpanel to edit theme file.

 

How to create a custom login/logout redirect in WordPress

Today I was creating a website when I need redirection options.  When user login then redirect to dashboard.

//REDIRECT TO A CUSTOM LINK AFTER LOGIN USER

function custom_login() {
wp_redirect( ‘https://offlearning.com/user-account/’ );
exit();
}
add_action(‘wp_login’, ‘custom_login’);

//REDIRECT TO A CUSTOM LINK AFTER LOG OUT USER:

function custom_logout(){
wp_redirect( ‘https://offlearning.com/my-account/’ );
exit();
}
add_action(‘wp_logout’,’custom_logout’);

Remove WooCommerce downloads tab

To remove WooCommerce downloads, add the code below into your theme’s functions.php file.

function custom_my_account_menu_items( $items ) {
     unset( $items['downloads'] );
     return $items;
}
add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );

 

Adding extra WooCommerce registration fields

To remove WooCommerce downloads, add the code below into your theme’s functions.php file.

//Add First Name and Last Name in Woocommerce Reg. Page
function custom_extra_register_fields() { ?>
<p class=”form-row form-row-wide”>
<label for=”reg_billing_first_name” class=”screen-reader-only”><?php _e( ‘First name’, ‘woocommerce’ ); ?><span class=”required”>*</span></label>
<input type=”text” class=”input-text” name=”billing_first_name” id=”reg_billing_first_name” value=”<?php if ( ! empty( $_POST[‘billing_first_name’] ) ) esc_attr_e( $_POST[‘billing_first_name’] ); ?>” placeholder=”First Name*” />
</p>
<p class=”form-row form-row-wide”>
<label for=”reg_billing_last_name” class=”screen-reader-only”><?php _e( ‘Last name’, ‘woocommerce’ ); ?><span class=”required”>*</span></label>
<input type=”text” class=”input-text” name=”billing_last_name” id=”reg_billing_last_name” value=”<?php if ( ! empty( $_POST[‘billing_last_name’] ) ) esc_attr_e( $_POST[‘billing_last_name’] ); ?>” placeholder=”Last Name*” />
</p>
<div class=”clearfix”></div>
<?php
}
add_action( ‘woocommerce_register_form_start’, ‘custom_extra_register_fields’ );

function custom_validate_extra_register_fields( $username, $email, $validation_errors ) {
if ( isset( $_POST[‘billing_first_name’] ) && empty( $_POST[‘billing_first_name’] ) ) {
$validation_errors->add( ‘billing_first_name_error’, __( ‘<strong>Error</strong>: First name is required!’, ‘woocommerce’ ) );
}
if ( isset( $_POST[‘billing_last_name’] ) && empty( $_POST[‘billing_last_name’] ) ) {
$validation_errors->add( ‘billing_last_name_error’, __( ‘<strong>Error</strong>: Last name is required!.’, ‘woocommerce’ ) );
}
return $validation_errors;
}
add_action( ‘woocommerce_register_post’, ‘custom_validate_extra_register_fields’, 10, 3 );

function custom_save_extra_register_fields( $customer_id ) {

if ( isset( $_POST[‘billing_first_name’] ) ) {
// First name input for WordPress
update_user_meta( $customer_id, ‘first_name’, sanitize_text_field( $_POST[‘billing_first_name’] ) );
// First name input for WooCommerce
update_user_meta( $customer_id, ‘billing_first_name’, sanitize_text_field( $_POST[‘billing_first_name’] ) );
}
if ( isset( $_POST[‘billing_last_name’] ) ) {
// Last name input for WordPress
update_user_meta( $customer_id, ‘last_name’, sanitize_text_field( $_POST[‘billing_last_name’] ) );
// Last name input for WooCommerce
update_user_meta( $customer_id, ‘billing_last_name’, sanitize_text_field( $_POST[‘billing_last_name’] ) );
}

}
add_action( ‘woocommerce_created_customer’, ‘custom_save_extra_register_fields’ );

 

Leave a Reply

Your email address will not be published. Required fields are marked *