Ami*_*ino 3 php wordpress product orders woocommerce
我需要在上个月通过Woocommerce中的USER ID获得用户完成的购买.
用户有级别(金牌,银牌):
我需要在将商品添加到购物车之前检查一下.我不想仅为此功能使用插件(无法找到,BTW).
那可能吗?
我怎样才能做到这一点?
谢谢
这是可能得到的总项目计数买由当前客户在过去的30天.
以下是基于此答案的此函数的代码:
function current_customer_month_count( $user_id=null ) {
if ( empty($user_id) ){
$user_id = get_current_user_id();
}
// Date calculations to limit the query
$today_year = date( 'Y' );
$today_month = date( 'm' );
$day = date( 'd' );
if ($today_month == '01') {
$month = '12';
$year = $today_year - 1;
} else{
$month = $today_month - 1;
$month = sprintf("%02d", $month);
$year = $today_year - 1;
}
// ORDERS FOR LAST 30 DAYS (Time calculations)
$now = strtotime('now');
// Set the gap time (here 30 days)
$gap_days = 30;
$gap_days_in_seconds = 60*60*24*$gap_days;
$gap_time = $now - $gap_days_in_seconds;
// The query arguments
$args = array(
// WC orders post type
'post_type' => 'shop_order',
// Only orders with status "completed" (others common status: 'wc-on-hold' or 'wc-processing')
'post_status' => 'wc-completed',
// all posts
'numberposts' => -1,
// for current user id
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'date_query' => array(
//orders published on last 30 days
'relation' => 'OR',
array(
'year' => $today_year,
'month' => $today_month,
),
array(
'year' => $year,
'month' => $month,
),
),
);
// Get all customer orders
$customer_orders = get_posts( $args );
$count = 0;
if (!empty($customer_orders)) {
$customer_orders_date = array();
// Going through each current customer orders
foreach ( $customer_orders as $customer_order ){
// Conveting order dates in seconds
$customer_order_date = strtotime($customer_order->post_date);
// Only past 30 days orders
if ( $customer_order_date > $gap_time ) {
$customer_order_date;
$order = new WC_Order( $customer_order->ID );
$order_items = $order->get_items();
// Going through each current customer items in the order
foreach ( $order_items as $order_item ){
$count++;
}
}
}
return $count;
}
}
Run Code Online (Sandbox Code Playgroud)
此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中.
该函数接受一个可选user_id参数(如果需要).例如,对于user_idwith 56值,您将使用该函数,这样:
// For user ID: "56".
$number_of_items_in_last_month = current_customer_month_count('56');
Run Code Online (Sandbox Code Playgroud)
该函数将当前用户的ID中$user_id = get_current_user_id();,如果你不设置一个参数user_id值时,这样说:
// For current logged user.
$number_of_items_in_last_month = current_customer_month_count();
Run Code Online (Sandbox Code Playgroud)
您可以在条件if语句中使用此函数来隐藏或替换通过somme WooCommerce 挂钩或相关模板的"添加到购物车"按钮.
您可以在该任务中获得帮助,询问包含此代码的新问题,并提供有关如何执行此操作的更多详细信息.
此代码经过测试和运行.
| 归档时间: |
|
| 查看次数: |
5747 次 |
| 最近记录: |