在挂钩函数中获取保存在订单项元中的自定义字段值

Get a custom field value saved in Order items meta in a hooked function

我能够在购物车和结帐页面上添加、验证和显示产品上的自定义字段 Page.Please 谁能告诉我如何使用 [= 检索自定义字段值20=]勾?

我想在确认电子邮件发送给用户后,再发送一封包含自定义字段数据的电子邮件

static function sendCustomData($order_id) {
    $order = wc_get_order($order_id);
    $items = $order->get_items();

    foreach ($items as $item) {
        $product_id = $item['product_id'];

        $Id = get_post_meta($product_id, '_wpws_ID', true);
        $first_name = $order->get_billing_first_name();
        $billing_email = $order->get_billing_email();


        if (empty($Id))
        continue;

        $mail = new CustomMails();
        $mail->SendMailtoReaderOnWCOrderComplete($first_name, $billing_email, $Id);
    }
}
add_action('woocommerce_order_status_completed','sendCustomData');

Saving custom order meta value

public static function tshirt_order_meta_handler( $item_id, $values, $cart_item_key ) {
    if( isset( $values['name_on_tshirt'] ) ) {
        wc_add_order_item_meta( $item_id, "name_on_tshirt", $values['name_on_tshirt'] );
    }
}
add_action( 'woocommerce_new_order_item', 'tshirt_order_meta_handler', 1, 3 );

if (empty($Id)) .. 更改为 if (empty($wbnId))

要获取 "name_on_tshirt" 自定义字段,您需要获取订单商品 ID,并且需要这样使用 wc_get_order_item_meta() 函数:

foreach ($order->get_items() as $item_id => $item) {
    ## HERE ==> Get your custom field value
    $name_on_tshirt wc_get_order_item_meta( $item_id, "name_on_tshirt", true );
}