WC_Order不会通过get_items()

时间:2017-08-18 06:09:39

标签: wordpress woocommerce

我试图通过以下代码获取产品ID和产品名称:

if ( $query->have_posts() ) {
    $order_id = $query->posts[0]->ID;
    $order = new WC_Order( $order_id );
    $items = $order->get_items();
}

    foreach ( $items as $item ) {
        $product_id = $item['product_id'];
        $product = wc_get_product( $item_id );
        $product_name = $item['name'];
    }

在上面的代码中,我得到了产品名称,但是对于$ product_id,它返回0。还有其他方法吗?

我无法找到解决方法。

我的编辑版本:

当我尝试这个时:

$order_id = $query->posts[0]->ID;
$order = new WC_Order( $order_id );
$items = $order->get_items();

foreach ( $items as $item ) {
    $item_id = $item['product_id'];
    $product_name = $item['name'];
    print_r( $item ); exit;
}

我得到了这个数组:

WC_Order_Item_Product Object
(
    [extra_data:protected] => Array
        (
            [product_id] => 0
            [variation_id] => 0
            [quantity] => 1
            [tax_class] => 
            [subtotal] => 0
            [subtotal_tax] => 0
            [total] => 0
            [total_tax] => 0
            [taxes] => Array
                (
                    [subtotal] => Array
                        (
                        )

                    [total] => Array
                        (
                        )

                )

        )

    [data:protected] => Array
        (
            [order_id] => 684
            [name] => Dark Skirt "Erebos" - Large, White
            [product_id] => 0
            [variation_id] => 0
            [quantity] => 1
            [tax_class] => 
            [subtotal] => 19
            [subtotal_tax] => 0
            [total] => 19
            [total_tax] => 0
            [taxes] => Array
                (
                    [total] => Array
                        (
                        )

                    [subtotal] => Array
                        (
                        )

                )

        )

    [cache_group:protected] => order-items
    [meta_type:protected] => order_item
    [object_type:protected] => order_item
    [id:protected] => 1
    [changes:protected] => Array
        (
        )

    [object_read:protected] => 1
    [default_data:protected] => Array
        (
            [order_id] => 0
            [name] => 
            [product_id] => 0
            [variation_id] => 0
            [quantity] => 1
            [tax_class] => 
            [subtotal] => 0
            [subtotal_tax] => 0
            [total] => 0
            [total_tax] => 0
            [taxes] => Array
                (
                    [subtotal] => Array
                        (
                        )

                    [total] => Array
                        (
                        )

                )

        )

    [data_store:protected] => WC_Data_Store Object
        (
            [instance:WC_Data_Store:private] => WC_Order_Item_Product_Data_Store Object
                (
                    [internal_meta_keys:protected] => Array
                        (
                            [0] => _order_id
                            [1] => _name
                            [2] => _product_id
                            [3] => _variation_id
                            [4] => _quantity
                            [5] => _tax_class
                            [6] => _subtotal
                            [7] => _subtotal_tax
                            [8] => _total
                            [9] => _total_tax
                            [10] => _taxes
                            [11] => _product_id
                            [12] => _variation_id
                            [13] => _qty
                            [14] => _tax_class
                            [15] => _line_subtotal
                            [16] => _line_subtotal_tax
                            [17] => _line_total
                            [18] => _line_tax
                            [19] => _line_tax_data
                        )

                    [meta_type:protected] => order_item
                    [object_id_field_for_meta:protected] => order_item_id
                )

            [stores:WC_Data_Store:private] => Array
                (
                    [coupon] => WC_Coupon_Data_Store_CPT
                    [customer] => WC_Customer_Data_Store
                    [customer-download] => WC_Customer_Download_Data_Store
                    [customer-session] => WC_Customer_Data_Store_Session
                    [order] => WC_Order_Data_Store_CPT
                    [order-refund] => WC_Order_Refund_Data_Store_CPT
                    [order-item] => WC_Order_Item_Data_Store
                    [order-item-coupon] => WC_Order_Item_Coupon_Data_Store
                    [order-item-fee] => WC_Order_Item_Fee_Data_Store
                    [order-item-product] => WC_Order_Item_Product_Data_Store
                    [order-item-shipping] => WC_Order_Item_Shipping_Data_Store
                    [order-item-tax] => WC_Order_Item_Tax_Data_Store
                    [payment-token] => WC_Payment_Token_Data_Store
                    [product] => WC_Product_Data_Store_CPT
                    [product-grouped] => WC_Product_Grouped_Data_Store_CPT
                    [product-variable] => WC_Product_Variable_Data_Store_CPT
                    [product-variation] => WC_Product_Variation_Data_Store_CPT
                    [shipping-zone] => WC_Shipping_Zone_Data_Store
                )

            [current_class_name:WC_Data_Store:private] => WC_Order_Item_Product_Data_Store
            [object_type:WC_Data_Store:private] => order-item-product
        )

    [meta_data:protected] => Array
        (
        )

)

你看到[data:protected]数组,你可以看到[product_id]为0的地方。这就是我猜的问题。但是这个解决方案是什么?

1 个答案:

答案 0 :(得分:0)

它应该是:

foreach ( $items as $item ) {
    $product = $item->get_product();
    $product_id = $product->get_id();
}