php - 检查客户完成的订单问题
问题描述
我正在尝试编写一个小插件,在管理面板的订单详细信息中,将显示新客户或退货客户,并显示有关客户已完成状态的订单数量的信息。现在代码可以工作了,但是我发现了几种代码不能正常工作的情况。我通过 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>';
}
}
}
我如何确保无论用户是否在订单期间注册,我始终收到所有订单的正确数量的已完成订单?
解决方案
推荐阅读
- http-redirect - 清漆直接请求内部 ip
- r - R中的for循环并通过创建新列来附加值
- javascript - 如何根据javascript中的输入值调用函数?
- r - 从 R 中的数据框中逐行提取值
- django - 从服务器到客户端的一种方式后台通知(Django 告诉 Angular 4 在后面发生了什么)
- javascript - 滚动是否会使元素失去焦点并触发 onBlur 事件?
- symfony - Doctrine QueryBuilder 从一对多关系中选择
- javascript - HTML数据表不起作用
- python - 无法在 Bazel genrule 中使用 Python 的 sh 模块
- raspberry-pi3 - BLE gatttool 从 bluno 读取串行数据?