Basic WooCommerce Snippets to paste and modify

·

·

Updated

Rapidly augment your WooCommerce store through hooks and actions, little references in the source code that allow us to inject our own code without it getting deleted later.

I’ve collected a bunch of these snippets, and thought a fledgling developer would appreciate them, too. Here’s a growing list of my favorites!

Instructions

  • Place these in your functions.php file or another PHP file called by functions.php.
  • Find and replace prefix with your given project code. Or skip that and namespace your PHP files.
  • I call add_action before my functions to improve readbility. It can follow after though, if you prefer.
  • Definitely test before implementing this stuff in your live environment! Your mileage may vary.

Global Options

Make your custom WordPress theme WooCommerce compatible

Note: Check for this if your store is acting up! Many quirky things can happen without it.

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

Remove that annoying styling wrapper!

remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10);
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);
add_action('woocommerce_before_main_content', 'prefix_woo_wrapper_start', 10);
add_action('woocommerce_after_main_content', 'prefix_woo_wrapper_end', 10);
function prefix_woo_wrapper_start() {
	echo '<div class="container">';
}
function prefix_woo_wrapper_end() {
	echo '</div>';
}

Log email notices to Order Notes

add_action( 'woocommerce_email_sent', 'prefix_woo_email_ordernote', 10, 3 );
function prefix_woo_email_ordernote( $wp_mail_return, $email_id, $email_obj ) {
	if ( !is_object( $email_obj ) ) { return; }
	if ( $email_obj->is_customer_email() ) {
		$order = new WC_Order( $email_obj->object->get_order_number() );
		if ( $wp_mail_return ) {
			$order_note = sprintf( '<em>%s</em> email sent.', $email_obj->get_title() );
		}
		else {
			$order_note = sprintf( '<em>%s</em> email NOT sent.', $email_obj->get_title() );
		}
		$order->add_order_note( $order_note );
	}
}

“Products” Archive Template

Disable breadcrumb navigation

add_action( 'init', 'prefix_remove_wc_breadcrumbs' );
function prefix_remove_wc_breadcrumbs() {
	remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );
}

Alter the number of displayed Products per row

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

Display Excerpts

add_action( 'woocommerce_after_shop_loop_item_title', 'prefix_woo_catalog_excerpt', 20 ); 
function prefix_woo_catalog_excerpt() { 
	global $product; 
	echo '<p class="woocommerce-loop-product__excerpt">' . $product->get_short_description() . '</p>';
}

Checkout Template

Remove that Company Field

add_filter( 'woocommerce_checkout_fields', 'prefix_woo_checkout_company' );
function prefix_woo_checkout_company( $fields ) {
	 unset($fields['billing']['billing_company']);
	 return $fields;
}

Modify the Notes field

add_filter( 'woocommerce_checkout_fields', 'prefix_woo_checkout_notes' );
function prefix_woo_checkout_notes( $fields ) {
	$fields['order']['order_comments']['placeholder'] = 'Notes or questions about your order';
	$fields['order']['order_comments']['label'] = 'Purchase notes';
	return $fields;
}

Cart Template

Add a policy notice

add_action('woocommerce_after_cart_totals', 'prefix_woo_policies');
function prefix_woo_policies( $output ) { 
	$output = '<p>Your HTML here!</p>';
	echo $output;
}