希望这段代码对你有帮助。我已使用代码完成当前登录用户的订单数据(或者您可以使用您的状态)。然后得到他们的产品数量,现在我们可以获得之前要购买的产品。刚刚完成了这个代码。
$customer_orders = get_posts( array(
\'numberposts\' => - 1,
\'meta_key\' => \'_customer_user\',
\'meta_value\' => get_current_user_id(),
\'post_type\' => \'shop_order\', // WC orders post type
\'post_status\' => \'wc-completed\' // Only orders with status "completed"
//use status such as "wc-processing"/"wc-on-hold"etc.
) );
$previous_count = 0;
// Going through each current customer orders
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order );
$items = $order->get_items();
// Going through each current customer products bought in the order
foreach ( $items as $item ) {
$id = $item[\'product_id\'];
// If product not in array, add it
if ( ! array_key_exists( $item[\'product_id\'], $products ) ) {
$products[ $id ] = array(
\'name\' => $item[\'name\'],
\'count\' => 0,
);
}
// Increment Product <code>count</code> from cart quantity
//$products[ $id ][\'count\'] += $item->get_quantity();// get the total product
$previous_count += $item->get_quantity();
}
}
$cart_count = WC()->cart->get_cart_contents_count();
$remaining_product_count = $previous_count+$cart_count;
if($remaining_product_count < 10){
$remaining_product_count = 10-$remaining_product_count;
echo $remaining_product_count;
}