Skip to content

Clean up the Orders Page

ajlowndes edited this page Mar 1, 2018 · 1 revision

I'm not sure if the creator of this plugin wants to incorporate these into the plugin code itself, but for now I will write them out here as a wiki entry, so that users of the Registrations for WooCommerce plugin can add them themselves if they want.

Put these code snippets into your theme (or child theme)'s functions.php or plugin.

Changing "In Stock" to "Places Available"

For a website selling Event Registrations rather than physical items that get sold and shipped, having the event described as "4 in stock" is misleading - cleaner to have something more like "4 places still available on this course". This function will change the words "in stock" that the user sees, to a more descriptive line, depending on if there are zero, one or more places still available.

woocommerce_get_availability

add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
    // Change In Stock Text
    if ( $_product->is_in_stock() ) {
		$numleft  = $_product->get_stock_quantity();
	    if ($numleft==1) {
            $availability['availability'] = __('Only 1 place left on this course!', 'woocommerce');
        } else {
            $availability['availability'] = __($numleft .' places still available on this course.', 'woocommerce');
        }
    }
    // Change Out of Stock Text
    if ( ! $_product->is_in_stock() ) {
        $availability['availability'] = __('Sorry, this course is fully booked.', 'woocommerce');
    }
    return $availability;
}

Change "Processing" Order Status to "Paid" and "On Hold" Order Status to "Deposit Paid"

For a website selling Event Registrations rather than physical items that get sold and then shipped, having the "Orders" page display "Processing" and "On hold" is a bit confusing. I changed these two to "Paid" and "Deposit Paid" respectively.

wc_order_statuses

add_filter( 'wc_order_statuses', 'wc_renaming_order_status' );
function wc_renaming_order_status( $order_statuses ) {
    foreach ( $order_statuses as $key => $status ) {
        $new_order_statuses[ $key ] = $status;
        if ( 'wc-completed' === $key ) {
            $order_statuses['wc-processing'] = _x( 'Paid', 'Order status', 'woocommerce' );
 		$order_statuses['wc-on-hold'] = _x( 'Deposit Paid', 'Order status', 'woocommerce' );
        }
    }
    return $order_statuses;
}

Add a new column in the "Orders" page to show which registration (variation) has been purchased.

I wanted to see the Event that each person had purchased/registered to in the "Orders" page. The below two snippets create a new column and populate it with the same information shown at the bottom of the Order details page for each order.

manage_edit-shop_order_columns

This snippet adds the new column, positioned right of the billing address column, but doesn't populate it (see next snippet for that).

add_filter( 'manage_edit-shop_order_columns', 'add_course_details_column', 20 );
function add_course_details_column( $columns ) {
    $new_columns = array();
    foreach ( $columns as $column_name => $column_info ) {
        $new_columns[ $column_name ] = $column_info;
        if ( 'billing_address' === $column_name ) {
            $new_columns['course_booked'] = __( 'Course(s) booked', 'woocommerce' );
        }
    }
    return $new_columns;
}

manage_shop_order_posts_custom_column

This snippet adds content to the new 'Course Date' column we created in the previous step. At the same time we use preg_replace to get the column content to look a bit better.

add_action( 'manage_shop_order_posts_custom_column', 'add_course_details_column_content' );
function add_course_details_column_content ( $column ) {
	global $post, $the_order;
	if ( empty( $the_order ) || $the_order->get_id() != $post->ID ) {
        $the_order = wc_get_order( $post->ID );
    	}
	if ( $column === 'course_booked' ){
		$items	= $the_order->get_items();
		$patterns = array ('/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/',
                   '/\{"type":.*":/',
                   '/\[/',
                   '/"/',
                   '/\,/',
                   '/\]/',
                   '/\}/');
		$replace = array ('\4/\3/\1\2', '', '', '', ' to ', '', '');
    		foreach ( $items as $item ) {
    			$product_name = $item['name'];
    			$product_id = $item['product_id']; // post id
    			$product_variation_id = $item['variation_id'];
    			echo preg_replace($patterns, $replace, "$product_name (ID #$product_variation_id)<br>");
    		}
	}
}