3
votes

I the cart i dynamically create some meta data for each item from a function located in functions.php.

IN CHECKOUT, I would like to save each meta data for each item of the current order.

In this way, once order completed, i would need to display these data in woo commerce admin and woocommerce email.

Basically, i need to save $date_start,$duration,$end_date when the order is completed and receive this data in woocomerce admin and emails.

    function get_infos_order ($date_start,$duration){


$end_date          = strtotime('+ '.$duration, $date_start); 



}

Could someone please give some advices how to do that please ?

Thanks a lot.

2
Please could you provide, editing your question, all the code you are using for this at the moment. ThanksLoicTheAztec
it's just variables calculated in factions.php that i would like to save with my order items...user6677795
Yes but how you get them in cart… update your question with the related code you got, this way it will be easier to help you. In StackOverFlow, we help people that have already work on some code that they submit in their question, with more details as possible…LoicTheAztec
ok thanks updated...user6677795

2 Answers

2
votes

Muhammad Muazzam solution is OK, but woocommerce_add_order_item_meta is deprecated, you have to use wc_add_order_item_meta

function add_order_item_meta($item_id, $values) {
    $key = ''; // Define your key here
    $value = filter_input(INPUT_POST, 'key_name'); // Safer retrieval
    wc_add_order_item_meta( $item_id, $meta_key, $meta_value);
}
add_action('woocommerce_add_order_item_meta', 'add_order_item_meta', 10, 2);

Source: https://docs.woocommerce.com/wc-apidocs/source-function-woocommerce_add_order_item_meta.html#428-433

0
votes

Save them using this function:

function add_order_item_meta($item_id, $values) {
    $key = ''; // Define your key here
    $value = $_POST['key_name']; // Get your value here
    woocommerce_add_order_item_meta($item_id, $key, $value);
}
add_action('woocommerce_add_order_item_meta', 'add_order_item_meta', 10, 2);