Add arbitrary product data to order items in WooCommerce

This seems to be way more convoluted than it needs to be, but I’m not sure how much of that is that it’s actually convoluted, how much is that Woo’s documentation sucks, and how much is that everyone else’s tutorial on it is tl;dr.

Anyway… I just wanted to do something fairly simple. I want to have each product’s short description get sent into the order data. This is a specific use case with a client who’s syncing data over the REST API with an external system, and we’re shoehorning data into the short description that maybe could go somewhere else. The point is, use your imagination as to how this might be useful to you.

I’m stripping out a lot of the other details. All I want is a way to a) add the data to the item in the cart, and b) carry that data over into the order item meta data in the database. You may need or want more, but this will get you started.

// Add custom order item meta data to cart
add_filter('woocommerce_add_cart_item_data', function($cart_item, $product_id) {
  if (!isset($cart_item['short_description'])) {
    if ($product = wc_get_product($product_id)) {
      $cart_item['short_description'] = $product->get_short_description();
    }
  }
  return $cart_item;
}, 10, 2);

// Add custom order item data from the cart into the order
add_action('woocommerce_checkout_create_order_line_item', function($item, $cart_item_key, $values, $order) {
  if  (isset($values['short_description'])) {
    $item->add_meta_data('Short Description', $values['short_description'], true);
  }
}, 10, 4);

This is a major distillation of stuff I found in these two tutorials: How to Add a Customizable Field to a WooCommerce Product and Add Custom Cart Item Data in WooCommerce.

Refresh the Page. Twice.

Although the tan-on-brown color scheme was… er… unique, and crisp to read on an LCD screen (which is all I own), I got tired of how smudgy and illegible it appeared on the ultra-X-treem high resolution CRT I use at work. (Uh… I mean… not that I… uh…)

So it was that I changed the color scheme of the site. But most browsers (at least Firefox, which is what I use) keep the CSS and image files cached more persistently than page content, so on first glance the pages here come off with the same circa 1978 color palette as before, just with a white page background instead of black.

If that’s what you’re seeing, for the love of Jehosephat* refresh the page now!

OK. Now I know what you’re seeing (at least if you’re using Firefox). This time around everything’s blue… except a little less than halfway across, the header graphic changes from blue to the old orange scheme. Ack! Refresh again!

Ah… that’s the stuff.

If it still looks like crap, maybe it’s your browser. You might need to actually clear the cache or something. Just don’t blame me. I already told you to use Firefox.

* Side note re: Jehosephat. What little I know about biblical King Jehosephat comes from hearing the phrase “jumpin’ Jehosephat” in passing, and the few minutes of Googling I did right after I posted this. (After all, I figured if I was enticing my reader [sic] with the love of Jehosephat as a reward for doing as I command, then perhaps I should know whether or not said love is desirable.)

From what I gather, King J. was a respectable fellow. For more on the matter, I direct you to the very Google search I myself undertook. But beware… as is often the case in life, that road is fraught with peril… and completely impertinent links. Seems the king himself is not exactly a hot topic around the virtual water cooler. Oh well… off you go!