woocommerce-functions

5 Useful WooCommerce Functions

All the time I find myself using the same code in different eCommerce projects so I gathered a few of my favourites that you could consider using in your project. You can add these php snippets to your theme functions file.

1. Declaring WooCommerce Support

add_action( 'after_setup_theme', 'theme_woocommerce_support' );
function theme_woocommerce_support() {
    add_theme_support( 'woocommerce' );
}

2. Breadcrumb Delimiter

add_filter( 'woocommerce_breadcrumb_defaults', 'theme_breadcrumb_delimiter' );
function theme_breadcrumb_delimiter( $defaults ) {
	$defaults['delimiter'] = '<i class="fa fa-angle-right"></i>';
	return $defaults;
}

3. Change “Add to Cart” Button Text

/**
 * Add to cart text on single products
 */
function theme_custom_cart_button_text() {
	return __('My Button Text', 'woocommerce');
}
add_filter('single_add_to_cart_text', 'theme_custom_cart_button_text');


/**
 * Add to cart text on product archives
 */
function theme_archive_custom_cart_button_text() {
	return __( 'My Button Text', 'woocommerce' );
}
add_filter( 'add_to_cart_text', 'theme_archive_custom_cart_button_text' );

4. Number of products per page

add_filter( 'loop_shop_per_page','theme_loop_shop_per_page', 20 );
function theme_loop_shop_per_page( $cols ) {
	return 9;
}

5. Number of products per row

add_filter('loop_shop_columns', 'theme_loop_columns');
function theme_loop_columns() {
	return 3;
}

And there you go. I hope you find these snippets useful and all the best in your development.