Popular custom WooComerce functions

In general when we start some project based on WooCommerce that we use many same function. The purpose of article is attempt to gather useful functions for work with WooCommerce.

WooCommerce useful functions

0. Enable WooCommerce support

add_action( 'after_setup_theme', 'enable_woocommerce_support' );

function enable_woocommerce_support() {
   add_theme_support( 'woocommerce' );

1. Customize WooCommerce breadcrumbs

add_filter( 'woocommerce_breadcrumb_defaults', 'change_breadcrumb' );
function change_breadcrumb( $defaults ) {
	$defaults['wrap_before'] = '<nav class="breadcrumbs"><ul>';
	$defaults['wrap_after'] = '</nav></ul>';
	$defaults['before'] = '<li>';
	$defaults['after'] = '</li>';
	$defaults['delimiter'] = '';
	return $defaults;

3. Show empty categories

add_filter( "woocommerce_product_subcategories_hide_empty", "__return_false" );

4. Exclude Uncategorized category from products page

add_filter( 'woocommerce_product_categories_widget_args', 'custom_woocommerce_product_subcategories_args' );
add_filter( 'woocommerce_product_subcategories_args', 'custom_woocommerce_product_subcategories_args' );

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

5. Change number of products that are displayed per page

add_filter( 'loop_shop_per_page', 'new_loop_shop_per_page', 20 );
function new_loop_shop_per_page( $cols ) {
	// $cols contains the current number of products per page based on the value stored on Options -> Reading
	// Return the number of products you wanna show per page.
	$cols = 20;
	return $cols;

6. Don't display WooCommerce notices

add_filter( 'wc_add_to_cart_message_html', '__return_false' );

7. Don't added product to cart after reload product page

add_action( 'woocommerce_add_to_cart', 'refresh_function' );

function refresh_function(){
    header( 'Refresh:0' );

to be continue ...


  1. https://stackoverflow.com/questions/62007837/refresh-woocommerce-product-page-on-added-to-cart

Leave Comment

Your email address will not be published. Required fields are marked *