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.
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' ) );
}
How to show featured products and/or contact form on Products not found page?
// 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;
}
Filter by featured products in admin
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' );