php获取当前订单排名,php – 从当前用户订单中获取订单ID在WooCommerce中

Updated Added Compatibility with WooCommerce 3+ (January 2018)

以下是获取所有客户订单并查看每个客户订单的每个项目所需的代码:

## ==> Define HERE the statuses of that orders

$order_statuses = array('wc-on-hold', 'wc-processing', 'wc-completed');

## ==> Define HERE the customer ID

$customer_user_id = get_current_user_id(); // current user ID here for example

// Getting current customer orders

$customer_orders = wc_get_orders( array(

'meta_key' => '_customer_user',

'meta_value' => $customer_user_id,

'post_status' => $order_statuses,

'numberposts' => -1

) );

// Loop through each customer WC_Order objects

foreach($customer_orders as $order ){

// Order ID (added WooCommerce 3+ compatibility)

$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

// Iterating through current orders items

foreach($order->get_items() as $item_id => $item){

// The corresponding product ID (Added Compatibility with WC 3+)

$product_id = method_exists( $item, 'get_product_id' ) ? $item->get_product_id() : $item['product_id'];

// Order Item data (unprotected on Woocommerce 3)

if( method_exists( $item, 'get_data' ) ) {

$item_data = $item->get_data();

$subtotal = $item_data['subtotal'];

} else {

$subtotal = wc_get_order_item_meta( $item_id, '_line_subtotal', true );

}

// TEST: Some output

echo '

Subtotal: '.$subtotal.'


';

// Get a specific meta data

$item_color = method_exists( $item, 'get_meta' ) ? item->get_meta('pa_color') : wc_get_order_item_meta( $item_id, 'pa_color', true );

// TEST: Some output

echo '

Color: '.$item_color.'


';

}

}

此代码经过测试和运行

有关:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值