添加到Woocommerce的自定义元数据未在订单项元中显示 [英] Custom meta data added to Woocommerce not displayed in order item meta

查看:63
本文介绍了添加到Woocommerce的自定义元数据未在订单项元中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对WooCommerce订单只有一个自定义元数据,现在我想在结帐后将其显示在谢谢"页面上,但是该数据不可用.数据已保存并可以在管理员中使用,我似乎无法访问它.

I've a single piece of custom metadata to a WooCommerce order and now I want to display this on the thank you page after checkout, however, the data isn't available. The data is saved and available in the admin, I just can't seem to access it.

function custom_order_item_meta( $item_id, $values ) {

    if ( ! empty( $values['custom_option'] ) ) {
        woocommerce_add_order_item_meta( $item_id, 'custom_option', $values['custom_option'] );           
    }
}
add_action( 'woocommerce_add_order_item_meta', 'custom_order_item_meta', 10, 2 );

但是当我转储wc_get_order时,我的元数据不存在.

But when I dump out the wc_get_order my meta data isn't there.

我正在使用;

woocommerce_add_order_item_meta() 保存数据但转储var_dump(wc_get_order( $order->id ));也不显示我的自定义元字段

woocommerce_add_order_item_meta() to save the data but dumping out var_dump(wc_get_order( $order->id )); also doesn't show my custom meta field

我应该使用另一个挂钩来访问此数据吗?

is there another hook I should be using to access this data?

推荐答案

您要查找的数据不是订单元数据,而是订单 item 元数据,并且位于wp_woocommerce_order_itemmeta数据库中表(请参阅下面的如何访问此数据).

The data that you are looking for is not order meta data, but order item meta data and is located in wp_woocommerce_order_itemmeta database table (see below how to access this data).

自woocommerce 3起,更好的挂钩替换旧的woocommerce_add_order_item_meta挂钩.

Since woocommerce 3, a much better hook replace old woocommerce_add_order_item_meta hook.

显示且可读的订单商品元数据:

要使自定义订单商品的元数据显示在各处,则元键应为可读标签名称,且不能以下划线开头,因为该数据将为显示在每个订单项下.

To make custom order item meta data displayed everywhere, the meta key should be a readable label name and without starting by an underscore, as this data will be displayed under each order item.

代码:

add_action( 'woocommerce_checkout_create_order_line_item', 'custom_order_item_meta', 20, 4 );
function custom_order_item_meta( $item, $cart_item_key, $values, $order ) {
    if ( isset( $values['custom_option'] ) ) {
        $item->update_meta_data( __('Custom option', 'woocommerce'), $values['custom_option'] );          
    }
}

在已收到订单"中(谢谢)页面,您将得到类似的信息:

In "Order received" (thankyou) page, you will get something like:

这也会在后端通知和电子邮件通知中显示.

This will be displayed too in backend and email notifications.

要访问此订单商品数据,您需要在foreach循环中从订单对象获取商品:

To access this order item data you need to get items from the order object in a foreach loop:

foreach( $order->get_items() as $item_id => $item ){

    $custom_data = $item->get_meta( 'Custom option' );
}

要获取第一笔订单项(避免foreach循环),您将使用:

To Get the first order item (avoiding a foreach loop), you will use:

$items       = $order->get_items(); // Order items

$item        = reset($items); // The first Order item
$custom_data = $item->get_meta( 'Custom option' ); // Your custom meta data


相关:替换Woocommerce 3.4中的woocommerce_add_order_item_meta钩子

这篇关于添加到Woocommerce的自定义元数据未在订单项元中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