2

I have a plugin for Woocommerce for custom metas. I have four but two of them shouldn't be shown in the customer E-Mails (but in my backend). So I try to edit the email-order-items.php and delete the standard wc_display_item_meta( $item ); In the first part of my code I try to delete two of them and in the second part I try to display the other two

$_product = apply_filters('woocommerce_order_item_product', $order->get_product_from_item($item), $item);

        $item_meta = wc_gzdp_get_order_meta($_product, $item);
        $item_meta_print = '';

        if ($item_meta->meta) {

            if (is_array($item_meta->meta)) {
                foreach ($item_meta->meta as $key => $value) {
                    if (stripos($key, "_wccf_pf_donotshow") === 0 || 
                    stripos($key, "_wccf_pf_donotshow2") === 0) {
                        unset($item_meta->meta[$key]);
                    }
                }
            }
            $item_meta_print = $item_meta->display(true, true, '_', ", ");

            if(isset($item_meta->meta['_wccf_pf_show'])) {
                $item_meta_print .= ', Name: '. $item_meta->meta[_'wccf_pf_show'];
            } elseif(isset($item_meta->meta['_wccf_pf_show2'])) {
                $item_meta_print .= ', Name2: '. $item_meta->meta['_wccf_pf_show2'];
            }

        }

This works fine with previous Woocommerce version but not any longer in Woocommerce 3. No metas anymore in the customer E-Mails :-(

I would be so grateful for any help.

Aurelia
  • 21
  • 1
  • 2
  • 1
    Any meta key that begins with `_` ie: `_hidden_meta_key` will not be shown via `wc_display_item_meta()` so you could try renaming your meta keys and reverting to the default function. – helgatheviking Aug 26 '17 at 00:13
  • I delete the _ in the metas and now every item meta appears :-( By the way, the same code works with my PDF invoice – Aurelia Aug 26 '17 at 08:02
  • 1
    Prefixing a meta with `_` hides it so, removing that prefix will show it. my suggestion was only to prefix the meta fields you need to hide. THough keep in mind that changing the name does have implications if you need to maintain backwards compatibility... like if you've already saved a bunch of data. – helgatheviking Aug 27 '17 at 09:34

2 Answers2

3

This will hide a key everywhere on the front-end, by filtering the results of woocommerce_order_item_get_formatted_meta_data:

/**
 * Hide container meta in emails.
 *
 * @param  array  $meta
 * @return array
 */
function kia_hide_mnm_meta_in_emails( $meta ) {
    if( ! is_admin() ) {
        $criteria = array(  'key' => 'Part of' );
        $meta = wp_list_filter( $meta, $criteria, 'NOT' );
    }
    return $meta;
}
add_filter( 'woocommerce_order_item_get_formatted_meta_data', 'kia_hide_mnm_meta_in_emails' );

I don't think there's a conditional to only do this filter in emails only.

helgatheviking
  • 25,596
  • 11
  • 95
  • 152
  • not work if admin change status of order or reset order details. see https://stackoverflow.com/a/61126082/5447232 – MrSwed Oct 02 '20 at 17:05
0

I found out a solution. Maybe it helps someone else

              if ( $item != '_wccf_pf_donotshow' || $item != '_wccf_pf_donotshow2') {
            $order->display_item_meta( $item );

        }

                $_product = apply_filters('woocommerce_order_item_product', $order->get_product_from_item($item), $item);

        $item_meta = wc_gzdp_get_order_meta($_product, $item);


        if ($item_meta->meta) {

            if(isset($item_meta->meta['_wccf_pf_show'])) {
             echo 'Name: ' . $order->get_item_meta($item_id, '_wccf_pf_show', true) . '<br><br>';
            } elseif(isset($item_meta->meta['_wccf_pf_show2'])) {
             echo 'Name2: ' . $order->get_item_meta($item_id, '_wccf_pf_show2', true) . '<br><br>';
            }

        }
Aurelia
  • 21
  • 1
  • 2