Every WooCommerce project reuses the same handful of utility snippets. This is a living reference of the most common ones — all tested with WooCommerce 3.x. Add them to your theme’s functions.php or a dedicated plugin file.
Problem: WooCommerce development involves the same repeated utility tasks — checking whether the current page is a WooCommerce page, accessing product data, reading cart totals, or identifying the current customer — and the correct functions are not always obvious.
Solution: The reference below groups the most useful WooCommerce utility functions by context: theme support declaration, page detection, product data access, cart and order retrieval, and customer information.
0. Declare WooCommerce theme support
<?php
add_action( 'after_setup_theme', 'my_theme_add_woocommerce_support' );
function my_theme_add_woocommerce_support() {
add_theme_support( 'woocommerce' );
add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' );
}
1. Customise WooCommerce breadcrumbs
<?php
add_filter( 'woocommerce_breadcrumb_defaults', 'custom_woocommerce_breadcrumb' );
function custom_woocommerce_breadcrumb( $defaults ) {
$defaults['wrap_before'] = '<nav class="breadcrumbs"><ul>';
$defaults['wrap_after'] = '</ul></nav>';
$defaults['before'] = '<li>';
$defaults['after'] = '</li>';
$defaults['delimiter'] = '';
return $defaults;
}
2. Show empty product categories
<?php
add_filter( 'woocommerce_product_subcategories_hide_empty', '__return_false' );
3. Exclude "Uncategorised" from the product category widget
<?php
add_filter( 'woocommerce_product_categories_widget_args', 'exclude_default_product_cat' );
add_filter( 'woocommerce_product_subcategories_args', 'exclude_default_product_cat' );
function exclude_default_product_cat( $args ) {
$args['exclude'] = get_option( 'default_product_cat' );
return $args;
}
4. Change the number of products displayed per page
<?php
add_filter( 'loop_shop_per_page', 'set_products_per_page', 20 );
function set_products_per_page( $cols ) {
return 20;
}
5. Suppress the "added to cart" notice
<?php
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
6. Prevent duplicate cart additions on page reload
<?php
add_action( 'woocommerce_add_to_cart', 'redirect_after_add_to_cart' );
function redirect_after_add_to_cart() {
// Redirect to the same page, clearing the POST data so a reload does not re-add
header( 'Refresh: 0' );
}
NOTE: The Refresh: 0 header is a quick fix for simple sites, but it causes an extra full-page load. A cleaner approach for larger projects is to use AJAX add-to-cart (add_theme_support('wc-product-gallery-slider') + the built-in WooCommerce AJAX handler) which avoids the reload entirely.