12 useful Woocommerce snippets & hacks

Here are 12 useful Woocommerce snippets and hacks I use on most of my sites. So, I would like to share them with you. If you want to use them then either paste them inside you theme’s function.php file or better yet, use Code Snippets plugin. If you to that then you will not lose the modifications during your next theme switch.

Blocksy theme

SAVE 10% COUPON: WPSH10

Kadence theme

SAVE 10% COUPON: SIMPLEHACKS

Video: How to add Woocommerce snippets & hacks

In this video I will show you how to add all those snippets and how will they affect your site.

How to hide Uncategorized product category from Woocommerce shop page and widget?

This code here below hides Uncategorized category from shop page

// Hide Uncategorized product category from shop page
add_filter( 'woocommerce_product_subcategories_args', 'hide_uncategorized_cat_from_shop_page' );
function hide_uncategorized_cat_from_shop_page( $args ) {
  $args['exclude'] = get_option( 'default_product_cat' );
  return $args;
}

This code here below hides Uncategorized category from the sidebar widget

// Hide Uncategorized product category from widget
add_filter( 'woocommerce_product_categories_widget_args', 'hide_uncategorized_cat_from_widget' );

function hide_uncategorized_cat_from_widget( $args ) {
  $args['exclude'] = get_option( 'default_product_cat' );
  return $args;
}

How to hide category product count in product archives?

add_filter( 'woocommerce_subcategory_count_html', '__return_false' );

How to remove Woocommerce reviews tab?

See another video I made on how to create, rename, remove and reorder custom product tabs.

add_filter( 'woocommerce_product_tabs', 'remove_review_tab', 98 );
function remove_review_tab( $tabs ) {
    unset( $tabs['reviews'] ); // Remove the reviews tab
    return $tabs;
}

How to activate Woocommerce catalogue mode without extra plugin?

add_filter( 'woocommerce_is_purchasable', '__return_false'); // DISABLING PURCHASE FUNCTIONALITY AND REMOVING ADD TO CART BUTTON FROM NORMAL PRODUCTS

remove_action('woocommerce_single_variation', 'woocommerce_single_variation', 10); // REMOVING PRICE FROM VARIATIONS

remove_action('woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20); // REMOVING ADD TO CART BUTTON FROM VARIATIONS

Bonus hack: How to add an enquiry form on Woocommerce single product page?

Also, if you would like to activate a catalogue mode and show a product enquiry form on single product page, then use this code instead.

add_filter( 'woocommerce_is_purchasable', '__return_false'); // DISABLING PURCHASE FUNCTIONALITY AND REMOVING ADD TO CART BUTTON FROM NORMAL PRODUCTS

remove_action('woocommerce_single_variation', 'woocommerce_single_variation', 10); // REMOVING PRICE FROM VARIATIONS

remove_action('woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20); // REMOVING ADD TO CART BUTTON FROM VARIATIONS

// Add an enquiry form on Woocommerce single product page
add_action( 'woocommerce_single_product_summary', 'single_product_message', 20 );
 
function single_product_message() {
    echo '<p class="woocommerce-message">Send us an enquiry </p>';
	echo do_shortcode(''); // Your contact form shortcode goes here. 
}

If you are interested then see this video on how to add enquiry form on product page.

How to show out of stock products last on Woocommerce product archive pages?

add_action( 'woocommerce_product_query', 'out_of_stock_last', 999 );
 
function out_of_stock_last( $query ) {
    if ( is_admin() ) return;
    $query->set( 'meta_key', '_stock_status' );
    $query->set( 'orderby', array( 'meta_value' => 'ASC' ) );
}
// You can use all Woocommerce shortcodes here below. See available shortcodes here https://docs.woocommerce.com/document/woocommerce-shortcodes/

add_action( 'woocommerce_no_products_found', 'featured_products_on_not_found', 20 );
function featured_products_on_not_found() {
	echo '<h4>' . __( 'No products were found matching your selection. Although... You may be interested in these products', 'domain' ) . '</h4>';
	echo do_shortcode( '[featured_products per_page="4"]' ); // Here goes your shortcode
	echo '<h4>' . __( 'Can’t find your product? Send us an enquiry', 'domain' ) . '</h4>';
	echo do_shortcode( '' ); // Here goes your shortcode
}

How to remove Woocommerce “Ship to a different address?” selection?

add_filter( 'woocommerce_cart_needs_shipping_address', '__return_false');

How to remove Woocommerce checkout fields without a plugin?

Here below are all the fields you can remove.

// Remove checkout fields
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {
 // remove billing fields
    unset($fields['billing']['billing_first_name']); // Billing First name
    unset($fields['billing']['billing_last_name']); // Billing Last name
    unset($fields['billing']['billing_company']); // Billing company
    unset($fields['billing']['billing_address_1']); // Billing Address 1
    unset($fields['billing']['billing_address_2']); // Billing Address 2
    unset($fields['billing']['billing_city']); // Billing city
    unset($fields['billing']['billing_postcode']); // Billing postcode
    unset($fields['billing']['billing_country']); // Billing country
    unset($fields['billing']['billing_state']); // Billing state
    unset($fields['billing']['billing_phone']); // Billing phone
    unset($fields['billing']['billing_email']); // Billing email
   
    // remove shipping fields 
    unset($fields['shipping']['shipping_first_name']); // Shipping first name  
    unset($fields['shipping']['shipping_last_name']); // Shipping last name  
    unset($fields['shipping']['shipping_company']); // Shipping company  
    unset($fields['shipping']['shipping_address_1']); // Shipping address 1
    unset($fields['shipping']['shipping_address_2']); // Shipping address 2
    unset($fields['shipping']['shipping_city']); // Shipping city 
    unset($fields['shipping']['shipping_postcode']); // Shipping postcode
    unset($fields['shipping']['shipping_country']); // Shipping country
    unset($fields['shipping']['shipping_state']); // Shipping state
    
    // remove order comment fields
    unset($fields['order']['order_comments']); // Order comments
     return $fields;
}

You don’t have to paste all of them, instead use only these you need. For example, if you need to remove billing address 2, state and company fields then use this code here below.

// Remove checkout fields
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {
 // remove billing fields

    unset($fields['billing']['billing_company']); // Billing company
    unset($fields['billing']['billing_address_2']); // Billing Address 2
    unset($fields['billing']['billing_state']); // Billing state
   
     return $fields;
}

How to make Woocommerce checkout fields optional?

Pay attention that “true” =required and if you want the field to be optional then set it to “false”.

add_filter( 'woocommerce_default_address_fields' , 'optional_default_address_fields' );
 function optional_default_address_fields( $address_fields ) {
 	$address_fields['first_name']['required'] = true;	 
 	$address_fields['last_name']['required'] = true;
 	$address_fields['company']['required'] = false;
	$address_fields['address_1']['required'] = true;
	$address_fields['address_2']['required'] = false;
	$address_fields['country']['required'] = false;
 	$address_fields['postcode']['required'] = false;
 	$address_fields['city']['required'] = false;
 	$address_fields['state']['required'] = false;
 return $address_fields;
 }
// For billing email and phone fields
add_filter('woocommerce_billing_fields', 'optional_checkout_fields1', 1000, 1);
function optional_checkout_fields1( $fields ) {
    $fields['billing_email']['required'] = true;
    $fields['billing_phone']['required'] = false;
    return $fields;
}

And once more, if you need a only couple of fields to be optional then just paste the lines you need. For example, this snippet makes country and phone fields optional.

add_filter( 'woocommerce_default_address_fields' , 'optional_default_address_fields' );
 function optional_default_address_fields( $address_fields ) {
	$address_fields['country']['required'] = false;
 return $address_fields;
 }

// For billing email and phone fields
add_filter('woocommerce_billing_fields', 'optional_checkout_fields1', 1000, 1);
function optional_checkout_fields1( $fields ) {
    $fields['billing_phone']['required'] = false;
    return $fields;
}
function featured_products_filter() {

     global $typenow, $wp_query;

    if ($typenow=='product') :
        // Featured/ Not Featured
        $output .= "<select name='featured_status' id='dropdown_featured_status'>";
        $output .= '<option value="">'.__( 'Choose status', 'woocommerce' ).'</option>';

        $output .="<option value='featured' ";
        if ( isset( $_GET['featured_status'] ) ) $output .= selected('featured', $_GET['featured_status'], false);
        $output .=">".__( 'Featured', 'woocommerce' )."</option>";

        $output .="<option value='normal' ";
        if ( isset( $_GET['featured_status'] ) ) $output .= selected('normal', $_GET['featured_status'], false);
        $output .=">".__( 'Non-featured', 'woocommerce' )."</option>";

        $output .="</select>";

        echo $output;
    endif;
}
add_action('restrict_manage_posts', 'featured_products_filter');

function featured_products_filter_query( $query ) {
    global $typenow;

    if ( $typenow == 'product' ) {

        // Subtypes
        if ( ! empty( $_GET['featured_status'] ) ) {
            if ( $_GET['featured_status'] == 'featured' ) {
                $query->query_vars['tax_query'][] = array(
                    'taxonomy' => 'product_visibility',
                    'field'    => 'slug',
                    'terms'    => 'featured',
                );
            } elseif ( $_GET['featured_status'] == 'normal' ) {
                $query->query_vars['tax_query'][] = array(
                    'taxonomy' => 'product_visibility',
                    'field'    => 'slug',
                    'terms'    => 'featured',
                    'operator' => 'NOT IN',
                );
            }
        }

    }

}
add_filter( 'parse_query', 'featured_products_filter_query' );

Filter by sale products in admin

function backend_onsale_filter($output) {
	global $wp_query;
	
	$selected = 0;
	if (isset($_GET['product_sale'])) {
		$selected = (int)$_GET['product_sale'];
	}
	
	$output .= '
		<select id="dropdown_product_sale" name="product_sale">
			<option value="">Sale price filter</option>
			<option selected="selected" value="1">On sale</option>
			<option selected="selected" value="2">Regular price</option>
		</select>
	';
	
	return $output;
}
add_action('woocommerce_product_filters', 'backend_onsale_filter');
 
/*
 * Woocommerce Filter by on sale where statement
 */
function backend_onsale_where_statement($where) {
	global $wp_query, $wpdb;
 
	if (!is_admin() || $_GET['post_type'] != "product" || !isset($_GET['product_sale']) || $_GET['product_sale'] <= 0) {
		return $where;
	}
 
	$productsIDs = [];
	if ($_GET['product_sale'] == 1) {
		$querystr = '
			SELECT p.ID
			FROM ' . $wpdb->posts . ' p
			WHERE p.ID IN (
				SELECT post_id FROM ' . $wpdb->postmeta . ' pm WHERE pm.meta_key = "_sale_price" AND pm.meta_value > \'\'
			)
		';
		
		$pageposts = $wpdb->get_results($querystr, OBJECT);
		
		$productsIDs = array_map(function($n){
			return $n->ID;
		}, $pageposts);
	} elseif ($_GET['product_sale'] == 2) {
		$querystr = '
			SELECT p.ID
			FROM ' . $wpdb->posts . ' p
			WHERE p.ID NOT IN (
				SELECT post_id FROM ' . $wpdb->postmeta . ' pm WHERE pm.meta_key = "_sale_price" AND pm.meta_value > \'\'
			)
		';
		
		$pageposts = $wpdb->get_results($querystr, OBJECT);
		
		$productsIDs = array_map(function($n){
			return $n->ID;
		}, $pageposts);
	}
	
	$where .= ' AND ' . $wpdb->posts . '.ID IN (' . implode(",", $productsIDs) . ') ';
	
	return $where;
}
add_filter('posts_where' , 'backend_onsale_where_statement');

Disable Woocommerce Analytics & other bloat

add_filter( 'woocommerce_admin_disabled', '__return_true' );

Here are some of my favorite WordPress tools

Thanks for reading this article! I hope it's been useful as you work on your own websites and e-commerce sites. I wanted to share some tools I use as a WordPress developer, and I think you'll find them helpful too.

Just so you know, these are affiliate links. If you decide to use any of them, I'll earn a commission. This helps me create tutorials and YouTube videos. But honestly, I genuinely use and recommend these tools to my friends and family as well. Your support keeps me creating content that benefits everyone.

Themes: Over the past few years, I've consistently relied on two primary themes for all sorts of projects: the Blocksy theme and the Kadence Theme. If you explore this website and my YouTube channel, you'll come across numerous tutorials that delve into these themes. If you're interested in obtaining a 10% discount for both of these themes, then:

Code Snippets Manager: WPCodeBox allows you to add code snippets to your site. Not only that, but it also provides you with the capability to construct and oversee your WordPress Code Snippets library right in the cloud. You can grab it with the 20% discount here (SAVE 20% Coupon: WPSH20).

Contact forms: There are hundreds of contact forms out there but Fluent Forms is the one I like the most. If you need a 20% discount then use this link (save 20% coupon is WPSH20).

Gutenberg add-ons: If I need a good Gutenberg blocks add-on then Kadence Blocks is the one I have used the most. You’ll get a 10% discount with the coupon SIMPLEHACKS here.

Website migration: While building a website you probably need a good plugin that can help you with the migration, backups, restoration, and staging sites. Well, WpVivid is the one I have used for the last couple of years. If you use this link along with the WPSH20 coupon you’ll get a 20% discount.

Woocommerce extensions: There are a bunch of Woocommerce extensions that I like but the one that stands out is Advanced Dynamic Pricing. Once again, you’ll get a 20% discount if you use this link here (save 20% coupon is WPSH20)

Web Hosting: If you would like to have a really fast and easy-to-use managed cloud hosting, then I recommend Verpex Hosting (see my review here). By the way, this site is hosted in Verpex.)

To see all my most up-to-date recommendations, check out this resource that I made for you!

Do you want to thank me and buy me a beer?

Every donation is entirely welcome but NEVER required. Enjoy my work for free but if you would like to thank me and buy me a beer or two then you can use this form here below.

Donation Form (#2)

Janek T.
Janek T.

Improve this text: {CLIPBOARD}

- I have been passionate about Wordpress since 2011, creating websites and sharing valuable tips on using Wordpress and Woocommerce on my site.
- Be the first to receive notifications about new tutorials by subscribing to my Youtube channel .
- Follow me on Twitter here

Articles: 116