php WooCommerce - get_order() 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28847093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:05:38  来源:igfitidea点击:

WooCommerce - get_order() is not working

phpwordpresswoocommerceglobal-variableshelpers

提问by nickyb

I'm trying to create a function that will retrieve an order by its ID. For some reason I can't get the WooCommerce global function get_orderto work. I'm passing a valid order idto the function and trying to print it out to verify that it's working. The function has been placed in my functions.phpfile.

我正在尝试创建一个函数,该函数将通过其 ID 检索订单。出于某种原因,我无法让 WooCommerce 全局功能get_order正常工作。我正在将有效订单传递id给该函数并尝试将其打印出来以验证它是否正常工作。该功能已放在我的functions.php文件中。

function getWC_order_details($id){
    global $woocommerce;

    $order = get_order( $id );

    print "<pre>";
    print_r($order);
    print "</pre>";
}

I have tested echoing other data out of the function without a problem.

我已经测试过从函数中回显其他数据没有问题。

回答by Rohil_PHPBeginner

First of all make function like this :

首先 make 函数是这样的:

function getWC_order_details($order_id) { 
    $order = new WC_Order( $order_id );
    var_dump($order);
}

After that, use it with some woo_commerceactionor filter.

之后,通过一些woo_commerce操作过滤器使用它。

function use_after_cart_table(){
    getWC_order_details(40);
}
add_action( 'woocommerce_after_cart_table', 'use_after_cart_table' );

So after adding any product to the cart, you will see after cart table that there is one arraycontaining all the details.

因此,在将任何产品添加到购物车后,您将在购物车表之后看到一个包含所有详细信息的数组

NOTE: You can use any other action or filter and you can find them here.

注意:您可以使用任何其他操作或过滤器,您可以在此处找到它们。

EDITED:

编辑:

function getWC_order_details($order_id) { 
    $order = new WC_Order( $order_id );
    //var_dump($order);
    $order_shipping_total = $order->get_shipping();
    $order_shipping_method = $order->get_shipping_methods();
    var_dump($order_shipping_total);//Use it for debugging purpose or to see details in that array
    var_dump($order_shipping_method);//Use it for debugging purpose or to see details in that array

    $_order =   $order->get_items(); //to get info about product
    foreach($_order as $order_product_detail){
        //var_dump($order_product_detail);
        echo "<b>Product ID:</b> ".$order_product_detail['product_id']."<br>";
        echo "<b>Product Name:</b> ".$order_product_detail['name']."<br><br>";
    }
    //var_dump($_order);
}

回答by Shravan Sharma

try this.It might be useful to you.

试试这个。它可能对你有用。

function getWC_order_details($id)
{
 $array = WC_API_Orders::get_order( $id, $fields );
 print "<pre>";
 print_r($order);
 print "</pre>";
}

Source: File name: woocommerce/includes/api/class-wc-api-orders.php

来源:文件名:woocommerce/includes/api/class-wc-api-orders.php