Is it possible to add a metadata on a product which is inside the Order?
In this case, there would be one metadata but each product (in the order) would have different value. Example:
Order 1:
* Product 1
Sample Meta: Meta1
* Product 2
Sample Meta: Meta2
Thanks.
Update1 :
I'm now stuck on how would I grab the values from the woocommerce_add_cart_item_data
filter. I was able to successfully add the meta data from there.
I need to grab those values for me to use in this woocommerce_add_order_item_meta
action hook.
This is how I successfully added the metadata to the filter:
function add_cart_item_data( $cart_item_data, $product_id ) {
$cart_item_data[ "meta1" ] = $_POST["meta1"];
$cart_item_data[ "meta2" ] = $_POST["meta2"];
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 99, 2 );
After creating attribute "The Meta" and adding it to the product.
I answered my problem with the following:
First: Add the meta via
woocommerce_add_cart_item_data
filter hook. This would add first the meta to the items inside the cart.Second: Save the meta added to the cart to the actual items purchased within the order
Yes this is possible using a custom function hooked in
woocommerce_add_order_item_meta
action hook.Update related to your answer: (See in the linked answer with a working real example):
Adding user custom field value to order items details
This product attribute creation name will be (regarding your answer code):
'The Meta'
and the slug'the_meta'
.Then you will have to set it in each related product with a mandatory value (just any value, as this value is going to be replaced by your custom value below).
So once done, your code will be:
Then you will get this kind of display in your Orders items ('XXXX' is your custom value here):