【问题标题】:Get remaining orders(difference) from orders processing and new cart orders从订单处理和新购物车订单中获取剩余订单(差异)
【发布时间】:2017-05-25 23:49:45
【问题描述】:

在我的 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();
}

我怎样才能使它按预期工作?

谢谢

【问题讨论】:

    标签: php wordpress woocommerce cart orders


    【解决方案1】:

    要从现有客户“处理”订单商品和实际购物车商品中获取剩余订单计算,您可以尝试此自定义函数(使用可选的 $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 文件中。

    此代码已经过测试并且可以正常工作。


    针对“非用户特定案例”的更新:

    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();
    

    这应该可以按您的预期工作......

    【讨论】:

    • 谢谢@LoicTheAztec 我想要一个通用的,因为我将在商店的前端使用它。它不是特定于用户的
    • @omukiguy 好的,我明白你的意思了......你的问题不是很清楚......我必须针对这个一般情况进行更新。
    • @omukiguy ...更新了我的代码以用于通用用法...
    猜你喜欢
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 2012-05-03
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多