本文介绍了获取挂钩函数中保存在“订单项"元数据中的自定义字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够在商品页面上添加,验证,显示在购物车和结帐页面上的自定义字段.请有人告诉我如何使用 woocommerce_order_status_completed 钩子检索自定义字段的值?

I am able to add, validate, display on cart and checkout page a custom field on the Product Page.Please can someone tell me how can I retrieve the custom field values using woocommerce_order_status_completed hook?

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

I want to send an additional email including the custom field data after the confirmation email is sent to the user

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

推荐答案

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

To get "name_on_tshirt" custom field, you need to get the order Item ID and you need to use wc_get_order_item_meta() function this way:

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

这篇关于获取挂钩函数中保存在“订单项"元数据中的自定义字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 01:43