从订单处理和新购物车订单中获取剩余订单(差异)
Get remaining orders(difference) from orders processing and new cart orders
在我的 Woocommerce 网站中,我的最大订单容量为 50。
我正在尝试在我们关闭订单之前与他们购物车中剩余的订单进行沟通。
我需要从最大值 50 中减去每个订单中已处理的商品总数 + 购物车中的新订单。
我已经尝试使用此代码:
function display_woocommerce_order_count() {
global $woocommerce;
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'shop_order_status',
'field' => 'slug',
'terms' => array('processing')
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
$order_count = 0;
foreach( $order as $product ) {
$order_item = $product['qty'];
if($qty) {
$order_count += $order_item;
}
}
ob_start();
//Echo the number of items in cart.
$count = $woocommerce->cart->cart_contents_count;
//Difference max - orders processing - cart items
$total_diff = 50 - number_format($order_count) - $count;
echo $total_diff;
return ob_get_clean();
}
我怎样才能使它按预期工作?
谢谢
要根据现有客户 "processing" 订单商品和实际购物车商品计算剩余订单,您可以尝试此自定义函数(使用可选的 $user_id
论点)。
这是代码:
function get_remaining_orders( $user_id = null ){
if( empty($user_id) && is_user_logged_in() )
$user_id = get_current_user_id();
if( ! empty($user_id) && ! is_admin() ){
$order_max = 50;
$processing_orders_items_count = 0;
$cart_count = 0;
$customer_orders = get_posts( array(
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => 'shop_order',
'numberposts' => -1,
'post_status' => 'wc-processing' // 'processing' order status only
) );
if(!empty($customer_orders))
foreach($customer_orders as $customer_order_values){
$customer_order = wc_get_order( $customer_order_values->ID );
$processing_orders_items_count += $customer_order->get_item_count('line_item');
}
if(!WC()->cart->is_empty())
$cart_count = WC()->cart->get_cart_contents_count( );
$ouput = $order_max - ($processing_orders_items_count + $cart_count);
return $ouput;
}
}
// USAGE: for a specific user ID (here for example $user_id is 22):
get_remaining_orders( 22 );
// USAGE: returning the value in a variable for current user:
$remaining_orders = get_remaining_orders();
// USAGE: displaying the value for current user (example):
echo 'Total remaining orders are ' . get_remaining_orders();
代码进入您的活动子主题(或主题)的 function.php 文件。或者在任何插件 php 文件中。
此代码已经过测试并且可以正常工作。
Update for a "not user specific case":
function get_remaining_orders(){
if( !is_admin() ){
$order_max = 50;
$processing_orders_items_count = 0;
$cart_count = 0;
$customer_orders = get_posts( array(
'post_type' => 'shop_order',
'numberposts' => -1,
'post_status' => 'wc-processing' // 'processing' order status only
) );
if(!empty($customer_orders))
foreach($customer_orders as $customer_order_values){
$customer_order = wc_get_order( $customer_order_values->ID );
$processing_orders_items_count += $customer_order->get_item_count('line_item');
}
if(!WC()->cart->is_empty())
$cart_count = WC()->cart->get_cart_contents_count( );
$ouput = $order_max - ($processing_orders_items_count + $cart_count);
return $ouput;
}
}
// USAGE: returning the value in a variable:
$remaining_orders = get_remaining_orders();
// USAGE: displaying the value (example):
echo 'Total remaining orders are ' . get_remaining_orders();
这应该会像您预期的那样工作……
在我的 Woocommerce 网站中,我的最大订单容量为 50。
我正在尝试在我们关闭订单之前与他们购物车中剩余的订单进行沟通。
我需要从最大值 50 中减去每个订单中已处理的商品总数 + 购物车中的新订单。
我已经尝试使用此代码:
function display_woocommerce_order_count() {
global $woocommerce;
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'shop_order_status',
'field' => 'slug',
'terms' => array('processing')
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
$order_count = 0;
foreach( $order as $product ) {
$order_item = $product['qty'];
if($qty) {
$order_count += $order_item;
}
}
ob_start();
//Echo the number of items in cart.
$count = $woocommerce->cart->cart_contents_count;
//Difference max - orders processing - cart items
$total_diff = 50 - number_format($order_count) - $count;
echo $total_diff;
return ob_get_clean();
}
我怎样才能使它按预期工作?
谢谢
要根据现有客户 "processing" 订单商品和实际购物车商品计算剩余订单,您可以尝试此自定义函数(使用可选的 $user_id
论点)。
这是代码:
function get_remaining_orders( $user_id = null ){
if( empty($user_id) && is_user_logged_in() )
$user_id = get_current_user_id();
if( ! empty($user_id) && ! is_admin() ){
$order_max = 50;
$processing_orders_items_count = 0;
$cart_count = 0;
$customer_orders = get_posts( array(
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => 'shop_order',
'numberposts' => -1,
'post_status' => 'wc-processing' // 'processing' order status only
) );
if(!empty($customer_orders))
foreach($customer_orders as $customer_order_values){
$customer_order = wc_get_order( $customer_order_values->ID );
$processing_orders_items_count += $customer_order->get_item_count('line_item');
}
if(!WC()->cart->is_empty())
$cart_count = WC()->cart->get_cart_contents_count( );
$ouput = $order_max - ($processing_orders_items_count + $cart_count);
return $ouput;
}
}
// USAGE: for a specific user ID (here for example $user_id is 22):
get_remaining_orders( 22 );
// USAGE: returning the value in a variable for current user:
$remaining_orders = get_remaining_orders();
// USAGE: displaying the value for current user (example):
echo 'Total remaining orders are ' . get_remaining_orders();
代码进入您的活动子主题(或主题)的 function.php 文件。或者在任何插件 php 文件中。
此代码已经过测试并且可以正常工作。
Update for a "not user specific case":
function get_remaining_orders(){
if( !is_admin() ){
$order_max = 50;
$processing_orders_items_count = 0;
$cart_count = 0;
$customer_orders = get_posts( array(
'post_type' => 'shop_order',
'numberposts' => -1,
'post_status' => 'wc-processing' // 'processing' order status only
) );
if(!empty($customer_orders))
foreach($customer_orders as $customer_order_values){
$customer_order = wc_get_order( $customer_order_values->ID );
$processing_orders_items_count += $customer_order->get_item_count('line_item');
}
if(!WC()->cart->is_empty())
$cart_count = WC()->cart->get_cart_contents_count( );
$ouput = $order_max - ($processing_orders_items_count + $cart_count);
return $ouput;
}
}
// USAGE: returning the value in a variable:
$remaining_orders = get_remaining_orders();
// USAGE: displaying the value (example):
echo 'Total remaining orders are ' . get_remaining_orders();
这应该会像您预期的那样工作……