15 WooCommerce Code Snippets that I’m Most Thankful For

woocommerce-thumbToday is Thanksgiving here in the USA and I thought it would be appropriate to share the WooCommerce code snippets that I’m most thankful to have in my arsenal.

WooCommerce is great out-of-the-box but many times you need to customize it’s look and feel or functionality. The following code snippets will help you do both.

Customizing WooCommerce Content

WooCommerce has default text throughout the shopping experience that you may want to change. For instance, I recently built a site for a non-profit that didn’t want any mention of “cart”, “product”, or “order”. While this may be hard to change in some eCommerce systems, WooCommerce makes it pretty easy.

Change Add to Cart button text

This will change the button text for the “Add to Cart” inputs. Update the text to suit your needs.

add_filter('single_add_to_cart_text', 'woo_custom_cart_button_text');

function woo_custom_cart_button_text() {

    return __('Donate Now', 'woocommerce');

}

Custom Add To Cart Message & Custom “Continue Shopping Link”

When you add a product to your cart a message appears with default text notifying you of the recent add and asking you to continue shopping. For some, this isn’t ideal. Here’s how to change that message:

add_filter( 'woocommerce_add_to_cart_message', 'woocommrece_custom_add_to_cart_message' );

function woocommrece_custom_add_to_cart_message() {

	global $woocommerce;

// Output success messages

	if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {

		$return_to = get_permalink( woocommerce_get_page_id( 'shop' ) ); // Give the url, you want to redirect
		$message   = sprintf( '<a href="%s" class="button">%s</a> %s', $return_to, __( 'More Donation Options &rarr;', 'woocommerce' ), __( 'Donation successfully added to your cart.', 'woocommerce' ) );
	} else {

		$message = sprintf( '<a href="%s">%s</a> %s', get_permalink( woocommerce_get_page_id( 'cart' ) ), __( 'View Cart &rarr;', 'woocommerce' ), __( 'Donation successfully added to your cart.', 'woocommerce' ) );

	}

	return $message;

}

Customizing WooCommerce Checkout

The checkout page is the most important page in your shop. It’s where you ask your customers to shell out their hard earned money to purchase your products. Why not spend a little extra time making it easier and better optimized to convert? Here’s several snippets I find myself using regularly to do just that.

Customizing Checkout Field Order

Sometimes the default ordering of fields isn’t optimal for your business. You can modify the order the fields appear by customizing the following function:

add_filter( 'woocommerce_checkout_fields', 'reorder_woo_fields' );

function reorder_woo_fields( $fields ) {
	//move these around in the order you'd like
	$fields2['billing']['billing_first_name'] = $fields['billing']['billing_first_name'];
	$fields2['billing']['billing_last_name']  = $fields['billing']['billing_last_name'];
	$fields2['billing']['billing_company']    = $fields['billing']['billing_company'];
	$fields2['billing']['billing_address_1']  = $fields['billing']['billing_address_1'];
	$fields2['billing']['billing_address_2']  = $fields['billing']['billing_address_2'];
	$fields2['billing']['billing_city']       = $fields['billing']['billing_city'];
	$fields2['billing']['billing_postcode']   = $fields['billing']['billing_postcode'];
	$fields2['billing']['billing_state']      = $fields['billing']['billing_state'];
	$fields2['billing']['billing_country']    = $fields['billing']['billing_country'];
	$fields2['billing']['billing_email']      = $fields['billing']['billing_email'];
	$fields2['billing']['billing_phone']      = $fields['billing']['billing_phone'];

	//just copying these (keeps the standard order)
	$fields2['shipping'] = $fields['shipping'];
	$fields2['account']  = $fields['account'];
	$fields2['order']    = $fields['order'];

	return $fields2;
}

Change Default Checkout Country

A default install of WooCommerce may not have your preferred country selected. If you typically sell primarily to one country then it makes sense to set that country as the default.

add_filter('default_checkout_country', 'change_default_checkout_country');

function change_default_checkout_country() {
    return 'US'; // country code
}

Change Default Checkout State

If you are selling a product mainly locally it doesn’t make sense to ask the user to select the state. Manually set the state and make the checkout process easier.

add_filter('default_checkout_state', 'change_default_checkout_state');

function change_default_checkout_state() {
    return 'OH'; // state code
}

Phone Field Not Required

The telephone number field is kinda silly to have as a required field. 99% of the time I don’t want people to call me and I don’t want to give them my phone number. I’ve had numerous clients ask me to make this field not required. Here’s the snippet I add:

add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_phone', 10, 1 );
function wc_npr_filter_phone( $address_fields ) {
	$address_fields['billing_phone']['required'] = false;

	return $address_fields;
}

Skip the Cart

This is probably one of my favorite snippets. Why? Because it makes checking out faster by removing the “Cart” page that appears prior to entering the checkout page. Many times, you don’t even need a cart page to display. Most times users are only shopping for a single product. The cart page adds an unnecessary step that users can bounce out from and abandon their cart. Skip the cart and increase conversions.

/**
 * Skip the Cart
 * @see: http://stackoverflow.com/questions/15592633/woocommerce-add-to-cart-button-redirect-to-checkout
 */

add_filter( 'add_to_cart_redirect', 'redirect_to_checkout' );

function redirect_to_checkout() {
	global $woocommerce;
	$checkout_url = $woocommerce->cart->get_checkout_url();

	return $checkout_url;
}

Custom Order Now Button Text

