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.
0. Enable WooCommerce support
<?php
add_action( 'after_setup_theme', 'enable_woocommerce_support' );
function enable_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
1. Customize WooCommerce breadcrumbs
<?php
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
<?php
add_filter( "woocommerce_product_subcategories_hide_empty", "__return_false" );
4. Exclude Uncategorized category from products page
<?php
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
<?php
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
<?php
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
7. Don't added product to cart after reload product page
<?php
add_action( 'woocommerce_add_to_cart', 'refresh_function' );
function refresh_function(){
header( 'Refresh:0' );
}
to be continue ...
Sources: