Group items in woocommerce including sub items

I had a client, who wanted to change woocommerce order page so that he can group the items

this becomes an issue when there are many orders and many options to it.

to explain it to you better, suppose there is a pizza company, client orders 30 differnet pizzas from him , and has different spice packets in each of the order, there is a total of say 70 spice packets that have to be put in the order

now the supply chain has to go to each order 30 times and count the number of spice packets in it, instead of that, it would have been better if there were a total of the orders as 70 spice packets, so that the supply chain can just put 70 spice packets at once, and he doesn’t has to count it 30 times.

to explain it to you better see this screenshot.

 

1

take a look at sub order number 1-3 and 2-4 you will know the issue

 

this is what this script does, it changes the order email to show number of times a particular product is to be put in the delivery, saving much time.

this was on the order page

2

 

if you want these two files to survive woocommerce update, then please put it in your themes folder

 

plugins/woocommerce/templates/single-product/price.php

themes/youractivetheme/woocommerce/single-product/price.php

( template is not included in it)

it will be in this format

 

two files are to be edited. for this purpose

one file is – class-wc-order-item-meta.php

this is situated in

/wp-content/plugins/woocommerce/classes

rename old file as class-wc-order-item-meta_old.php ( you can even copy it to the other location if you want it to survive modifications)

 

 

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
error_reporting(0);
/**
* Order Item Meta
*
* A Simple class for managing order item meta so plugins add it in the correct format
*
* @class         order_item_meta
* @version        2.0.4
* @package        WooCommerce/Classes
* @author         WooThemes
*/
class WC_Order_Item_Meta {

public $meta;

/**
* Constructor
*
* @access public
* @param string $item_meta (default: '')
* @return void
*/
public function __construct( $item_meta = array() ) {
$this->meta = $item_meta;
}

/**
* Display meta in a formatted list
*
* @access public
* @param bool $flat (default: false)
* @param bool $return (default: false)
* @param string $hideprefix (default: _)
* @return void
*/
public function display( $flat = false, $return = false, $hideprefix = '_' ) {
global $woocommerce;

if ( ! empty( $this->meta ) ) {

$output = $flat ? '' : '<dl>';

$meta_list = array();





foreach ( $this->meta as $meta_key => $meta_values ) {




if ( empty( $meta_values ) || ( ! empty( $hideprefix ) && substr( $meta_key, 0, 1 ) == $hideprefix ) )
continue;

foreach( $meta_values as $meta_value ) {

// Skip serialised meta
if ( is_serialized( $meta_value ) )
continue;

// If this is a term slug, get the term's nice name
if ( taxonomy_exists( esc_attr( str_replace( 'attribute_', '', $meta_key ) ) ) ) {
$term = get_term_by('slug', $meta_value, esc_attr( str_replace( 'attribute_', '', $meta_key ) ) );
if ( ! is_wp_error( $term ) && $term->name )
$meta_value = $term->name;
}

if ( $flat ){
$tempKey = $woocommerce->attribute_label( str_replace( 'attribute_', '', $meta_key ) );
$meta_list[] = esc_attr( $tempKey . ': ' . $meta_value );
}else{
$tempKey = wp_kses_post( $woocommerce->attribute_label( str_replace( 'attribute_', '', $meta_key ) ) );
$meta_list[] = '<dt>' . $tempKey . ':</dt><dd>' . wp_kses_post( $meta_value ) . '</dd>';
}
$mhdMetas[$tempKey] = $meta_value;

}
}

/*EDITED BY MOHAMMAD*/
$allMetas = array();
$newArray = array_count_values($mhdMetas);
foreach ($newArray as $meta_key=>$meta_values){

$key=array_search($meta_key, $mhdMetas);
if($meta_values>1)
$allMetas[$key] = "$meta_values x $meta_key <br/>";
else
$allMetas[$key] = "1 x $meta_key <br/>";
}
$meta_list = $allMetas;
/*END*/





if ( $flat )
$output .= implode( ", \n", $meta_list );
else
$output .= implode( '', $meta_list );

if ( ! $flat )
$output .= '</dl>';

if ( $return )
return $output;
else
echo $output;
}
}
}

email-order-items.php

this is the second one -

/wp-content/plugins/woocommerce/templates/emails

 

if you need any help, dont’ hesitate to contact us.

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
//sort items by Fargold.com

$allItems = array();
foreach ($items as $key=>$item){

if (in_array($item['name'],$allItems)){
$tkey = array_search($item['name'], $allItems);
$items[$tkey]['qty'] = $items[$tkey]['qty'] + $items[$key]['qty'];
unset($items[$key]);
}else{
$allItems[$key] = $item['name'];
}
}

/**
* Email Order Items
*
* @author         WooThemes
* @package     WooCommerce/Templates/Emails
* @version     2.0.3
*/

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $woocommerce;

foreach ($items as $item) :

// Get/prep product data
$_product = $order->get_product_from_item( $item );
$item_meta = new WC_Order_Item_Meta( $item['item_meta'] );
$attachment_image_src = wp_get_attachment_image_src( get_post_thumbnail_id( $_product->id ), 'thumbnail' );
$image = ( $show_image ) ? '<img src="' . current( $attachment_image_src ) . '" alt="Product Image" height="' . $image_size[1] . '" width="' . $image_size[0] . '" style="vertical-align:middle; margin-right: 10px;" />' : '';

?>
<tr>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee; word-wrap:break-word;"><?php

// Show title/image etc
echo     apply_filters( 'woocommerce_order_product_image', $image, $_product, $show_image);

// Product name
echo     apply_filters( 'woocommerce_order_product_title', $item['name'], $_product );

// SKU
echo     ($show_sku && $_product->get_sku()) ? ' (#' . $_product->get_sku() . ')' : '';

// File URLs
if ( $show_download_links && $_product->exists() && $_product->is_downloadable() ) {

$download_file_urls = $order->get_downloadable_file_urls( $item['product_id'], $item['variation_id'], $item );

$i = 0;

foreach ( $download_file_urls as $file_url => $download_file_url ) {
echo '<br/><small>';

$filename = woocommerce_get_filename_from_url( $file_url );

if ( count( $download_file_urls ) > 1 ) {
echo sprintf( __('Download %d:', 'woocommerce' ), $i + 1 );
} elseif ( $i == 0 )
echo __( 'Download:', 'woocommerce' );

echo ' <a href="' . $download_file_url . '" target="_blank">' . $filename . '</a></small>';

$i++;
}
}

// Variation
echo     ($item_meta->meta) ? '<br/><small>' . nl2br( $item_meta->display( true, true ) ) . '</small>' : '';

?></td>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo $item['qty'] ;?></td>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
</tr>

<?php if ($show_purchase_note && $purchase_note = get_post_meta( $_product->id, '_purchase_note', true)) : ?>
<tr>
<td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo apply_filters('the_content', $purchase_note); ?></td>
</tr>
<?php endif; ?>

<?php endforeach;

?>

 

this is the result of these emails.

 

3