5 Programming Questions and Solutions Regarding WooCommerce Checkout Page

January 13, 2017,
5 Programming Questions and Solutions Regarding WooCommerce Checkout Page
It has been seen that a lot of programmers are facing various issues while coding WooCommerce checkout page. We have selected the top 5 questions asked by programmers in various platforms and will provide best possible answers in this article. As these answers are given by community members on and not represented us.  Let have a look on five hot WooCommerce checkout fields questions and their best solutions.
Important Note: These are the answers provided by the community and best rated by the community as well. 

Question No. 1

Ajax Callback in WooCommerce Checkout A user has asked for JavaScript callback for ajax action when changing the shipping on WooCommerce checkout.

Answer

Use this code
jQuery( document ).on( 'updated_checkout', function() {

    console.log( 'here goes a action' );

} );

Question No. 2

How to override WooCommerce checkout fields? A user has created the fields in WooCommerce checkout page which labeled as “po_number” and he wants to display PO Numbers only for the user role “distributor”. As he is using WordPress 4.5.1 / WooCommerce 2.5.5. He has placed in child theme's functions.php is
function custom_override_checkout_fields( $fields ) {

    if ( ! current_user_can( 'distributor' ) && isset( $fields['billing']['po_number'] ) ) {
        unset($fields['billing']['po_number']);

    }
     return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

Answer

The function current_user_can() is associated with the capabilities of user roles but not for the recognition of their roles. That is why didn’t work in code. However, you can use this conditional function.
function is_user_role( $role, $user_id = null ) {
    if ( is_numeric( $user_id ) ) {
        $user = get_userdata( $user_id );
    } else {
        $user = wp_get_current_user();
    }
    if ( empty( $user ) ) {
        return false;
    }
    if ( in_array( $role, (array) $user->roles ) == 1) {
        return true;
    } else {
        return false;
    }
}
After that, you have to use this function in your code. :
function custom_override_checkout_fields( $fields ) {
    if ( !is_user_role( 'distributor' ) && isset( $fields['billing']['po_number'] ) ) {
        unset($fields['billing']['po_number']);
    }
        return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );

Question No. 3

How to trim the WooCommerce checkout page? A user has WooCommerce based e-store. After WP update he is facing some issue in his checkout page and wants to trim the checkout page. He also wants to hide some unnecessary options on the checkout page for users.

Answer:

Use this code to theme function.php
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {
    unset($fields['billing']['billing_country']);
    unset($fields['billing']['billing_address_2']);
    unset($fields['order']['order_comments']);
    return $fields;
}
Plus use this custom CSS code
.checkout-group h3 {
    display:none;
}

Question No. 4

How to display messages on the checkout page for the specific category of products? A user wants to display the message on the checkout page to customers if they will buy specific category of products. He is using the following code but the message is displaying on the checkout page for all the products.
add_action( 'woocommerce_after_checkout_form', 'allclean_add_checkout_content', 12 );
function allclean_add_checkout_content() {
echo '<div class="checkoutdisc">Custom message appears here fine.</div>';
}

Answer:

Please check that you have the products in the cart with the special category. When any item with the special category in a cart will be in the cart, the message will have appeared.  Use this code
add_action( 'woocommerce_after_checkout_form', 'allclean_add_checkout_content', 12 );
function allclean_add_checkout_content() {
    // set your special category name, slug or ID here:
    $special_cat = 'special_category';
    $bool = false;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $item = $cart_item['data'];
        if ( has_term( $special_cat, 'product_cat', $item->id ) )
            $bool = true;
    }
    // If the special cat is detected in one items of the cart
    // It displays the message
    if ($bool)
        echo '<div class="checkoutdisc">This is Your custom message displayed.</div>';
}
You can also do this by using an array of product IDs rather than product category. Here is the code to do this.
add_action( 'woocommerce_after_checkout_form', 'allclean_add_checkout_content', 12 );
function allclean_add_checkout_content() {
    // set your products IDs here:
    $product_ids = array( 31, 68, 87, 124);
    $bool = false;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $item = $cart_item['data'];
        if ( in_array( $item->id, $product_ids ) )
            $bool = true;
    }
    // If the special cat is detected in one items of the cart
    // It displays the message
    if ($bool)
        echo '<div class="checkoutdisc">This is Your custom message displayed.</div>';
}
You have to place this code in function.php file of child theme or plugin file

Question No.   5

How to redirect the customer to my account page from WooCommerce checkout page if he is not signed in? A user wants to redirect his customers to login page from WooCommerce checkout page if they are not signed in. He has used the following code but not worked.
<?php 
function my_page_template_redirect()
{
    $checkouturl = home_url( '/checkout/' );
    if( is_page($checkouturl) && ! is_user_logged_in() )
    {
        wp_redirect( home_url( '/my-account/' ) );
        exit();
    }
}
add_action( 'template_redirect', 'my_page_template_redirect' );
?>

Answer

Please put this code into theme function.php file to redirect customers to my account page from checkout page, who are not logged in. 
function wpse_131562_redirect() {
    if (
        ! is_user_logged_in()
        && (is_cart() || is_checkout())
    ) {
        // feel free to customize the following line to suit your needs
        wp_redirect(wp_login_url());
        exit;
    }
}
add_action('template_redirect', 'wpse_131562_redirect');

Download WooCommerce Checkout Field Editor additional checkout fields