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.',
	        	$minimum_num_products,
	        	$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') )) {
     if(is_user_logged_in()):
         $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>';
    else:
         $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>';
    endif;
 }
 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.

Register a Custom Hook and use it in WordPress

In WordPress, we have the option to register a custom hook for our use. For this, we have “do_action();” which register a hook in WordPress.

do_action(‘custom_hook_name’,$parameters);

you have right above code line to register a hook. you can choose you to hook name and parameters which you want to pass in that hook. Hook name should be unique.

How to use this hook ?

For using this hook we have another function is “add_action();” in this function we need to specify the hook name in which you want to perform the action and the function name in which we’ll write our performing code.

add_action(‘custom_hook_name’,’custom_function’,10,1);

10 is the priority of action and 1 is the no of a parameter which you wanna get via this hook.

What is hook ??

WordPress hooks are arguably the basis of WordPress development, forming a large part of the core functionality and used by almost every plugin and theme available to date. The concept of hooks can also be somewhat daunting for users who are starting out with developing for WordPress. Today, we’ll jump in and find out a bit more about just what exactly WordPress hooks are and how they can help you on your way to WordPress rock stardom.

What exactly *are* WordPress hooks anyways?

WordPress hooks are, essentially, triggers of sorts that allow users to, with short snippets of code, modify areas a WordPress theme or plugin, or add their own code to various parts of WordPress without modifying the original files. An example of this could be along the lines of either “when WordPress chooses which template file to load, run our custom code” or “when you generate the content for each post, add social bookmarking links to the end of the content”. These examples will be expanded upon once we’re a bit more familiar with what exactly the different types of hooks are.

Hooks can be divided into “Action” and “Filter” hooks, the former allowing for insertion of custom code at various points (not unlike events in JavaScript) and the latter allowing for the manipulation of various bits of content (for example, the content of a page or blog post). Lets take a closer look at each of these, shall we?
Action Hooks

Action hooks are designated points in the WordPress core, theme and plugin code where it is possible for outside resources (outside of the scope of where the hook is… either in the core, theme or plugin) to insert additional code and, there by, customise the code to do additional functions they may desire. An example of this is the commonly used wp_head action hook, used by many themes and plugins to inject additional CSS stylesheets, processing code or anything else they require to sit between theandtags of their WordPress theme’s XHTML structure. This is the reason for including wp_head(); in all WordPress themes.

To hook on to an action, create a function in your theme’s functions.php file (or in your plugin’s code) and hook it on using the add_action() function, as follows:

<?php
	add_action( 'wp_head', 'wpcandy_actionhook_example' );

	function wpcandy_actionhook_example () {

		echo '<meta name="description" content="This is the meta description for this page." />' . "
";

	} // End wpcandy_actionhook_example()
?>

The above code adds the text “Hello WPCandy Readers!” between your theme’stags. Placing “wp_head” in the call to add_action() with “get_header” would display this text above your theme.

The way I like to explain action hooks, in a single sentence, is: “When you get to this point, do that.”

Filter Hooks

Filter hooks are used to manipulate output. An example of this would be to add a line or text (or a hyperlink, or a signature sign-off—whatever you’d like) to the end of the content of each of your blog posts. Filter hooks can also be used for truncating text, changing formatting of content, or just about any other programming manipulation requirement (for example, adding to or overriding an array of values).

Custom code is added as a filter using the add_filter() function. The following code adds a sign-off to the end of each blog post, only when viewing the full blog post screen:

<?php
	add_filter( 'the_content', 'wpcandy_filterhook_signoff' );

	function wpcandy_filterhook_signoff ( $content ) {

		if ( is_single() ) {

			$content .= '
Th-th-th-th-th That\'s all, folks!
' . " "; } // End IF Statement return $content; } // End wpcandy_filterhook_signoff() ?>

The above code adds a new div tag to the end of the content of our blog post, only when on a single blog post screen.