添加到 Woocommerce 的自定义元数据未显示在订单项元中
Custom meta data added to Woocommerce not displayed in order item meta
我有一个 WooCommerce 订单的自定义元数据,现在我想在结帐后在感谢页面上显示它,但是,数据不可用。数据已保存并在管理员中可用,我似乎无法访问它。
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
时,我的元数据不存在。
我正在使用;
woocommerce_add_order_item_meta()
保存数据但转储 var_dump(wc_get_order( $order->id ));
也没有显示我的自定义元字段
我应该使用另一个挂钩来访问此数据吗?
您要查找的数据不是订单元数据,而是订单item元数据,位于wp_woocommerce_order_itemmeta
数据库table(请参阅下文如何访问此数据)。
从 woocommerce 3 开始, 替换旧的 woocommerce_add_order_item_meta
挂钩。
Displayed and readable order item meta data:
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'] );
}
}
在“已收到订单”(thankyou) 页面中,您将看到如下内容:
这也会显示在后端和电子邮件通知中。
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' );
}
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 订单的自定义元数据,现在我想在结帐后在感谢页面上显示它,但是,数据不可用。数据已保存并在管理员中可用,我似乎无法访问它。
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
时,我的元数据不存在。
我正在使用;
woocommerce_add_order_item_meta()
保存数据但转储 var_dump(wc_get_order( $order->id ));
也没有显示我的自定义元字段
我应该使用另一个挂钩来访问此数据吗?
您要查找的数据不是订单元数据,而是订单item元数据,位于wp_woocommerce_order_itemmeta
数据库table(请参阅下文如何访问此数据)。
从 woocommerce 3 开始,woocommerce_add_order_item_meta
挂钩。
Displayed and readable order item meta data:
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'] );
}
}
在“已收到订单”(thankyou) 页面中,您将看到如下内容:
这也会显示在后端和电子邮件通知中。
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' );
}
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
相关: