当前位置: 首页 > 面试题库 >

如何获取WooCommerce订单详细信息

巫马泓
2023-03-14
问题内容

在WooCommerce中,从以下代码行:

$order = new WC_Order( $order_id );

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


问题答案:

3.0版以上的WOOCOMMERCE订单

自Woocommerce大型主要更新3.0+以来,事情已经发生了很多变化:

  • 对于 WC_Order 对象,无法像以前一样直接访问属性,并且会引发一些错误。
  • 现在,对象实例需要使用new WC_Order 以及WC_Abstract_Order getter和setter方法WC_Order
  • 此外,还有一些用于订购商品的新类:
    • WC_Order_Item 类,
    • WC_Order_Item_Productclass
    • _WC_Order_Item_Tax class
    • _WC_Order_Item_Shippingclass
    • _WC_Order_Item_Coupon class
    • _WC_Order_Item_Fee class

因此,无法像以前那样在 foreach 循环中访问Orderitems属性,而必须使用这些特定的getter和setter方法]。

使用一些 WC_OrderWC_Abstract_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 
$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 ...

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

// 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['cart_tax'];
$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() method可以使我们访问受保护的数据(关联数组模式)…



 类似资料:
  • 从以下行代码输入: 如何从订单id获取WooCommerce订单详细信息?

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

  • 我正在搜索和尝试它2天没有成功,请帮助。 我想筛选woocommerce订单,以便根据产品属性将其他详细信息从db添加到订单详细信息页面,但我找不到适合此任务的woocommerce操作/筛选器挂钩。这里假设变量; 如果,那么我需要将自定义数据从数据库添加到订单详细信息页面。 注意:我不想添加额外的元框,而是想更改订单明细表: 将默认产品映像替换为存储在数据库和中的映像, 在产品名称下面添加包含自

  • 上次WooCommerce更新后,订单详细信息不再显示在感谢页面上。从那时起,我使用WooCommerce Storefront主题开发了一个儿童主题。不管我做了什么,我看到的只是感谢页面上的“谢谢”信息。 到目前为止我所尝试的: > 对整个进程进行故障排除,以查找任何可能出错的问题。这包括以下内容: 检查了调用order-details模板和it相关操作挂钩的wc模板函数(均存在) 确保我的Ch

  • 我有一个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代码示例,您可以将其转换