What is WordPress hooks? What are WordPress hooks?

WordPress Hooks are the functions that can be applied to an Action or a Filter in WordPress.
Two types of hooks exist in WordPress. That are

  1. Action
  2. Filter

Action hooks allow you to add additional code to the WordPress core or theme so that you can achieve some new functionality or customizations.
It can be handled by add_action() and do_action().

Filter hooks allow you to control how something happens or change something that’s already being output.
It can be handled by add_filter and apply_filter().

How to get using current WordPress version programmatically?

You can get the WordPress version using the following code:

$version=bloginfo('version');
echo $version;

Here $version displays the WordPress Version you use. You can get different information about the current site by passing parameters to bloginfo(). Some important parameter’s are listed below

  1. name
  2. description
  3. html_type
  4. url
  5. admin_email
  6. language
  7. stylesheet_directory
  8. template_directory

Have any doubt, then comment here!

Disable Payment Gateway for a Specific Country in WooCommerce

If you want to disable payment gateway for a specific country in WooCommerce then you have to decide the payment gateway and country code. Here I’m disabling the cod for India. You have to replace “cod” instead of your payment gateway. Have to replace “IN” instead of your decided country code. Place the following code in your functions.php file.

function disable_payment_gateway_for_a_country( $available_gateways ) {
  global $woocommerce;
  if ( isset( $available_gateways['cod'] ) && $woocommerce->customer->get_country() == 'IN' ) 
  {
    unset( $available_gateways['cod'] );
  } 
  return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'disable_payment_gateway_for_a_country' );

Have any doubt, then comment here!

Get all orders of a single customer in WooCommerce by email ID

You can fetch all WooCommerce orders of a single customer by different parameter.
Here you can find all orders of a customer by email id.
Use the following to fetch all orders of a single customer in WooCommerce by email ID.
You have to replace customer email id instead of ‘CUSTOMER_EMAIL_ID_HERE’.

$billing_email='CUSTOMER_EMAIL_ID_HERE';
$customer_orders = get_posts( array(
                    'meta_key'    => '_billing_email',
                    'meta_value'  => $billing_email,
                    'post_type'   => 'shop_order',
                    'post_status' => array_keys( wc_get_order_statuses() ),
                    'numberposts' => -1
                ));

Have any doubt, then comment here!

Get all orders of a single customer in WooCommerce by user ID

You can fetch all WooCommerce orders of a single customer by different parameter.
Here you can find all WooCommerce orders of a customer by user id.
Use the following to fetch all orders of a single customer in WooCommerce by user ID.
You have to replace customer id instead of ‘CUSTOMER_USER_ID_HERE’.

$user_id='CUSTOMER_USER_ID_HERE';
$customer_orders = get_posts( array(
                    'meta_key'    => '_customer_user',
                    'meta_value'  => $user_id,
                    'post_type'   => 'shop_order',
                    'post_status' => array_keys( wc_get_order_statuses() ),
                    'numberposts' => -1
                ));

Have any doubt, then comment here!