Useful WooCommerce functions for themes
When we start a project on WooCommerce, we use many of the same functions again and again. The purpose of this article is to gather the useful ones in one place. All of them go into the theme’s functions.php.
1. Enable WooCommerce support
Without it WooCommerce treats the theme as not compatible and wraps the shop pages in its own fallback templates.
<?php
add_action( 'after_setup_theme', 'enable_woocommerce_support' );
function enable_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
2. Customize the WooCommerce breadcrumbs
For example, output them as a list:
<?php
add_filter( 'woocommerce_breadcrumb_defaults', 'change_breadcrumb' );
function change_breadcrumb( $defaults ) {
$defaults['wrap_before'] = '<nav class="breadcrumbs"><ul>';
$defaults['wrap_after'] = '</ul></nav>';
$defaults['before'] = '<li>';
$defaults['after'] = '</li>';
$defaults['delimiter'] = '';
return $defaults;
}
3. Show empty categories
By default categories without products are hidden on the shop page.
<?php add_filter( 'woocommerce_product_subcategories_hide_empty', '__return_false' );
4. Exclude the Uncategorized category
From the categories on the shop page and from the product categories 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;
}
5. Change the number of products per page
<?php
add_filter( 'loop_shop_per_page', 'new_loop_shop_per_page', 20 );
function new_loop_shop_per_page( $cols ) {
// $cols is the current number of products per page.
return 20;
}
6. Hide the “added to cart” message
<?php add_filter( 'wc_add_to_cart_message_html', '__return_false' );
This removes only the “… has been added to your cart” notice. Other notices, such as errors, stay.
7. Do not add the product again when the page is reloaded
On a product page, the “Add to cart” button sends a POST form. If the visitor reloads the page after that, the browser sends the form again and the product is added a second time. The fix is to redirect after adding, so the reload is a normal GET request:
<?php
add_filter( 'woocommerce_add_to_cart_redirect', 'redirect_back_after_add_to_cart' );
function redirect_back_after_add_to_cart( $url ) {
if ( ! $url && ! wp_doing_ajax() && isset( $_REQUEST['add-to-cart'] ) ) {
$url = get_permalink( absint( $_REQUEST['add-to-cart'] ) );
}
return $url;
}
If the shop should go to the cart after adding, there is no need for code: enable “Redirect to the cart page after successful addition” in WooCommerce → Settings → Products.
Update (2020): A simpler variant from the source below is a Refresh header right after the product is added: add_action( 'woocommerce_add_to_cart', function () { header( 'Refresh:0' ); } );. It also works, but the browser loads the page twice.
To be continued…
Sources: