WooCommerce 订单仅显示一项元数据而不显示另一项元数据
WooCommerce order only shows one item meta but not the other one
对于每个订单项目,我想显示两个元字段。一个显示,一个不显示。
显示的是一个在产品和人员中显示的字段 select 一个选项,并且添加得很好。未显示的是在创建订单时计算的必填字段。我正在这样添加元数据:
function woo_save_course_to_order_items($item, $cart_item_key, $values, $order)
{
if (empty($values['course_to_enroll'])) {
return;
}
// This one works
$item->add_meta_data(__('Course', 'my-plugin'), intval($values['course_to_enroll']));
$students_array = array();
// Here goes some programming to grab all the student's ids and put them in $students_array.
//This part works.
$item->add_meta_data(__('Students', 'my-plugin'), $students_array);
// The value is saved, but it is not shown in the order
}
add_action('woocommerce_checkout_create_order_line_item', 'woo_save_course_to_order_items', 10, 4);
我强制它按照这样的顺序显示:
function customizing_order_item_display($value, $meta, $item)
{
error_log(print_r($meta->key, true));// Only outputs 'Course', never 'Students'
if ($item->get_type() === 'line_item' && $meta->key === 'Course') {
$value = get_the_title(intval($value));
//Without the following, 'Students' would never appear in the order details (front and backend).
$students = $item->get_meta('Students', true); //It is being saved fine
if (is_array($students)) {
$links = array_map(function ($s) {
return '<a href="' . get_the_permalink(intval($s)) . '">' . get_the_title(intval($s)) . '</a>';
}, $students);
$value .= "<br><strong>Students</strong>: " . implode(", ", $links);
}
}
return $value;
}
add_filter('woocommerce_order_item_display_meta_value', 'customizing_order_item_display', 10, 3);
为什么我必须强制它显示我的第二个元值?不应该只是显示它吗?
为什么只显示第一个字段,而不显示第二个。它正在被保存。这是项目元的日志:
[meta_data:protected] => Array
(
[0] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 121
[key] => Course
[value] => 60
)
[data:protected] => Array
(
[id] => 121
[key] => Course
[value] => 60
)
)
[1] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 122
[key] => Students
[value] => Array
(
[0] => 102
)
)
[data:protected] => Array
(
[id] => 122
[key] => Students
[value] => Array
(
[0] => 102
)
)
)
)
将数组存储为 JSON
数据,然后它将显示没有代码片段的数据。
$students_array = array(12,14);
// Here goes some programming to grab all the student's ids and put them in $students_array.
$item->add_meta_data(__('Students', 'my-plugin'), json_encode( $students_array ));
以下是跳过显示的元数据验证。
if ( '' === $meta->value || is_serialized( $meta->value ) || ( ! empty( $hideprefix ) && substr( $meta->key, 0, 1 ) === $hideprefix ) ) {
continue;
}
将跳过 empty
或 serialized
或 hidden meta
(元以下划线开头)。
对于每个订单项目,我想显示两个元字段。一个显示,一个不显示。
显示的是一个在产品和人员中显示的字段 select 一个选项,并且添加得很好。未显示的是在创建订单时计算的必填字段。我正在这样添加元数据:
function woo_save_course_to_order_items($item, $cart_item_key, $values, $order)
{
if (empty($values['course_to_enroll'])) {
return;
}
// This one works
$item->add_meta_data(__('Course', 'my-plugin'), intval($values['course_to_enroll']));
$students_array = array();
// Here goes some programming to grab all the student's ids and put them in $students_array.
//This part works.
$item->add_meta_data(__('Students', 'my-plugin'), $students_array);
// The value is saved, but it is not shown in the order
}
add_action('woocommerce_checkout_create_order_line_item', 'woo_save_course_to_order_items', 10, 4);
我强制它按照这样的顺序显示:
function customizing_order_item_display($value, $meta, $item)
{
error_log(print_r($meta->key, true));// Only outputs 'Course', never 'Students'
if ($item->get_type() === 'line_item' && $meta->key === 'Course') {
$value = get_the_title(intval($value));
//Without the following, 'Students' would never appear in the order details (front and backend).
$students = $item->get_meta('Students', true); //It is being saved fine
if (is_array($students)) {
$links = array_map(function ($s) {
return '<a href="' . get_the_permalink(intval($s)) . '">' . get_the_title(intval($s)) . '</a>';
}, $students);
$value .= "<br><strong>Students</strong>: " . implode(", ", $links);
}
}
return $value;
}
add_filter('woocommerce_order_item_display_meta_value', 'customizing_order_item_display', 10, 3);
为什么我必须强制它显示我的第二个元值?不应该只是显示它吗? 为什么只显示第一个字段,而不显示第二个。它正在被保存。这是项目元的日志:
[meta_data:protected] => Array
(
[0] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 121
[key] => Course
[value] => 60
)
[data:protected] => Array
(
[id] => 121
[key] => Course
[value] => 60
)
)
[1] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 122
[key] => Students
[value] => Array
(
[0] => 102
)
)
[data:protected] => Array
(
[id] => 122
[key] => Students
[value] => Array
(
[0] => 102
)
)
)
)
将数组存储为 JSON
数据,然后它将显示没有代码片段的数据。
$students_array = array(12,14);
// Here goes some programming to grab all the student's ids and put them in $students_array.
$item->add_meta_data(__('Students', 'my-plugin'), json_encode( $students_array ));
以下是跳过显示的元数据验证。
if ( '' === $meta->value || is_serialized( $meta->value ) || ( ! empty( $hideprefix ) && substr( $meta->key, 0, 1 ) === $hideprefix ) ) {
continue;
}
将跳过 empty
或 serialized
或 hidden meta
(元以下划线开头)。