添加自定义订单商品元数据

问题描述 投票:2回答:2

我目前正在处理一些将与woocommerce_order_status_completed钩子一起运行的代码。代码将连接到第三方以检索所购商品的序列号。最初我打算只是通过电子邮件将序列号发送给客户,但我想知道,是否可以将检索到的序列号添加到订购商品(元?),以便当客户进入他们的帐户页面查看订单时,他们看到那里列出的序列号(在购买的产品下?

这有可能是怎样的?将信息添加到要在帐户页面中显示的订单?

woocommerce
2个回答
6
投票

当然,像这样添加:

function add_order_item_meta($item_id, $values) {
    $key = ''; // Define your key here
    $value = ''; // 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);

1
投票

如果您使用的是Class:

add_action( 'woocommerce_add_order_item_meta', array( $this,'add_order_item_meta'), 10, 2 );

public static function add_order_item_meta($item_id, $values) {
    $key = 'statusitem'; // Define your key here
    $value = 'AAA'; // Get your value here

    wc_update_order_item_meta($item_id, $key, $value);
}

如果不使用类:

add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta', 10, 2 );

function add_order_item_meta($item_id, $values) {
    $key = 'statusitem'; // Define your key here
    $value = 'AAA'; // Get your value here

    wc_update_order_item_meta($item_id, $key, $value);
}

来自API的Json响应调用,请看meta_data:

"line_items": [{
    "id": 425,
    "name": "FANTA",
    "product_id": 80,
    "variation_id": 0,
    "quantity": 1,
    "tax_class": "",
    "subtotal": "30000.00",
    "subtotal_tax": "0.00",
    "total": "30000.00",
    "total_tax": "0.00",
    "taxes": [],
    "meta_data": [{
        "id": 3079,
        "key": "statusitem",
        "value": "AAA"
    }],
    "sku": "000000000000009",
    "price": 30000
}],
© www.soinside.com 2019 - 2024. All rights reserved.