微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 在WooCommerce 3中获取订单商品和WC_Order_Item_Product

好了,看看WooCommerce 3.0中的变化,似乎你不能再直接访问这个类,所以我认为这个代码需要改变,因为它正在吐出一个错误

$order_item_id = 15;
$order_item = new WC_Order_Item_Product($order_item_id);
$return = $order_item->get_id() ? $order_item : false;

但是,令人尴尬的是,我不确定如何更改此代码以在此类的最新版本中使用正确的新getter和setter函数,该类不再具有构造.怎么做得好?我没有看到任何获取订单项的功能与上面相同. https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item_Product.html

也许我在这里忽视一些事情?

解决方法:

If you use the get_id() method, you get your item ID which is 15 in your code.

获取产品ID:
获取Product id的正确WC_Order_Item_Product方法是:get_product_id()

获取变体ID:
获取Product id的正确WC_Order_Item_Product方法是:get_variation_id()

获取订单ID
获取订单ID的正确WC_Order_Item_Product方法是:get_order_id()

获取WC_Product对象
获取WC_Product对象的正确WC_Order_Item_Product方法是:
get_product()

获取WC_Order对象
获取WC_order对象的正确WC_Order_Item_Product方法是:
get_order()

使用WC_Data方法获取和取消保护数据和元数据:
GET_DATA()
get_Meta_data()

从订单商品ID获取WC_Product对象:

$order_item_id = 15;
$item = new WC_Order_Item_Product($order_item_id);

// The product ID
$product_id = $item->get_product_id(); 

// The variation ID
$product_id = $item->get_variation_id(); 

// The WC_Product object
$product = $item->get_product(); 

// The quantity
$order_id = $item->get_quantity(); 

// The order ID
$order_id = $item->get_order_id(); 

// The WC_Order object
$order = $item->get_order(); 

// The item ID
$item_id = $item->get_id(); // which is your $order_item_id

// The product name
$product_name = $item->get_name(); // … OR: $product->get_name();

//Get the product SKU (using WC_Product method)
$sku = $product->get_sku();

从WC_Order对象获取订单商品(并使用WC_product对象):

$order_id = 156; // The order_id

// get an instance of the WC_Order object
$order = wc_get_order( $order_id );

// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach( $order->get_items() as $item_id => $item ){
    //Get the product ID
    $product_id = $item->get_product_id();

    //Get the variation ID
    $product_id = $item->get_variation_id();

    //Get the WC_Product object
    $product = $item->get_product();

    // The quantity
    $product_name = $item->get_quantity();

    // The product name
    $product_name = $item->get_name(); // … OR: $product->get_name();

    //Get the product SKU (using WC_Product method)
    $sku = $product->get_sku();
}

访问和取消保护WC_Order_Item_Product数据:

您可以使用所有WC_Order_Item_Product data方法,也可以使用WC_Data以下方法取消保护数据:

$order_id = 156; // The order_id

// get an instance of the WC_Order object
$order = wc_get_order( $order_id );

// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach( $order->get_items() as $item_id => $item ){

    // Get the common data in an array: 
    $item_product_data_array = $item->get_data();

    // Get the special Meta data in an array: 
    $item_product_Meta_data_array = $item->get_Meta_data();

    // Get the specific Meta data from a Meta_key: 
    $Meta_value = $item->get_Meta( 'custom_Meta_key', true );

    // Get all additional Meta data (formatted in an unprotected array)
    $formatted_Meta_data = $item->get_formatted_Meta_data( ' ', true );
}

作为参考:

> Get the metadata of an order item in woocommerce 3
> How to get WooCommerce order details

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