• This is how I set custom meta data for a product which gets added to basket:

    add_action( 'woocommerce_checkout_create_order_line_item', 'save_cart_item_custom_meta_as_order_item_meta', 10, 4 );
    function save_cart_item_custom_meta_as_order_item_meta( $item, $cart_item_key, $values, $order ) {
        
        $meta_key1 = 'Product 1';
        
        if ( isset($values['add_size']) && isset($values['add_size'][$meta_key1]) ) {
            $item->update_meta_data( $meta_key1, $values['add_size'][$meta_key1] );
        }

    I need to remove this custom meta data after a purchase (‘woocommerce_thankyou’) but first I need to use it to add a product to the order.

    add_action('woocommerce_thankyou', 'add_order_gift_note', 10, 1);
    function add_order_gift_note( $order_id ) {
        foreach ( $order->get_items() as $item_id => $item ) {
    $extras = $item->get_formatted_meta_data('_', true);
                    foreach($extras as $extra){
                    $mini_id = $extra->value;                       
    $order->add_product( wc_get_product($mini_id), $product_quantity); // Add Minis 
    }
    // Now hide or remove the meta data.

    Either removing it or hiding some meta data from the order at this point is the desired solution.

    I don’t want the IDs to show in the orders: https://imgur.com/a/taBHRMh

    • This topic was modified 5 years, 5 months ago by swerlz.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You could likely hide the IDs with appropriate CSS rules in the UI, but if they are also in confirmation emails you’d have to suppress their inclusion in the email. One possible way to alter email content is through the “wp_mail” filter, but it’s not ideal since all outgoing emails go through this filter. There’s likely a specific order confirmation filter applied by WooCommerce that would be better to use. For specifics about WooCommerce filters, refer to their docs or inquire through their dedicated support forum.

Viewing 1 replies (of 1 total)

The topic ‘Remove MetaData from OrderItem’ is closed to new replies.