php WooCommerce订单只显示一个项目Meta,而不是另一个

wi3ka0sx  于 12个月前  发布在  PHP
关注(0)|答案(1)|浏览(96)

对于每个订单项目,我想显示两个Meta字段。一个是显示,但另一个不是。

显示的是产品中显示的一个字段,人们选择一个选项,然后添加一个很好的选项。未显示的字段是在订单创建时计算的必填字段。我添加Meta如下:

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值?它不应该只是显示它吗?为什么只显示第一个字段,而不显示第二个字段。它正在被拯救。这是项目Meta的日志:

[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
                                )

                        )

                )

        )
8gsdolmq

8gsdolmq1#

将数组存储为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;
}

将跳过emptyserializedhidden meta(以下划线开头的Meta数据)。

相关问题