How to Add Custom Data to WooCommerce Order – Part 2

    Akshaya Rane
Listen to this article

custom-data-woocommerce-orderWe have previously seen how we can add custom data to a WooCommerce order in 6 simple steps. That approach comprised of the addition of data to a custom session, extracting it from the WooCommerce Session, and inserting it into the Cart. The data would then be displayed on the Cart and Checkout Page.

However, this approach was limited to WooCommerce versions prior to v3.0.

Now, we have a solution for v3.0 and above, with a brand new strategy as well!

Most of the steps in the procedure remain the same. However, we are doing away with the need to add data to the WooCommerce Session. Instead, we will be adding the data directly to the WooCommerce Cart.

Let’s see how. Here is the code compatible with WooCommerce 3.0+.

Step 1: Adding Custom Field for Product

We previously made use of a WooCommerce session for adding custom fields in WooCommerce session & ultimately to WooCommerce Cart object. Let’s consider the alternative method for this.

The code below can be used to add a Custom Field for Product.


 * Adds custom field for Product
 * @return [type] [description]
function wdm_add_custom_fields()

    global $product;


        <div class="wdm-custom-fields">
            <input type="text" name="wdm_name">
        <div class="clear"></div>


    $content = ob_get_contents();

    return $content;

If we need to modify Price based on the parameter selection/value entered by the User in our custom fields, we can use ‘woocommerce_before_calculate_totals‘ action to set additional Price.

Step 2: Add Customer Data to WooCommerce Cart

Now, we need to add customer entered data into WooCommerce Cart. We can achieve this using the following functionality. Here, we are setting key as ‘name‘.


 * Add custom data to Cart
 * @param  [type] $cart_item_data [description]
 * @param  [type] $product_id     [description]
 * @param  [type] $variation_id   [description]
 * @return [type]                 [description]
function wdm_add_item_data($cart_item_data, $product_id, $variation_id)
        $cart_item_data['wdm_name'] = sanitize_text_field($_REQUEST['wdm_name']);

    return $cart_item_data;

Step 3: Display Details as Meta in Cart

We can display the details entered by User as meta in Cart using the below snippet.


 * Display information as Meta on Cart page
 * @param  [type] $item_data [description]
 * @param  [type] $cart_item [description]
 * @return [type]            [description]
function wdm_add_item_meta($item_data, $cart_item)

    if(array_key_exists('wdm_name', $cart_item))
        $custom_details = $cart_item['wdm_name'];

        $item_data[] = array(
            'key'   => 'Name',
            'value' => $custom_details

    return $item_data;

Step 4: Add Custom Details as Order Line Items

We need to add the below code to add custom details entered by customers as extra details for order line items, using the following code.

add_action( 'woocommerce_checkout_create_order_line_item', 'wdm_add_custom_order_line_item_meta',10,4 );

function wdm_add_custom_order_line_item_meta($item, $cart_item_key, $values, $order)

    if(array_key_exists('wdm_name', $values))


That’s it! You are good to go! If you have any query, feel free to add it in the comments section below.

Is WooCommerce customization or development still giving you a headache?

Talk to us and we can help you out!

Akshaya Rane

Akshaya Rane

31 Responses

  1. Hi,
    Your article is awesome.

    I am using woocommerce 3.2.5 and I put your code in functions.php. and If I var_dumped the user posted data means it shows nothing. Please help me.

    1. Hi Boopathi,
      I suppose you are working at Step 2 and you have specified the name for a text field or custom field you have added.
      In this case, you should see values in $_REQUEST var_dump.

      1. Hi again,

        I used modal for adding custom field to the product skipped your step1 and I var_dump($_REQUEST); my custom field is not added to cart. Any help?. Or else I need to use step 1 for the modal?

        1. Hi Boopathi,
          You can either follow step 1 or else you could add a hidden field in step 1 and after submitting values in ‘Popup’ you could update hidden field values.

  2. Great article! It works out of the box. But if I want to use this functionality in the cart view. Let’s say I want to add a specific address detail to every product but after the product has been added to the cart. Do you have any suggestions on how to do that?

    1. Hi Marku,
      This is out of the scope of this article. You can refer to specific actions in Cart template to add the field and on Update cart action / after some custom action you can add user added data to the cart object. After this, you can follow the same steps to add the details in Order.

  3. How do you get the custom field you add to include on the email template or into the product name of the POS system after purchase? I can’t get it to show up anywhere besides cart and checkout..

    1. You must have saved the meta value by prefixing with ‘_’ as stated in the example, ‘_wdm_name’. You can save the details directly with meta key without prefixing it with ‘_’, This can display the details in the Order and email template. If you need to change the position where it is displayed or any other requirement, you need to add custom code which is beyond the scope of the current article.

  4. Firstly i want to thank you for such a amazing article. It just worked out of the box.
    My only issue is i have more than one custom input field so was figuring out how it wil work as i tried to extent the code for another input field also but its displaying only last inputed field in cart.

  5. Hi Akshaya, thank you for sharing this article to add custom data in WooCommerce. I just need your help as i am trying to display customer information but i am having some error in the code as i have seen this from the article .

    add_action( ‘woocommerce_admin_order_data_after_billing_address’, ‘blog_anothermethod_display’);
    function blog_anothermethod_display( $order ){
    $order_id =43;
    $order_meta = get_post_meta($order_id);

  6. Hello,
    not sure what this section needs to be changed to, any help would be appreciated

    Just need to gather x4 form fields

    * Adds custom field for Product
    * @return [type] [description]

  7. Thanks for a clear write up for customization. I wanted to know how can I have the custom field appear for a specific product category?


  8. Hi Akshaya

    Thanks for this article. It would really helpful for me. I have some questions, i.e., This script working only on a single product page. I would like to implement the same in shop page grid view. Is this possible to send a form value in add_to_cart_ajax?. Please help me out of this. Thanks in advance.

  9. Thanks for the article, everything is working fine but custom field information were not adding into product invoice, can u pls help me ,how to send info related with current product to product invoice

  10. Hi

    Many (many, many) thanks for this article!!

    I am a wedding photographer, for years I have tested every possible plugin or integrated solution to sell prints while showing the photos to the weddings guests.
    During wedding seasons it is at least 2500 photos to put online that I keep online between 6 months and one year.

    I had ended up with a woocommerce shop, making a product from each photo as I found a plugin to do this. But it is verrrrryyyyyyyy long and painful, not speaking of millions lines database tables, and the lack of ergonomic, obliging customers to have different pages for viewing photos or purchasing prints.

    Your article made my day. As i used to be a software developper, last week-end, I closed the door of my home office for 3 days to get this:

    It is based on Justified Grid Image which is in my opinion the best gallery plugin.

    And you know what? There is only 1 woocommerce product to make all this 🙂

    And it is the best “gallery viewing and prints selling” I have ever seen. Everything can be donne from the photo lightbox, download, purchasing, remaining on the same page, with the ability to go on through the photos!

    For those who want to have emails with meta, here is what I use:

    add_filter( ‘woocommerce_order_item_meta_end’, ‘custom_woocommerce_email_order_meta_fields’, 10, 3 );
    function custom_woocommerce_email_order_meta_fields( $item_id, $item, $order) {
    echo ‘ ‘.$wdm;

    So again, many many thanks!

    After 3 days of trial and error and trawling incorrect info, I came accross this solution
    ***** 5 Star
    Spot on, I am now able to add everything i need to on the flay to the cart and final order.
    Extremely thankful

  12. I see that I managed to post this in your last post (for older woocommerce) – is there any way of using this to “Add to Cart” from a custom post using javascript and sending some parameters as custom data to the product?

    I appreciate any help I can get 🙂

  13. Hi
    thanks for the best solution for custom fields.

    i extended this with at least 23 fields but after the final update, i am missing one field in final order details in woocommerce order. although its showing on cart page and checkout page but not on admin order page and also on emails.

    checked all things but no solution. Sometimes the field missing is the random sometime first field.

    Please help

  14. I want to get the value of that custom field key ‘wdm_name’ and place it my order notes. But below code doesnt seem to work:

    add_action( ‘woocommerce_new_order’, ‘add_engraving_notes’ );
    function add_engraving_notes( $order_id ) {

    $order = wc_get_order( $order_id );
    $note = get_post_meta( $order_id, ‘wdm_name’, true );
    $order->add_order_note( $note );

    Please advise.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads