当前位置: 首页 > 知识库问答 >
问题:

如何获取商业订单详细信息

谢运良
2023-03-14

从以下行代码输入:

$order = new WC_Order( $order_id );

如何从订单id获取WooCommerce订单详细信息?

共有3个答案

呼延鹏云
2023-03-14

解释了访问直接属性和相关属性

// Get an instance of the WC_Order object
            $order = wc_get_order($order_id);
            $order_data = array(
                    'order_id' => $order->get_id(),
                    'order_number' => $order->get_order_number(),
                    'order_date' => date('Y-m-d H:i:s', strtotime(get_post($order->get_id())->post_date)),
                    'status' => $order->get_status(),
                    'shipping_total' => $order->get_total_shipping(),
                    'shipping_tax_total' => wc_format_decimal($order->get_shipping_tax(), 2),
                    'fee_total' => wc_format_decimal($fee_total, 2),
                    'fee_tax_total' => wc_format_decimal($fee_tax_total, 2),
                    'tax_total' => wc_format_decimal($order->get_total_tax(), 2),
                    'cart_discount' => (defined('WC_VERSION') && (WC_VERSION >= 2.3)) ? wc_format_decimal($order->get_total_discount(), 2) : wc_format_decimal($order->get_cart_discount(), 2),
                    'order_discount' => (defined('WC_VERSION') && (WC_VERSION >= 2.3)) ? wc_format_decimal($order->get_total_discount(), 2) : wc_format_decimal($order->get_order_discount(), 2),
                    'discount_total' => wc_format_decimal($order->get_total_discount(), 2),
                    'order_total' => wc_format_decimal($order->get_total(), 2),
                    'order_currency' => $order->get_currency(),
                    'payment_method' => $order->get_payment_method(),
                    'shipping_method' => $order->get_shipping_method(),
                    'customer_id' => $order->get_user_id(),
                    'customer_user' => $order->get_user_id(),
                    'customer_email' => ($a = get_userdata($order->get_user_id() )) ? $a->user_email : '',
                    'billing_first_name' => $order->get_billing_first_name(),
                    'billing_last_name' => $order->get_billing_last_name(),
                    'billing_company' => $order->get_billing_company(),
                    'billing_email' => $order->get_billing_email(),
                    'billing_phone' => $order->get_billing_phone(),
                    'billing_address_1' => $order->get_billing_address_1(),
                    'billing_address_2' => $order->get_billing_address_2(),
                    'billing_postcode' => $order->get_billing_postcode(),
                    'billing_city' => $order->get_billing_city(),
                    'billing_state' => $order->get_billing_state(),
                    'billing_country' => $order->get_billing_country(),
                    'shipping_first_name' => $order->get_shipping_first_name(),
                    'shipping_last_name' => $order->get_shipping_last_name(),
                    'shipping_company' => $order->get_shipping_company(),
                    'shipping_address_1' => $order->get_shipping_address_1(),
                    'shipping_address_2' => $order->get_shipping_address_2(),
                    'shipping_postcode' => $order->get_shipping_postcode(),
                    'shipping_city' => $order->get_shipping_city(),
                    'shipping_state' => $order->get_shipping_state(),
                    'shipping_country' => $order->get_shipping_country(),
                    'customer_note' => $order->get_customer_note(),
                    'download_permissions' => $order->is_download_permitted() ? $order->is_download_permitted() : 0,
            );

其他细节

  $line_items_shipping = $order->get_items('shipping');
            foreach ($line_items_shipping as $item_id => $item) {
                if (is_object($item)) {
                    if ($meta_data = $item->get_formatted_meta_data('')) :
                        foreach ($meta_data as $meta_id => $meta) :
                            if (in_array($meta->key, $line_items_shipping)) {
                                continue;
                            }
                            // html entity decode is not working preoperly
                            $shipping_items[] = implode('|', array('item:' . wp_kses_post($meta->display_key), 'value:' . str_replace('×', 'X', strip_tags($meta->display_value))));
                        endforeach;
                    endif;
                }
            }

            //get fee and total
            $fee_total = 0;
            $fee_tax_total = 0;

            foreach ($order->get_fees() as $fee_id => $fee) {

                $fee_items[] = implode('|', array(
                        'name:' .  html_entity_decode($fee['name'], ENT_NOQUOTES, 'UTF-8'),
                        'total:' . wc_format_decimal($fee['line_total'], 2),
                        'tax:' . wc_format_decimal($fee['line_tax'], 2),
                ));

                $fee_total += $fee['line_total'];
                $fee_tax_total += $fee['line_tax'];
            }

            // get tax items
            foreach ($order->get_tax_totals() as $tax_code => $tax) {            
                $tax_items[] = implode('|', array(
                    'rate_id:'.$tax->id,
                    'code:' . $tax_code,
                    'total:' . wc_format_decimal($tax->amount, 2),
                    'label:'.$tax->label,                
                    'tax_rate_compound:'.$tax->is_compound,
                ));
            }

            // add coupons
            foreach ($order->get_items('coupon') as $_ => $coupon_item) {

                $coupon = new WC_Coupon($coupon_item['name']);

                $coupon_post = get_post((WC()->version < '2.7.0') ? $coupon->id : $coupon->get_id());
                $discount_amount = !empty($coupon_item['discount_amount']) ? $coupon_item['discount_amount'] : 0;
                $coupon_items[] = implode('|', array(
                        'code:' . $coupon_item['name'],
                        'description:' . ( is_object($coupon_post) ? $coupon_post->post_excerpt : '' ),
                        'amount:' . wc_format_decimal($discount_amount, 2),
                ));
            }

            foreach ($order->get_refunds() as $refunded_items){
                $refund_items[] = implode('|', array(
                    'amount:' . $refunded_items->get_amount(),
            'reason:' . $refunded_items->get_reason(),
                    'date:'. date('Y-m-d H-i-s',strtotime((WC()->version < '2.7.0') ? $refunded_items->date_created : $refunded_items->get_date_created())),
                ));
            }
彭高畅
2023-03-14

仅适用于WOOCOMMERCE 2.5版。x和2.6。十、

有关WOOCOMMERCE 3.0版,请参阅此更新

这是我制作的一个自定义函数,用于让您清楚地了解与获取订单ID数据相关的内容。您将看到可以获取的所有不同原始输出以及如何获取所需数据…

使用print\u r()函数(或也使用var\u dump()函数)可以输出对象或html" target="_blank">数组的原始数据。

首先,我输出这些数据以显示对象或数组层次结构。然后根据变量的类型(字符串、数组或对象)使用不同的语法来输出所需的特定数据。

重要提示:使用$order对象,您可以使用大多数WC\u-orderWC\u-Abstract\u-order方法(使用对象语法)…

代码如下:

function get_order_details($order_id){

    // 1) Get the Order object
    $order = wc_get_order( $order_id );

    // OUTPUT
    echo '<h3>RAW OUTPUT OF THE ORDER OBJECT: </h3>';
    print_r($order);
    echo '<br><br>';
    echo '<h3>THE ORDER OBJECT (Using the object syntax notation):</h3>';
    echo '$order->order_type: ' . $order->order_type . '<br>';
    echo '$order->id: ' . $order->id . '<br>';
    echo '<h4>THE POST OBJECT:</h4>';
    echo '$order->post->ID: ' . $order->post->ID . '<br>';
    echo '$order->post->post_author: ' . $order->post->post_author . '<br>';
    echo '$order->post->post_date: ' . $order->post->post_date . '<br>';
    echo '$order->post->post_date_gmt: ' . $order->post->post_date_gmt . '<br>';
    echo '$order->post->post_content: ' . $order->post->post_content . '<br>';
    echo '$order->post->post_title: ' . $order->post->post_title . '<br>';
    echo '$order->post->post_excerpt: ' . $order->post->post_excerpt . '<br>';
    echo '$order->post->post_status: ' . $order->post->post_status . '<br>';
    echo '$order->post->comment_status: ' . $order->post->comment_status . '<br>';
    echo '$order->post->ping_status: ' . $order->post->ping_status . '<br>';
    echo '$order->post->post_password: ' . $order->post->post_password . '<br>';
    echo '$order->post->post_name: ' . $order->post->post_name . '<br>';
    echo '$order->post->to_ping: ' . $order->post->to_ping . '<br>';
    echo '$order->post->pinged: ' . $order->post->pinged . '<br>';
    echo '$order->post->post_modified: ' . $order->post->post_modified . '<br>';
    echo '$order->post->post_modified_gtm: ' . $order->post->post_modified_gtm . '<br>';
    echo '$order->post->post_content_filtered: ' . $order->post->post_content_filtered . '<br>';
    echo '$order->post->post_parent: ' . $order->post->post_parent . '<br>';
    echo '$order->post->guid: ' . $order->post->guid . '<br>';
    echo '$order->post->menu_order: ' . $order->post->menu_order . '<br>';
    echo '$order->post->post_type: ' . $order->post->post_type . '<br>';
    echo '$order->post->post_mime_type: ' . $order->post->post_mime_type . '<br>';
    echo '$order->post->comment_count: ' . $order->post->comment_count . '<br>';
    echo '$order->post->filter: ' . $order->post->filter . '<br>';
    echo '<h4>THE ORDER OBJECT (again):</h4>';
    echo '$order->order_date: ' . $order->order_date . '<br>';
    echo '$order->modified_date: ' . $order->modified_date . '<br>';
    echo '$order->customer_message: ' . $order->customer_message . '<br>';
    echo '$order->customer_note: ' . $order->customer_note . '<br>';
    echo '$order->post_status: ' . $order->post_status . '<br>';
    echo '$order->prices_include_tax: ' . $order->prices_include_tax . '<br>';
    echo '$order->tax_display_cart: ' . $order->tax_display_cart . '<br>';
    echo '$order->display_totals_ex_tax: ' . $order->display_totals_ex_tax . '<br>';
    echo '$order->display_cart_ex_tax: ' . $order->display_cart_ex_tax . '<br>';
    echo '$order->formatted_billing_address->protected: ' . $order->formatted_billing_address->protected . '<br>';
    echo '$order->formatted_shipping_address->protected: ' . $order->formatted_shipping_address->protected . '<br><br>';
    echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - <br><br>';

    // 2) Get the Order meta data
    $order_meta = get_post_meta($order_id);

    echo '<h3>RAW OUTPUT OF THE ORDER META DATA (ARRAY): </h3>';
    print_r($order_meta);
    echo '<br><br>';
    echo '<h3>THE ORDER META DATA (Using the array syntax notation):</h3>';
    echo '$order_meta[_order_key][0]: ' . $order_meta[_order_key][0] . '<br>';
    echo '$order_meta[_order_currency][0]: ' . $order_meta[_order_currency][0] . '<br>';
    echo '$order_meta[_prices_include_tax][0]: ' . $order_meta[_prices_include_tax][0] . '<br>';
    echo '$order_meta[_customer_user][0]: ' . $order_meta[_customer_user][0] . '<br>';
    echo '$order_meta[_billing_first_name][0]: ' . $order_meta[_billing_first_name][0] . '<br><br>';
    echo 'And so on ……… <br><br>';
    echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - <br><br>';

    // 3) Get the order items
    $items = $order->get_items();

    echo '<h3>RAW OUTPUT OF THE ORDER ITEMS DATA (ARRAY): </h3>';

    foreach ( $items as $item_id => $item_data ) {

        echo '<h4>RAW OUTPUT OF THE ORDER ITEM NUMBER: '. $item_id .'): </h4>';
        print_r($item_data);
        echo '<br><br>';
        echo 'Item ID: ' . $item_id. '<br>';
        echo '$item_data["product_id"] <i>(product ID)</i>: ' . $item_data['product_id'] . '<br>';
        echo '$item_data["name"] <i>(product Name)</i>: ' . $item_data['name'] . '<br>';

        // Using get_item_meta() method
        echo 'Item quantity <i>(product quantity)</i>: ' . $order->get_item_meta($item_id, '_qty', true) . '<br><br>';
        echo 'Item line total <i>(product quantity)</i>: ' . $order->get_item_meta($item_id, '_line_total', true) . '<br><br>';
        echo 'And so on ……… <br><br>';
        echo '- - - - - - - - - - - - - <br><br>';
    }
    echo '- - - - - - E N D - - - - - <br><br>';
}

代码进入函数。活动子主题(或主题)或任何插件文件中的php文件。

使用方法(例如,如果您的订单ID为159):

get_order_details(159);

此代码经过测试并有效。

2016年11月21日更新代码

冯驰
2023-03-14

WOOCOMMERCE 3.0版中的订单

自Woocommerce mega major更新3.0以来,情况发生了很大变化:

  • 对于WC\u Order对象,无法再像以前那样直接访问属性,并且会抛出一些错误
  • 现在,WC\u Order对象实例上需要新的WC\u OrderWC\u Abstract\u Ordergetter和setter方法
  • 此外,还有一些新的订单项目类:
    • WC\u订单\u项目class,
    • WC\u订单\u项目\u产品class,
    • WC\u订单\u项目\u税class,
    • WC\u订单\u项目\u发货class,
    • WC\u订单\u项目\u优惠券class,
    • WC\u订单\u项目\u费用class

    相关内容:
    如何从WooCommerce订单中获取客户详细信息?
    在WooCommerce 3中获取订单项目和WC_Order_Item_Product

    因此,在foreach循环中,Order items属性将无法像以前那样访问,您将不得不使用这些特定的getter和setter方法。

    使用一些WC\u-OrderWC\u-Abstract\u-Order方法(示例):

    // Get an instance of the WC_Order object (same as before)
    $order = wc_get_order( $order_id );
    
    $order_id  = $order->get_id(); // Get the order ID
    $parent_id = $order->get_parent_id(); // Get the parent order ID (for subscriptions…)
    
    $user_id   = $order->get_user_id(); // Get the costumer ID
    $user      = $order->get_user(); // Get the WP_User object
    
    $order_status  = $order->get_status(); // Get the order status (see the conditional method has_status() below)
    $currency      = $order->get_currency(); // Get the currency used  
    $payment_method = $order->get_payment_method(); // Get the payment method ID
    $payment_title = $order->get_payment_method_title(); // Get the payment method title
    $date_created  = $order->get_date_created(); // Get date created (WC_DateTime object)
    $date_modified = $order->get_date_modified(); // Get date modified (WC_DateTime object)
    
    $billing_country = $order->get_billing_country(); // Customer billing country
    
    // ... and so on ...
    

    对于作为条件方法的订单状态(其中“目标订单状态”需要定义并替换为订单状态以针对特定订单状态):

    if ( $order->has_status('completed') ) {
        // Do something
    }
    

    获取和访问订单数据属性(在值数组中):

    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );
    
    $order_data = $order->get_data(); // The Order data
    
    $order_id = $order_data['id'];
    $order_parent_id = $order_data['parent_id'];
    $order_status = $order_data['status'];
    $order_currency = $order_data['currency'];
    $order_version = $order_data['version'];
    $order_payment_method = $order_data['payment_method'];
    $order_payment_method_title = $order_data['payment_method_title'];
    $order_payment_method = $order_data['payment_method'];
    $order_payment_method = $order_data['payment_method'];
    
    ## Creation and modified WC_DateTime Object date string ##
    
    // Using a formated date ( with php date() function as method)
    $order_date_created = $order_data['date_created']->date('Y-m-d H:i:s');
    $order_date_modified = $order_data['date_modified']->date('Y-m-d H:i:s');
    
    // Using a timestamp ( with php getTimestamp() function as method)
    $order_timestamp_created = $order_data['date_created']->getTimestamp();
    $order_timestamp_modified = $order_data['date_modified']->getTimestamp();
    
    $order_discount_total = $order_data['discount_total'];
    $order_discount_tax = $order_data['discount_tax'];
    $order_shipping_total = $order_data['shipping_total'];
    $order_shipping_tax = $order_data['shipping_tax'];
    $order_total = $order_data['total'];
    $order_total_tax = $order_data['total_tax'];
    $order_customer_id = $order_data['customer_id']; // ... and so on
    
    ## BILLING INFORMATION:
    
    $order_billing_first_name = $order_data['billing']['first_name'];
    $order_billing_last_name = $order_data['billing']['last_name'];
    $order_billing_company = $order_data['billing']['company'];
    $order_billing_address_1 = $order_data['billing']['address_1'];
    $order_billing_address_2 = $order_data['billing']['address_2'];
    $order_billing_city = $order_data['billing']['city'];
    $order_billing_state = $order_data['billing']['state'];
    $order_billing_postcode = $order_data['billing']['postcode'];
    $order_billing_country = $order_data['billing']['country'];
    $order_billing_email = $order_data['billing']['email'];
    $order_billing_phone = $order_data['billing']['phone'];
    
    ## SHIPPING INFORMATION:
    
    $order_shipping_first_name = $order_data['shipping']['first_name'];
    $order_shipping_last_name = $order_data['shipping']['last_name'];
    $order_shipping_company = $order_data['shipping']['company'];
    $order_shipping_address_1 = $order_data['shipping']['address_1'];
    $order_shipping_address_2 = $order_data['shipping']['address_2'];
    $order_shipping_city = $order_data['shipping']['city'];
    $order_shipping_state = $order_data['shipping']['state'];
    $order_shipping_postcode = $order_data['shipping']['postcode'];
    $order_shipping_country = $order_data['shipping']['country'];
    

    获取订单项并使用WC_Order_Item_ProductWC_Order_Item方法访问数据:

    // Get an instance of the WC_Order object
    $order = wc_get_order($order_id);
    
    // Iterating through each WC_Order_Item_Product objects
    foreach ($order->get_items() as $item_key => $item ):
    
        ## Using WC_Order_Item methods ##
    
        // Item ID is directly accessible from the $item_key in the foreach loop or
        $item_id = $item->get_id();
    
        ## Using WC_Order_Item_Product methods ##
    
        $product      = $item->get_product(); // Get the WC_Product object
    
        $product_id   = $item->get_product_id(); // the Product id
        $variation_id = $item->get_variation_id(); // the Variation id
    
        $item_type    = $item->get_type(); // Type of the order item ("line_item")
    
        $item_name    = $item->get_name(); // Name of the product
        $quantity     = $item->get_quantity();  
        $tax_class    = $item->get_tax_class();
        $line_subtotal     = $item->get_subtotal(); // Line subtotal (non discounted)
        $line_subtotal_tax = $item->get_subtotal_tax(); // Line subtotal tax (non discounted)
        $line_total        = $item->get_total(); // Line total (discounted)
        $line_total_tax    = $item->get_total_tax(); // Line total tax (discounted)
    
        ## Access Order Items data properties (in an array of values) ##
        $item_data    = $item->get_data();
    
        $product_name = $item_data['name'];
        $product_id   = $item_data['product_id'];
        $variation_id = $item_data['variation_id'];
        $quantity     = $item_data['quantity'];
        $tax_class    = $item_data['tax_class'];
        $line_subtotal     = $item_data['subtotal'];
        $line_subtotal_tax = $item_data['subtotal_tax'];
        $line_total        = $item_data['total'];
        $line_total_tax    = $item_data['total_tax'];
    
        // Get data from The WC_product object using methods (examples)
        $product        = $item->get_product(); // Get the WC_Product object
    
        $product_type   = $product->get_type();
        $product_sku    = $product->get_sku();
        $product_price  = $product->get_price();
        $stock_quantity = $product->get_stock_quantity();
    
    endforeach;
    

    因此,使用get_data()方法允许我们访问受保护的数据(关联数组模式)…

 类似资料:
  • 问题内容: 在WooCommerce中,从以下代码行: 如何从订单ID获取WooCommerce订单详细信息? 问题答案: 3.0版以上的WOOCOMMERCE订单 自Woocommerce大型主要更新3.0+以来,事情已经发生了很多变化: 对于 对象,无法像以前一样直接访问属性,并且会引发一些错误。 现在,对象实例需要使用new 以及 getter和setter方法。 此外,还有一些用于订购商品

  • 我有一个Magento2商店,带有默认的结帐模块,使用nvp api下单。我需要稍后从paypal获取订单,并根据其状态执行一些操作,但是从rest api(/v2/checkout/orders/{id})我只获取使用相同rest api创建的订单的结果,而不是在签出期间放置的订单(使用nvp api)。 我已经在paypal上配置了一个沙箱帐户,用于nvp api调用的业务帐户可以访问payp

  • 问题内容: 我要获取exe / dll / sys文件的“文件描述”和“版权”,如右键单击文件并选择属性时,在“详细信息”选项卡中所示。 问题答案: 使用Windows API,您可以调用VerQueryValue以获取该信息。JNA有一个用于访问此API的类,称为Version。 这另一个问题有一些代码示例可以帮助您入门: 获取.exe的版本信息 这是一个读取产品名称的C代码示例,您可以将其转换

  • 请求地址 http://api.dc78.cn/Api/do_get_order 请求方式 GET 请求参数 参数 参数名称 描述 id 订单号 订单号,取自上一协议返回值 getpay=1 获取订单支付信息 获取订单支付信息 ,用于订单有支付时,一次性获取订单信息和支付信息 invoice=1 是否单独推送发票信息 设置该参数备注(memo字段)与发票信息(invoice字段)分别单独显示,不设

  • 本接口用于获取指定的订单详情。 订单id由平台通过推送接口向下推送,API模式收到推送消息后,需要根据消息中的订单id调用此接口获取订单详情。 DLL接口中已经封装好,向DLL主程序推送时已经准备好完整的订单详情数据,不需要单独调用此接口。 请求参数说明 参数 描述 必填 示例值 类型 最大长度 action 接口参数组 是 object └action 需要调用的接口名称 是 do_get_or

  • 我试图自定义我的Woocommerce电子邮件,我已经为它创建了一个html模板,它已经可以工作了。 我的问题是,它没有显示像这样的WooCommerce变量: 这样 或者这样 我总是这样说: 下面是HTML代码的一个片段: 所以它是一个普通的html文件,在我需要它们的地方带有这个php变量。我把html文件放进了Woocommerce 我是否必须将其全部打包到一个php文件中,或者如何让变量如