Sometimes you want to personalize the button text on the most important button in your entire shop: the “Order Now” button. This is the button that the user clicks when they’re ready to spend money. The money button.

add_action( 'woocommerce_order_button_text', 'my_custom_checkout_text' );

function my_custom_checkout_text() {
	return "Place Donation";
}

Removing WooCommerce Elements

Depending on the site you’re building you may need to heavily customize WooCommerce. Thankfully it is very well documented and has tons of actions and filters for you to hook into. Here’s several snippets I find myself using all the time to remove elements found in a default WooCommerce implementation such as breadcrumbs, tabs, reviews and page headers.

Remove WooCommerce Breadcrumbs

Breadcrumbs are great, sometimes. Many designs don’t have them, so here’s how you get rid of them.

remove_action('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0);

Remove WooCommerce Tabs

I’m not a fan of the tabs on WooCommerce product pages. I almost always from them using:

remove_action('woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10);

… adios tabs!

Remove Product Reviews

Product reviews can be great for some businesses, but many times my clients don’t want them. Thankfully, removing them is easy:

remove_action('woocommerce_product_tabs', 'woocommerce_product_reviews_tab', 30);
remove_action('woocommerce_product_tab_panels', 'woocommerce_product_reviews_panel', 30);

 Remove Sidebar from WooCommerce

Who needs a sidebar, anyways? For many designs I’ve built out into WooCommerce there hasn’t been a sidebar in the shop. Remove that bad boy:

remove_action('woocommerce_sidebar', 'woocommerce_get_sidebar', 10);

Additional Woo Goodness

Here are a number of snippets that don’t fall in the categories above but are very useful.

Optimize WooCommerce Scripts

WooCommerce like to place all their scripts on every page of your site. You can tell it to load only on the shop pages using the following code. For more information see this article.

/**
 * Optimize WooCommerce Scripts
 * Remove WooCommerce Generator tag, styles, and scripts from non WooCommerce pages.
 */
add_action( 'wp_enqueue_scripts', 'child_manage_woocommerce_styles', 99 );

function child_manage_woocommerce_styles() {
	//remove generator meta tag
	remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ) );

	//first check that woo exists to prevent fatal errors
	if ( function_exists( 'is_woocommerce' ) ) {
		//dequeue scripts and styles
		if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
			wp_dequeue_style( 'woocommerce_frontend_styles' );
			wp_dequeue_style( 'woocommerce_fancybox_styles' );
			wp_dequeue_style( 'woocommerce_chosen_styles' );
			wp_dequeue_style( 'woocommerce_prettyPhoto_css' );
			wp_dequeue_script( 'wc_price_slider' );
			wp_dequeue_script( 'wc-single-product' );
			wp_dequeue_script( 'wc-add-to-cart' );
			wp_dequeue_script( 'wc-cart-fragments' );
			wp_dequeue_script( 'wc-checkout' );
			wp_dequeue_script( 'wc-add-to-cart-variation' );
			wp_dequeue_script( 'wc-single-product' );
			wp_dequeue_script( 'wc-cart' );
			wp_dequeue_script( 'wc-chosen' );
			wp_dequeue_script( 'woocommerce' );
			wp_dequeue_script( 'prettyPhoto' );
			wp_dequeue_script( 'prettyPhoto-init' );
			wp_dequeue_script( 'jquery-blockui' );
			wp_dequeue_script( 'jquery-placeholder' );
			wp_dequeue_script( 'fancybox' );
			wp_dequeue_script( 'jqueryui' );
		}
	}

}

Custom Columns on Woo Orders Page (in admin)

Sometimes it’s useful to add additional custom column data to the standard order columns in the WordPress backend.

/**
 * Custom Columns to Woo Orders Page
 * @SEE: http://stackoverflow.com/questions/13683162/woocommerce-show-custom-column
 */
add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION' );
function MY_COLUMNS_FUNCTION( $columns ) {
	$new_columns = ( is_array( $columns ) ) ? $columns : array();
	unset( $new_columns['order_actions'] );
	//edit this for you column(s)
	//all of your columns will be added before the actions column
	$new_columns['donation_code'] = 'Donation Code';

	$new_columns['order_actions'] = $columns['order_actions'];

	return $new_columns;
}

add_action( 'manage_shop_order_posts_custom_column', 'MY_COLUMNS_VALUES_FUNCTION', 2 );
function MY_COLUMNS_VALUES_FUNCTION( $column ) {
	global $post;
	$order = new WC_Order( $post->ID );
	$items = $order->get_items();

	//start editing, I was saving my fields for the orders as custom post meta
	//if you did the same, follow this code
	if ( $column == 'donation_code' ) {
		foreach ( $items as $item ) {
			$product_name = $item['name'];
			$product_id   = $item['product_id'];

			$donation_id = get_post_meta( $product_id, 'donation_code', true );
			echo $donation_id . "<br/>";

		}
	}

}

Auto-complete All Orders

If you sell digital products then you’re not shipping anything. You can autocomplete your orders using the following code:

/**
 * Auto Complete all WooCommerce orders.
 * http://docs.woothemes.com/document/automaticaaly-complete-orders/
 */

add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
	global $woocommerce;

	if ( ! $order_id ) {
		return;
	}
	$order = new WC_Order( $order_id );
	$order->update_status( 'completed' );
}

 

Final Thoughts

As you can see WooCommerce is incredibly flexible and you can easily customize and remove various elements throughout the cart. If you have a snippet you think should be included please use the comments below!