Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Last active November 21, 2019 07:01
Show Gist options
  • Save plugin-republic/42763da835450aa956728ca968618c93 to your computer and use it in GitHub Desktop.
Save plugin-republic/42763da835450aa956728ca968618c93 to your computer and use it in GitHub Desktop.
Iterate through product extras meta in order
<?php
function pewc_check_order_meta( $product_name, $item ) {
if( isset( $item['product_extras']['groups'] ) ) {
foreach ( $item['product_extras']['groups'] as $groups ) {
if( $groups ) {
$product_name .= '<ul>';
foreach( $groups as $group ) {
if( isset( $group['type'] ) ) {
// if( isset( $group['type'] ) && empty( $group['flat_rate'] ) ) {
$classes = array( strtolower( str_replace( ' ', '_', $group['type'] ) ) );
$classes[] = strtolower( str_replace( ' ', '_', $group['label'] ) );
// Display the price
$price = '';
$hide_zero = get_option( 'pewc_hide_zero', 'no' );
// Calculate price
if( isset( $group['price'] ) ) {
if( $hide_zero != 'yes' && $group['price'] != '0.00' ) {
global $product;
$price = pewc_maybe_include_tax( $product, $group['price'] );
$price = ' ' . wc_price( $price );
}
}
if( $group['type'] == 'upload' ) {
$display = sprintf(
'<li class="%s"><span class="pewc-order-item-label">%s:</span> <span class="pewc-order-item-item">%s</span> <span class="pewc-order-item-price">%s</span><img src="%s"></li>',
join( ' ', $classes ),
$group['label'],
$group['display'],
$price,
esc_url( $group['url'] )
);
$product_name .= $display;
} else if( $group['type'] == 'checkbox' ) {
$product_name .= '<li class="' . join( ' ', $classes ) . '"><span class="pewc-order-item-label">' . $group['label'] . '</span> <span class="pewc-order-item-price">' . $price . '</span></li>';
} else {
$product_name .= '<li class="' . join( ' ', $classes ) . '"><span class="pewc-order-item-label">' . $group['label'] . ':</span> <span class="pewc-order-item-item">' . $group['value'] . '</span> <span class="pewc-order-item-price">' . $price . '</span></li>';
}
}
}
$product_name .= '</ul>';
}
}
}
return $product_name;
}
// Update with your own filter
add_filter( 'woocommerce_order_item_name', 'pewc_check_order_meta', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment