Support » Plugin: WooCommerce » Seeing product custom meta field in order

  • Resolved teuptworkshop

    (@teuptworkshop)


    My programming skills are really basic and I don’t know the next thing about PHP, but with some resources, I figured out how to add a custom meta field – a second SKU – to my products, and how to import values for that field with a csv. This is how far I got.

    Now for the last step: How to make the second SKU appear in my received orders? There is a “Order items post meta” dialog that contains a list of values for each product in an order. I want to somehow get my second SKU into that list.

    I noticed the file “woocommerce/includes/admin/meta-boxes/views/html-order-item-meta.php” might have something to do with this but I am unsure how to edit it. If anyone could give me a snippet and tell me what file to add it in, that’d be awesome!

Viewing 6 replies - 1 through 6 (of 6 total)
  • You’ve picked a difficult place to start with PHP! There are several steps.

    Have you added the custom field to the cart?

    Don’t alter core files.

    Here’s a framework but it might need some work before it works for you:

    <?php
    
    // show the custom meta data on the cart and checkout pages
    add_filter( 'woocommerce_get_item_data', 'tp_get_custom_item_data', 10, 2 );
    function tp_get_custom_item_data( $item_data, $cart_item ) {
      if( isset( $cart_item['size'] ) ) {  
        $item_data[] = array( 'name' => 'Size', 'value' => $cart_item['size'] );
      }
      return $item_data;
    }
    
    // add custom meta to order
    add_action( 'woocommerce_new_order_item', 'tp_add_custom_meta_to_order', 99, 3 );
    function tp_add_custom_meta_to_order( $item_id, $item, $order_id ) {
      if( ! isset( $item->legacy_values ) ) { // applies to order_item_shipping
        return;
      }
      if( isset( $item->legacy_values['size'] ) ) {
        wc_add_order_item_meta( $item_id, 'Size', $item->legacy_values['size'] );
      }
    }
    Thread Starter teuptworkshop

    (@teuptworkshop)

    Thanks for your reply! I’m still having a bit of a tough time there. Let’s make sure I explained myself properly before I give another shot at dissecting your suggestion, just to make sure this is going in the right direction:

    I have a second SKU called bl_lotid on every product. It should not be visible to the customer or have any kind of functionality, I only need it to be displayed on the backend when I open an order for picking. Actually pretty much anywhere on that page will do.

    The backstory: I need to sync my inventory with another platform that has the master stock, and to do this, the idea is that I copy the order page (just Ctrl+A Ctrl+C). Then, with some C# skills I make some software that can pull that data apart and convert it to an upload for my master stock. To do this, besides the quantity to substract I need that second SKU to be displayed somewhere in that copied data, so I know how much to substract from which product in the master stock. Only the original SKU field is displayed, but due to other constraints, that one has a different logic and does not match with the master stock’s SKU’s…

    My understanding is that the sequence is:
    – create and populate the custom meta field in the admin product page
    – add the field to the public product page (it can be in a hidden field or in a place which is hidden by css)
    – customer adds the product to the cart, and that takes the field with the product
    – field is in the cart, though the field may not be visible
    – customer purchases, your code adds the field to the order meta
    – to show the field in the emails, custom email templates would be needed, but you don’t have to do that step. Or you can make an email template for the admin notification but not for the customer email.
    – the field should by now be in the order meta so it will show on the admin order screen.

    I don’t think the field can’t go direct from the admin product page to the order. It has to go via the product page, the add-to-cart and the checkout.

    Of course, each step has to be working for the whole thing to work.

    All these steps can be accomplished by hooks in functions.php so no need to alter core files.

    Thread Starter teuptworkshop

    (@teuptworkshop)

    Aha, I wasn’t aware of that. OK, so I modified and added this code, as far as I understand it, the orders should now contain the custom meta field along with the rest of the product data.. now for a way to display it in the backend “edit order” screen..

    Actually I’m surprised this is so complicated. Showing extra fields for orderpicking seems like a fairly basic functionality to me. I sell LEGO pieces on a platform, and hundreds of sellers there work with a hidden remark field that is displayed on each item in their order picking list. I would have expected more plugins around this..

    Once the custom meta has been successfully added to the order meta, it will appear by itself on the order screen.

    There’s quite a few plugins that do custom fields for WooCommerce. Unfortunately the only free one I know about that does admin-side fields is:
    https://wordpress.org/plugins/wc-fields-factory/
    but not sure if it still works.

    The author explained the principles here:
    https://sarkware.com/adding-custom-product-fields-to-woocommerce-without-using-plugins/

    Its not a big job for a dev. You can post a job:
    https://jobs.wordpress.net/

    Thread Starter teuptworkshop

    (@teuptworkshop)

    Ok, I’m going to try a bit more if I can make it work and otherwise post it as a job. Thanks for the advice! 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Seeing product custom meta field in order’ is closed to new replies.