Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save plugin-republic/d1f5c80d6fc0794d87b040f55267a746 to your computer and use it in GitHub Desktop.

Select an option

Save plugin-republic/d1f5c80d6fc0794d87b040f55267a746 to your computer and use it in GitHub Desktop.
<?php
add_filter( 'woocommerce_order_item_display_meta_value', 'my_pewc_hide_field_value_by_type', 20, 3 );
function my_pewc_hide_field_value_by_type( $display_value, $meta, $order_item ) {
if ( ! is_admin() ) {
return $display_value;
}
// Field types to hide the entered value for (price-only display)
$target_types = array( 'radio-group', 'calculation' );
$field = my_pewc_find_field_by_meta_key( $meta->key, $order_item );
if ( ! $field || empty( $field['type'] ) || ! in_array( $field['type'], $target_types, true ) ) {
return $display_value;
}
// Keep only the wc_price() markup, drop the entered value
if ( preg_match( '/<span class="woocommerce-Price-amount[^"]*">.*?<\/span>(<\/bdi>)?/s', $display_value, $matches ) ) {
return $matches[0];
}
return '';
}
function my_pewc_find_field_by_meta_key( $meta_key, $order_item ) {
$product_extras = $order_item->get_meta( 'product_extras' );
if ( empty( $product_extras['groups'] ) ) {
return false;
}
// Strip the leading underscore PEWC adds to the meta key
$target_label = ltrim( $meta_key, '_' );
$all_groups = pewc_rebuild_cart_item_data( $product_extras, 'order' );
foreach ( $all_groups as $group ) {
if ( ! $group ) {
continue;
}
foreach ( $group as $field_id => $field ) {
if ( ! is_array( $field ) ) {
continue;
}
$label = ! empty( $field['label'] ) ? $field['label'] : ( $field['id'] ?? '' );
if ( $label === $target_label ) {
return $field;
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment