【问题标题】:Woocommerce - Getting meta data by product IDsWoocommerce - 通过产品 ID 获取元数据
【发布时间】:2023-04-07 19:00:01
【问题描述】:

我希望通过与当前用户关联的产品 ID 返回每个订单的所有元数据。我尝试了各种返回空数组的方法。我正在寻找访问 ID、键和值元数据

有人建议我哪里出错了吗?谢谢

$table_posts = $wpdb->prefix . "posts";
$table_items = $wpdb->prefix . "woocommerce_order_items";
$table_itemmeta = $wpdb->prefix . "woocommerce_order_itemmeta";
$orders_statuses = "'wc-completed', 'wc-processing', 'wc-on-hold'";

$orders_ids = $wpdb->get_col("
SELECT $table_items.order_id
FROM $table_itemmeta, $table_items, $table_posts
WHERE $table_items.order_item_id = $table_itemmeta.order_item_id
AND $table_items.order_id = $table_posts.ID
AND $table_posts.post_status IN ( $orders_statuses )
AND $table_itemmeta.meta_key LIKE '_product_id'
AND $table_itemmeta.meta_value LIKE '$product_id'
ORDER BY $table_items.order_item_id DESC"
);

上面的查询返回正确的订单ID。

Array
(
    [0] => 881
    [1] => 708
)

我的所有订单循环

foreach ($orders_ids as $key => $value) {

    $orders = wc_get_order($value);

    foreach ($orders->get_items() as $item ){

        $item_meta = $item->get_meta();
        $item_meta_data = $item->get_meta_data();
        $item_formatted_data = $item->get_formatted_meta_data();
        $item_formatted_data_true = $item->get_formatted_meta_data( '_', true );

        print_r($item_meta); // returns empty array
        print_r($item_meta_data); // returns example below
        print_r($item_formatted_data); // returns empty array
        print_r($item_formatted_data_true); // returns empty array

    }
}

这是我使用上面这段代码的输出

Array
(
    [0] => WC_Meta_Data Object
        (
            [current_data:protected] => Array
                (
                    [id] => 646
                    [key] => yith_booking_data
                    [value] => Array
                        (
                            [from] => 1603152000
                            [to] => 1603411200
                            [duration] => 3
                            [person_types] => Array
                                (
                                )

                            [booking_services] => Array
                                (
                                )

                            [booking_service_quantities] => Array
                                (
                                )

                            [_added-to-cart-timestamp] => 1602161920
                        )

                )

            [data:protected] => Array
                (
                    [id] => 646
                    [key] => yith_booking_data
                    [value] => Array
                        (
                            [from] => 1603152000
                            [to] => 1603411200
                            [duration] => 3
                            [person_types] => Array
                                (
                                )

                            [booking_services] => Array
                                (
                                )

                            [booking_service_quantities] => Array
                                (
                                )

                            [_added-to-cart-timestamp] => 1602161920
                        )

                )

        )

    [1] => WC_Meta_Data Object
        (
            [current_data:protected] => Array
                (
                    [id] => 647
                    [key] => _booking_id
                    [value] => 882
                )

            [data:protected] => Array
                (
                    [id] => 647
                    [key] => _booking_id
                    [value] => 882
                )

        )

)
Array
(
    [0] => WC_Meta_Data Object
        (
            [current_data:protected] => Array
                (
                    [id] => 451
                    [key] => yith_booking_data
                    [value] => Array
                        (
                            [from] => 1600905600
                            [to] => 1600992000
                            [duration] => 1
                            [person_types] => Array
                                (
                                )

                            [booking_services] => Array
                                (
                                )

                            [booking_service_quantities] => Array
                                (
                                )

                            [_added-to-cart-timestamp] => 1600955166
                        )

                )

            [data:protected] => Array
                (
                    [id] => 451
                    [key] => yith_booking_data
                    [value] => Array
                        (
                            [from] => 1600905600
                            [to] => 1600992000
                            [duration] => 1
                            [person_types] => Array
                                (
                                )

                            [booking_services] => Array
                                (
                                )

                            [booking_service_quantities] => Array
                                (
                                )

                            [_added-to-cart-timestamp] => 1600955166
                        )

                )

        )

    [1] => WC_Meta_Data Object
        (
            [current_data:protected] => Array
                (
                    [id] => 452
                    [key] => _booking_id
                    [value] => 709
                )

            [data:protected] => Array
                (
                    [id] => 452
                    [key] => _booking_id
                    [value] => 709
                )

        )

)

【问题讨论】:

    标签: woocommerce metadata orders


    【解决方案1】:

    找到答案。在循环中使用此代码

    foreach ($orders_ids as $key => $value) {
    
        $orders = wc_get_order($value);
    
        foreach ($orders->get_items() as $item ){
    
            $item_meta_data = $item->get_meta_data();
            $item_key_meta = $item_meta_data[0]->value; <- add code
            print_r($item_key_meta);
    
        }
    }
    

    返回

    Array
    (
        [from] => 1603152000
        [to] => 1603411200
        [duration] => 3
        [person_types] => Array
            (
            )
    
        [booking_services] => Array
            (
            )
    
        [booking_service_quantities] => Array
            (
            )
    
        [_added-to-cart-timestamp] => 1602161920
    )
    Array
    (
        [from] => 1600905600
        [to] => 1600992000
        [duration] => 1
        [person_types] => Array
            (
            )
    
        [booking_services] => Array
            (
            )
    
        [booking_service_quantities] => Array
            (
            )
    
        [_added-to-cart-timestamp] => 1600955166
    )
    

    【讨论】:

      猜你喜欢
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      • 2014-07-04
      • 2016-01-09
      相关资源
      最近更新 更多