Skip to content

Instantly share code, notes, and snippets.

@itzmekhokan
Created July 11, 2019 17:06
Show Gist options
  • Save itzmekhokan/d0f1b79d60c0817d8db6320c714d67ce to your computer and use it in GitHub Desktop.
Save itzmekhokan/d0f1b79d60c0817d8db6320c714d67ce to your computer and use it in GitHub Desktop.
Woocommerce email order items filter by categories
<?php
function modify_wc_email_order_items_args( $args ) {
if( isset( $args['order'] ) && $args['order'] ){
$items = ( isset( $args['items'] ) && $args['items'] ) ? $args['items'] : array();
if( $items ) :
$cat_wisw_pros = array();
foreach ( $items as $item_id => $item ){
$product_id = $item['product_id'];
$cat_ids = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) );
foreach ( $cat_ids as $id ) {
$cat_wisw_pros[$id][$item_id] = $item;
}
}
ksort( $cat_wisw_pros ); // Cat ID wise sort
$grouped_order_items = array();
foreach ( $cat_wisw_pros as $cat_id => $order_items ) {
foreach ( $order_items as $item_id => $item ) {
if( !array_key_exists( $item_id, $grouped_order_items ) )
$grouped_order_items[$item_id] = $item;
}
}
$args['items'] = $grouped_order_items;
endif;
}
return $args;
}
add_filter( 'woocommerce_email_order_items_args', 'modify_wc_email_order_items_args', 99 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment