skip to Main Content

I’m developing a WordPress plugin with WooCommerce so the customer can add customizable dynamic products to their cart. In order to get the customized data into the cart, I have the following lines:

WC()->add_to_cart($product_id, $quantity, 0, [], [ "custom_data" => $custom_data ]);

Where $custom_data stores the customized information of the product (coordinates and dimensions basically).
This works properly.

When a customer actually buys the product, a new order is created and I can view it in my administrator panel from WooCommerce and I want the custom data to be displayed for every item on the order in order to then manufacture the item given the coordinates and dimensions, but I don’t want the customer to see these data at any point because it makes no sense. (The data is just a stringified JSON).

I’ve tried this:

function add_custom_data_to_order($item, $cart_item_key, $values, $order) {
    if (isset($values["custom_data"]) {
        $item->add_meta_data("custom_data", $values["custom_data"]);
    }
}

add_action("woocommerce_checkout_create_order_line_item", "add_custom_data_to_order", 10, 4);

This works but it’s not what I’m trying to achieve since the data is shown to the user during checkout and it’s displayed on the admin order view for the item as a property of the item, not as extra information.

How can I achieve this?

2

Answers


  1. You need to prepend an underscore to your metadata key, you can see that here:

    WC()->add_to_cart($product_id, $quantity, 0, [], [ "_custom_data" => $custom_data ]);
    

    This should hide it from the front-end for the user, then you can add the following filter and function to display it on the admin order page:

    add_filter('woocommerce_order_item_display_meta_key', 'display_custom_meta_key', 20, 3 );
    function display_custom_meta_key( $meta_key, $meta, $item ) {
    
        if( $meta->key == '_custom_data' && is_admin() ){
            $meta_key = __("Custom Data", "woocommerce" );
        }
    
        return $meta_key;    
    }
    
    Login or Signup to reply.
  2. First, you have a mistake in WC()->add_to_cart( that should be WC()->cart->add_to_cart( instead, if you want to add a product to cart. There is also a missing closing bracket ) in your function.

    You can hide custom order item metadata, from customer, in 2 different ways:

    1. Adding an underscore before the meta key, inside your function (and replace the displayed meta key with a readable label name):
    add_action("woocommerce_checkout_create_order_line_item", "add_custom_data_to_order", 10, 4);
    function add_custom_data_to_order($item, $cart_item_key, $values, $order) {
        if ( isset($values['custom_data']) ) {
            // Meta key starting an underscore
            $item->add_meta_data('_custom_data', $values['custom_data']);
        }
    }
    
    // Add readable "meta key" label name replacement
    add_filter('woocommerce_order_item_display_meta_key', 'custom_order_item_displayed_meta_key', 10, 3 );
    function custom_order_item_displayed_meta_key( $display_key, $meta, $item ) {
        if( $item->get_type() === 'line_item' ) {
            if( $meta->key === '_custom_data' ) {
                $display_key = __('Custom data', 'woocommerce');
            }
        }
        return $display_key;
    }
    
    1. Or using the following hooked function:
    add_filter( 'woocommerce_order_item_get_formatted_meta_data', 'hide_specific_order_item_meta_data', 10, 2);
    function hide_specific_order_item_meta_data($formatted_meta, $item){
        // Targeting only admin
        if( is_admin() ) 
            return $formatted_meta;
    
        foreach( $formatted_meta as $key => $meta ){
            // Here we target your meta key
            if( $meta->key === 'custom_data' )
                // Remove displayed meta data from front end
                unset($formatted_meta[$key]);
        }
        return $formatted_meta;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search