• Resolved MagusTSF

    (@magustsf)


    Hi

    Is there any way to set the order that items are displayed in the order emails? I have already customized the templates to give the layout we want but cannot find any reference for sorting the list of items.

    At the moment they are listed in the order they were added to the cart and I would like to sort them by the SKU

    Simon

    https://wordpress.org/plugins/woocommerce/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    This isn’t something we support in the get_items() function. They come out and display as they were created during checkout.

    Thread Starter MagusTSF

    (@magustsf)

    If it cannot be handled there, then what if I re-sort the array prior to parsing it through the template?

    before this line in the template
    foreach ( $items as $item_id => $item ) :

    reprocess the array through a custom function?

    Plugin Contributor Mike Jolley

    (@mikejolley)

    You could loop over the items and put them in another array rather than output them. THEN loop to display as we do in the template as-is.

    Thread Starter MagusTSF

    (@magustsf)

    I hope you do not mind if I run this past you. Because the sku is stored separately to the order details I am unsure if I am on the right path and do not want to mess up an already live site because the customer has requested alterations after 12 months.

    I have looked at using usort to rearrange the array but am having problems trying to figure out how to get it to compare with the sku obtained from the product meta.

    Any insights would be most welcome.

    Thread Starter MagusTSF

    (@magustsf)

    Nevermind, got it working by populating a new array with the line details and sorting that.

    My solution in case anyone else is interested

    <?php
    /**
     * Email Order Items
     *
     * This template can be overridden by copying it to yourtheme/woocommerce/emails/email-order-items.php.
     *
     * HOWEVER, on occasion WooCommerce will need to update template files and you (the theme developer).
     * will need to copy the new files to your theme to maintain compatibility. We try to do this.
     * as little as possible, but it does happen. When this occurs the version of the template file will.
     * be bumped and the readme will list any important changes.
     *
     * @see 	    http://docs.woothemes.com/document/template-structure/
     * @author 		WooThemes
     * @package 	WooCommerce/Templates/Emails
     * @version     2.1.2
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly
    }
    $myorder = array();
    foreach ( $items as $item_id => $item ) :
    	$_product     = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
    	$item_meta    = new WC_Order_Item_Meta( $item, $_product );
    	$categories = get_the_terms( $_product->id, 'product_cat' ); 
    
    	//get the product category
    	if ( $categories && ! is_wp_error( $category ) ) : 
    
        // loop through each cat
        foreach($categories as $category) :
    		if($category->parent == 0){
    		   $cat = $category->name;
    		   //break;
    		}
        endforeach;
    
    	endif;
    
    	//loop items into a sortable array
    	$myorder[] = array('id' => $item, 'sku' => $_product->get_sku(), 'name' => apply_filters( 'woocommerce_order_item_name', $item['name'], $item, false ), 'cat' => $cat, 'price' => $_product->price, 'qty' => apply_filters( 'woocommerce_email_order_item_quantity', $item['qty'], $item ), 'subtot' => $order->get_formatted_line_subtotal( $item ) );
    
    endforeach;
    
    	//resort array by sku
    	  function compare_sku($a, $b)
      {
        return strnatcmp($a['sku'], $b['sku']);
      }
    
      // sort alphabetically by name
      usort($myorder, 'compare_sku');
    
    foreach ( $myorder as $itemline ) {
    	if ( apply_filters( 'woocommerce_order_item_visible', true, $itemline['id'] ) ) {
    		?>
    		<tr>
    			<td style="text-align:left; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word; padding: 0; padding-left: 12px;"><?php echo $itemline['sku'] ;?></td>
    			<td style="text-align:left; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word; padding: 0; padding-left: 12px;"><?php echo $itemline['name'] ;?></td>
    			<td style="text-align:left; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word; padding: 0; padding-left: 12px;"><?php echo $itemline['cat'] ;?></td>
    			<td style="text-align:right; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word; padding: 0;"><?php echo $itemline['price']; ?></td>
    			<td style="text-align:right; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word; padding: 0;"><?php echo $itemline['qty']; ?></td>
    			<td style="text-align:right; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word; padding: 0;"><?php echo $itemline['subtot']; ?></td>
    		</tr>
    		<?php
    	}
    
    	}
    ?>
Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘email order items table sorting’ is closed to new replies.