首页 > 解决方案 > 检查客户完成的订单问题

问题描述

我正在尝试编写一个小插件,在管理面板的订单详细信息中,将显示新客户或退货客户,并显示有关客户已完成状态的订单数量的信息。现在代码可以工作了,但是我发现了几种代码不能正常工作的情况。我通过 ID 或电子邮件检查客户并计算已完成订单的数量。但是,如果同一个用户下了几个订单,首先作为注册用户,我会在订单上看到由客户 ID 计算的数字,如果客户以访客身份下订单,我会看到电子邮件计算的不同数字. 也就是说,在客户在他的帐户下下订单的那些订单上,我会看到例如数字 3,以及他作为客人下订单的地方,但他的电子邮件是 4。

我的代码:

<?php
/*
 * Plugin Name: Kasha returning customers
 * Plugin URI: https://urich.org
 * Description: Displays information returning customer or new, and also displays the number of completed orders
 * Version: 1.0.0
 * Author: Urich
 * License: GPLv2 or later
 */


if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    add_action( 'woocommerce_admin_order_data_after_order_details', 'render_customer_is_new_field', 1 );
    function render_customer_is_new_field( $order ) {
        if( is_a( $order, 'WC_Order') ) {
            $args = array(
                'limit'  => -1,
                'status' => array('wc-completed'),
                'return' => 'ids',
            );

            if ( $order->get_user_id() > 1 ) {
                $args['customer_id'] = $order->get_customer_id();
            } else {
                $args['customer'] = $order->get_billing_email();
            }

            $orders_ids   = wc_get_orders( $args );
            $orders_count = intval( count($orders_ids) );
            $markData     = ( $orders_count >= 2 )
                ? array('status-processing', 'Returning customer')
                : array('status-on-hold', 'New Customer');

            echo '
            <p class="kasha-customer-counter" style="float: left">
                <mark class="order-status '. $markData[0] .'">
                    <span>'. $markData[1] .' <strong style="color:#ff0000;font-weight: 900;"> '. $orders_count .'</strong></span>
                </mark>
            </p>';
        }
    }
}

我如何确保无论用户是否在订单期间注册,我始终收到所有订单的正确数量的已完成订单?

标签: phpwordpresswoocommerceplugins

解决方案


推荐阅读