保存 woocommerce ITEM 元数据

Save woocommerce ITEM meta data

我在购物车中通过位于 functions.php 的函数为每个项目动态创建一些元数据。

在结帐时,我想保存当前订单中每件商品的元数据。

这样,一旦订单完成,我需要在 woo Commerce 管理员和 woocommerce 电子邮件中显示这些数据。

基本上,我需要节省 $date_start,$duration,$end_date 当订单完成并在 woocomerce 管理员和电子邮件中收到此数据时。

    function get_infos_order ($date_start,$duration){


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



}

有人可以给一些建议如何做吗?

非常感谢。

使用此功能保存它们:

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);

Muhammad Muazzam 解决方案还可以,但是 woocommerce_add_order_item_meta 已弃用,您必须使用 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);

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