Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add extra meta for orders in Woocommerce

I'm creating a custom plugin for my website.

In some part of this plugin I need to store extra meta in wp_postmeta for each orders.

I added this in my plugin's class:

add_action ('woocommerce_before_checkout_process', array( &$this, 'add_item_meta', 10, 2) ); 

And this is add_item_meta() function:

function add_item_meta( $item_id, $values ) {   wc_add_order_item_meta($item_id, '_has_event', 'yes' ); } 

This function is not complete, but nothing happens with this codes; I think I need to use another hook but I can't find a proper one.

Does anyone know anything about this?

I also have another problem with $item_id: this is woocommerce global variable but I can't see it in my plugin!

I mean I don't have access to this variable from my plugin or something like this!

like image 470
Mo Saeedi Avatar asked Sep 02 '14 14:09

Mo Saeedi


People also ask

How do I add a custom field to a WooCommerce order?

First, to create a field, go to WooCommerce > Custom Order Fields. Click “Add Field” and begin creating your order field. The “label” is the field name, and will be displayed in the order details. The “description” will be displayed to the user upon hovering over the “?” symbol.

What is meta key WooCommerce?

The metakey is used to retrieve the saved value from the database and display it. If you are a developer, chances are you already know about this WordPress function. https://codex.wordpress.org/Function_Reference/get_user_meta.

Can you edit orders in WooCommerce?

The Edit Order by Customer for WooCommerce extension allows customers to edit placed orders with certain order statuses.


2 Answers

The 2018 way:

Built on Guido W.P. answer you can use instead woocommerce_checkout_create_order action hook in a more lighter and effective version code (using WC 3+ CRUD methods):

add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2); function before_checkout_create_order( $order, $data ) {     $order->update_meta_data( '_custom_meta_key', 'value' ); } 

Code goes in function.php file of your active child theme (or active theme).

Tested and works in WooCommerce 3+ (only).


SOME EXPLANATIONS:

The woocommerce_checkout_create_order action hook is just one step before saving the order data. See below in an extract of the WC_Checkout create_order() method (with both hooks):

/**  * Action hook to adjust order before save.  * @since 3.0.0  */ do_action( 'woocommerce_checkout_create_order', $order, $data );  // Save the order. $order_id = $order->save();  do_action( 'woocommerce_checkout_update_order_meta', $order_id, $data );  return $order_id; 

Why using woocommerce_checkout_create_order instead?:

  • Because You don't need to use $order = wc_get_order( $order_id ); as you already got $order as an argument in the hooked function.
  • You don't need to use $order->save(); as this will be done just after anyway (see the source code)
  • This hook has been released since WooCommerce version 3 and it's made for the same purpose, allowing to use all available WC_Order methods.

So this just works with a single line of code inside the function.

like image 89
LoicTheAztec Avatar answered Oct 17 '22 02:10

LoicTheAztec


Building on Mo Saeedi answer, I believe this snippet is more in line with the new CRUD approach introduced by WooCommerce 3.0:

add_action('woocommerce_checkout_update_order_meta',function( $order_id, $posted ) {     $order = wc_get_order( $order_id );     $order->update_meta_data( 'my_custom_meta_key', 'my data' );     $order->save(); } , 10, 2); 

See also this threads on the WordPress forums:

  • Oh CRUD! Custom Meta to order from cart
  • What is the correct way to update meta fields in Woocommerce?
like image 43
Guido Walter Pettinari Avatar answered Oct 17 '22 04:10

Guido Walter Pettinari