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 usefull 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;
}

to be continue ...

Leave Comment

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