Stephen
Stephen

Reputation: 8218

WooCommerce hide order item meta from front end (not admin)

I have a couple of order item meta details that I would not like the customer to see (on the view orders page under account info). I have found a filter that will remove this data from the ADMIN (where I would still like to see it), but can't find a similar filter to remove it from the FRONT END (where it should be hidden).

Here is the code that will (uselessly, for me) remove it from the back end admin:

add_filter( 'woocommerce_hidden_order_itemmeta', 'add_hidden_order_items' );
function add_hidden_order_items( $order_items ) {
    $order_items[] = 'paid_already';
    $order_items[] = 'variation_sku';
    // and so on...
    return $order_items;
}

Upvotes: 6

Views: 10106

Answers (6)

Fikret Çin
Fikret Çin

Reputation: 31

use " _ " before order item meta meta_key it will be hide automatically

Upvotes: 3

passatgt
passatgt

Reputation: 4432

Save the values with an underscore prefix, that way it won't be displayed, just saved. Like so:

$item->add_meta_data('_hidden_field', '123', true);

Upvotes: 18

Carl
Carl

Reputation: 51

Dealing with item meta data, I'm simply hiding the item meta data from the cart using CSS:

.wc-item-meta {display:none;}

Upvotes: 0

Hà Bầu
Hà Bầu

Reputation: 91

//remove order item meta key
add_filter( 'woocommerce_order_item_get_formatted_meta_data', 'mobilefolk_order_item_get_formatted_meta_data', 10, 1 );

function mobilefolk_order_item_get_formatted_meta_data($formatted_meta){
    $temp_metas = [];
    foreach($formatted_meta as $key => $meta) {
        if ( isset( $meta->key ) && ! in_array( $meta->key, [
                'lyric_id',
                'lyric_song_title',
                'lyric_artist_name'
            ] ) ) {
            $temp_metas[ $key ] = $meta;
        }
    }
    return $temp_metas;
}

Upvotes: 6

Stephen
Stephen

Reputation: 8218

Well, it ended up being much easier than I was supposing, the template actually already puts out a class with the name of my item meta, so I just hid it in the css like so:

.order_details .variation-variation_sku, .order_details .variation-paid_already {
    display: none !important;
}

While it would be nice to know how to prevent these item meta from ever being output, I can live with this as a solution.

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 253824

if you want to hide some items meta from front end order view, you will need to override
order-details-item.php woocommerce template file.

I recommend you: Overriding woocommerce templates via a Theme method, copying templates woocommerce folder to your active theme (or child theme better) and renaming it "woocommerce". Once copied in that woocommerce folder (inside your active theme), you will find order-details-item.php in order subfolder.

In line 36 of order-details-item.php you will find this:

            $order->display_item_meta( $item );
            $order->display_item_downloads( $item );

You can change this adding a conditional like:

            if ( $item != 'paid_already' || $item != 'variation_sku') {
                $order->display_item_meta( $item );
                $order->display_item_downloads( $item );
            }

I am not sure of the veracity of this conditional and can't test it. You may be obliged to change it a bit…

just for information, function display_item_meta() works this way:

/**
 * Display meta data belonging to an item.
 * @param  array $item
 */
public function display_item_meta( $item ) {
    $product   = $this->get_product_from_item( $item );
    $item_meta = new WC_Order_Item_Meta( $item, $product );
    $item_meta->display();
}

References:

Upvotes: 2

Related Questions