Skip to content

Instantly share code, notes, and snippets.

@1ucay
Last active September 9, 2016 11:20
Show Gist options
  • Save 1ucay/893b5a29fb0adc83d23340011bc1887e to your computer and use it in GitHub Desktop.
Save 1ucay/893b5a29fb0adc83d23340011bc1887e to your computer and use it in GitHub Desktop.
Woocommerce product variation nicename in Cart and Order
add_filter( 'woocommerce_get_item_data', 'woocommerce_get_item_data', 10, 2);
function woocommerce_get_item_data($item_data, $cart_item) {
$variation = $cart_item['variation'];
// Format item data ready to display
foreach ( $item_data as $key => $data ) {
// Set hidden to true to not display meta on cart.
if ( ! empty( $data['hidden'] ) ) {
unset( $item_data[ $key ] );
continue;
}
$taxonomy = array_search ($data['value'], $variation);
$meta = get_post_meta($cart_item['variation_id'], 'attribute_'.$taxonomy, true);
$term = get_term_by('slug', $meta, $taxonomy);
if ( !empty( $term->name ) )
$data['display'] = $term->name;
else
$data['display'] = str_replace('-',' ',$data['value'] );
$item_data[ $key ]['key'] = ! empty( $data['key'] ) ? $data['key'] : $data['name'];
$item_data[ $key ]['display'] = ! empty( $data['display'] ) ? $data['display'] : $data['value'];
}
return $item_data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment