How to change magento 2 admin password from database

As we know Magento database are very complex, so not easy to change Magento 2 admin password from database.
I am going to explain short technique how to change Magento 2 admin password from database.
Open admin_user table from database and paste this query on sql editor.
Please read below instruction before paste query.
Please make sure your admin username is admin or anything else, change username = ‘admin’ if admin username is not admin.
After fire this query your admin password will be set admin123. Replace admin123 if you want to set another.
Please make sure not included any space between random character and new password.

UPDATE mg_admin_user SET password = CONCAT(SHA2(‘abcdefghadmin123’, 256), ‘:abcdefgh:1’) WHERE username = ‘admin’

How to add month in current date using Php

This is useful when you are playing with date and time.This is very short solution for it.

Below example is showing how to add 1 month in current date.

Suppose current date is 2016-05-25 10:33:19 and new date should be 2016-06-25 10:33:19

$today = date("Y-m-d h:i:s");// current date
$datemonth = strtotime(date("Y-m-d h:i:s", strtotime($today)) . "+1 month"); 
$after_month = date('Y-m-d h:i:s', $datemonth);

Minimum Number Of Products Required For Per Order

A minimum number of products that have to be ordered at a time before allowing the customer to fully pay for his order and shipped. Change ’5’ to whatever works best for your needs.

How to use it
This is example for set minimum product is 5.

