fbpx

Copyright 2018 Brad Holmes

5 Most used Woocomerce snippets

By day im a web dev by night im Batman, not really by night i spend most my time trying to put together things i find interesting and maybe you will too, so i thought i would post about my top 5 woocommerce snippets.

I build almost all of my custom WordPress builds using the awesome Sage starter theme from roots

Adding Woocommerce theme support

My #1 most used Woocommerce snippet is adding theme support. My custom themes are built using Sage 8.6 starter and Sage doesn.t come with woocommerce support automatically, therefore meaning this is my number one go to snippet

/**
* Theme Support for WooCommerce
*/
add_theme_support( 'woocommerce' );  

/**
 * Add Theme Support for WooCommerce Gallery Lightbox
 */ 
add_action( 'after_setup_theme', 'wootheme_setup' );
 
function wootheme_setup() {
    add_theme_support( 'wc-product-gallery-zoom' );
    add_theme_support( 'wc-product-gallery-lightbox' );
    add_theme_support( 'wc-product-gallery-slider' );
}

Optimising Woocommerce

Woocommerce adds a whole lot of functionality but also a whole lot of scripts and styles which can slow things done so removing Woocommerce scripts from none store pages can help you speed your awesome site back up

/**
* Optimizing WooCommerce Scripts
* Removes WooCommerce styles and scripts from non WooCommerce pages.
*/  
if (!function_exists( 'woo_manage_woocommerce_styles' ) ) :

function evolution_manage_woocommerce_styles() {

    //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() && ! is_account_page() ) {          
            wp_dequeue_style( 'woocommerce-layout' );
            wp_dequeue_style( 'woocommerce-smallscreen' );
            wp_dequeue_style( 'woocommerce-general' );
            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' );
        }
    }
}
add_action( 'wp_enqueue_scripts', 'evolution_manage_woocommerce_styles', 99 );
endif;

Adding Purchased Products Column

This is one of my clients favourite snippets. This snippet adds an extra column to the order admin page listing the products purchased making life so much easier for a store admin to quickly browse of the orders and whats been bought without having to click the funky little eye on every product

add_filter('manage_edit-shop_order_columns', 'woo_order_items_column' );
function woo_order_items_column( $order_columns ) {
    $order_columns['order_products'] = "Purchased Products";
    return $order_columns;
}
 
add_action( 'manage_shop_order_posts_custom_column' , 'woo_order_items_column_cnt' );
function woo_order_items_column_cnt( $colname ) {
	global $the_order; // the global order object
 
 	if( $colname == 'order_products' ) {
 
		// get items from the order global object
		$order_items = $the_order->get_items();
 
		if ( !is_wp_error( $order_items ) ) {
			foreach( $order_items as $order_item ) {
 
 				echo $order_item['quantity'] .' × <a href="' . admin_url('post.php?post=' . $order_item['product_id'] . '&amp;action=edit' ) . '">'. $order_item['name'] .'</a>
';
				// you can also use $order_item-&gt;variation_id parameter
				// by the way, $order_item['name'] will display variation name too
 
			}
		}
 
	}
 
}