add_action( 'woocommerce_check_cart_items', 'spyr_set_min_num_products' );
function spyr_set_min_num_products() {
	// Only run in the Cart or Checkout pages
	if( is_cart() || is_checkout() ) {
		global $woocommerce;

		// Set the minimum number of products before checking out
		$minimum_num_products = 5;
		// Get the Cart's total number of products
		$cart_num_products = WC()->cart->cart_contents_count;

		if( $cart_num_products < $minimum_num_products ) {
			// Display our error message
	        wc_add_notice( sprintf( 'A Minimum of %s products is required before checking out.' 
	        	. '
Current number of items in the cart: %s.',
	        	$cart_num_products ),
	        'error' );

Add Custom Menu Link Via Code In WordPress

We can add our custom menu link any menu location via code. For this, we have “wp_nav_menu_items” filter which help us to add the custom menus.

add_filter( ‘wp_nav_menu_items’, ‘add_custom_nav_menus’, 10, 2 );

this filter returns two parameters, $items and $args.

$items is a variable which contains the menu list which we have set from wp-admin->menus.

$args is an array which contains the arguments data like menu name, menu link, menu order etc.
How to use this filter ?

add_filter( 'wp_nav_menu_items', 'add_custom_nav_menus', 10, 2 );
function add_custom_nav_menus($items, $args ) {
 global $wpdb;
   if(in_array($args->theme_location,array('primary') )) {
         $items .= '<li class=”menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children has_children”>';
         $items .= '<a href=”'.get_site_url().'/my-account/” >My Account</a>';
         $items .= '<p class=”dropdownmenu”></p>';
         $items .= '<ul role=”menu” class=”sub-menu”>';
         $items .= '<li class=”menu-item menu-item-type-custom menu-item-object-custom”>';
         $items .= '<a href=”‘.wp_logout_url().'” >Logout</a>';
         $items .= '</li>';
         $items .= '</ul>';
         $items .= '</li>';
         $items .= '<li class=”menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children has_children”>';
         $items .= '<a href=”‘.get_site_url().’/login/” >Login</a>';
         $items .= '<p class=”dropdownmenu”></p>';
         $items .= '<ul role=”menu” class=”sub-menu”&gt';
         $items .= '<li class=”menu-item menu-item-type-custom menu-item-object-custom”>';
         $items .= '<a href=”‘.get_site_url().’/register/” >Register</a>';
         $items .= '</li>';
         $items .= '</ul>';
         $items .= '</li>';
 return  $items;

in above code, I’m adding for custom menu links which are login, register, my account and log out.

Why am I using custom menu ?

I want that if the user is login then only my account and log out menu should be shown. and if a user is not logged in then only login and register menu should be shown.

this can be done via wp-admin because I can’t apply a logical condition on that.

now by this if(in_array($args->theme_location,array(‘primary’) )) condition i am adding custom menu only on primary menu. the keyword “primary” is not fixed it always changes as per your theme. you need to check the menu location in which you want to add the custom menu links.

now as you can in code, i am just appending the HTML code in $items variable and returning the $items variable in this function.

You can apply our own logic to adding the custom menu as per your need.

Thanks for your reading.

Add Custom Form Field on WooCommerce Checkout Page

Sometimes we want to add some other/extra information of the user on checkout. For example, we want to add a privacy policy checkbox or want to add a dropdown to select specific information etc.

So for this woocommerce provide us another useful filter which allows us to add the custom form field on the checkout page.

Logically we’ll have to use one filter and one action for this process:

add_filter( ‘woocommerce_checkout_fields’ , ‘custom_drop_down’);

This filter will display the customs form field on the checkout page.

This action will save the custom field data in the database.

How to use the filter ?

add_filter( 'woocommerce_checkout_fields' , 'custom_drop_down');

function custom_drop_down( $fields ) {

$fields['billing']['custom_drop_down'] = array(

'type' => 'select',
'options' => array(
'1' => 'Yes',
'0' => 'No',


'label' => __('Custom Dropdown', 'woocommerce'),
'required' => false,
'class' => array('custom_dropdown'),
'clear' => true


return $fields;


woocommerce_checkout_fields’ filter returns a parameter $fields which contain all the checkout page fields.

$fields is an array so we add new custom field value in this array and pass the args in this.

type => it define the field type e.g. text,select,text area etc.
options => it is used for select type only.
label => if define the field label.
required => it defines the field required a condition.
class => if define the field classes.

How to use action to store the custom field data ?

add_action( 'woocommerce_checkout_update_order_meta', 'custom_update_order_meta' );
function custom_update_order_meta( $order_id ) {

if ( $_POST[ ‘custom_dropdown’ ] )
update_post_meta( $order_id, 'custom_dropdown', esc_attr( $_POST[ 'custom_dropdown' ] ) );


as you can see the code. in that function, we’re checking the custom_dropdown data exist in post data or not ? if it exists then we update the post meta and store the custom field data in post meta table.

Thanks for reading.

Add Fee In Woocommerce Cart

If you want to apply extra fee in the cart then woocommerce provide an action for applying a fee on the cart.

The fee can be used as shipping fee, installation fee or whatever as per your requirement.

add_action( ‘woocommerce_cart_calculate_fees’, ‘add_extra_cart_fee’ ,10,1 );
This action calculates the total fee and adds into the cart amount.


This woocommerce function is used to add the fee to woocommerce cart.

How to use it ?

	     add_action( 'woocommerce_cart_calculate_fees', 'add_extra_cart_fee' ,10,1 );
    function add_extra_cart_fee( $cart_object ) {

    global $woocommerce;
    $spfee = 14.00; // initialize special fee
    $woocommerce->cart->add_fee( 'Extra Tax Fee', $spfee, true, 'standard' );


The code is very simple you have to define a global $woocommerce variable and with the help of this variable apply the fee to the cart.

You’ve to specify fee name/label and fee amount, another parameter is optional.

I am just adding 14.00 fee for every product.

if you want to apply the fee for special category or product then you can use $cart_object variable. it contains all the cart products.

using this “foreach ( $cart_object->cart_contents as $key => $value )” you can get the all products by $value[‘product_id’] and can apply your own logic.

For example, if the product Id is 45 then only apply the 23.00 fee or is product category id is 5 then apply 2.00.

you can create own logic and apply the fee.

also, you can specify the fee for each product using a loop.

Update Order Review Via Ajax On WooCommerce Checkout Page

Many Times we need to apply some custom condition for calculating the cart total. For example, applying a different type of shipping and calculating the new cart total and showing in order review.

But you want to do it via AJAX. it means you don’t want to reload the checkout page.

For this, there is an action “woocommerce_update_order_review” which we have to use with WooCommerce Ajax Call.

Here is the code :


function custom_checkbox_checker () 
		if ( is_checkout() ) 
			wp_enqueue_script( 'jquery' ); ?>
			jQuery(document).ready( function (e) 
				var $ = jQuery;
				if ( typeof wc_checkout_params === ‘undefined’ )
				return false;

				var updateTimer,dirtyInput = false,xhr;

				function update_shipping(billingstate,billingcountry) 

					if ( xhr ) xhr.abort();

					$( '#order_methods, #order_review' ).block({ message: null, overlayCSS: { background: '#fff url(' + wc_checkout_params.ajax_loader_url + ') no-repeat center', backgroundSize:'16px 16px', opacity: 0.6 } });

						var data = {

						action: 'woocommerce_update_order_review',

						security: wc_checkout_params.update_order_review_nonce,

						billing_state: billingstate,

						billing_country : billingcountry,

						post_data: $( 'form.checkout' ).serialize()


						xhr = $.ajax({

						type: 'POST',

						url: wc_checkout_params.ajax_url,

						data: data,

						success: function( response ) {

						var order_output = $(response);

						$( '#order_review' ).html( response['fragments']['.woocommerce-checkout-review-order-table']+response['fragments']['.woocommerce-checkout-payment']);



						error: function(code){





			jQuery('.state_select').change(function(e, params){



<?php }


    add_action( 'wp_footer', 'custom_checkbox_checker', 50 );

In Above code, I am updating order review on change of state.

First of all I’ve created a function “custom_checkbhox_checker();” which has been initialize in “wp_footer” action.
after that, i’m checking for checkout page “if ( is_checkout() )”, if checkout page found then only apply this script otherwise not.

then after I’m passing parameter for AJAX in which we have used “action: ‘woocommerce_update_order_review’,” this action. also, you can pass additional data as per your need.

For Example, we have used “billing_state: billingstate, billing_country : billingcountry” as parameters.

in success response of Ajax, you’ll get the update HTML code for order review total and shipping method which you have to put into “#order_review” ID.