php – WooCommerce – 用户在一段时间内完成状态订单
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – WooCommerce – 用户在一段时间内完成状态订单,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2872字,纯文字阅读大概需要5分钟。
内容图文
![php – WooCommerce – 用户在一段时间内完成状态订单](/upload/InfoBanner/zyjiaocheng/734/bf61dcc6e18d451a844e5c72b3e81390.jpg)
我需要在上个月通过Woocommerce中的USER ID获得用户完成的购买.
用户有级别(金牌,银牌):
>金卡会员每月可以购买4件商品;
>银卡会员每月可以购买1件商品.
我需要在将商品添加到购物车之前检查一下.我不想仅为此功能使用插件(无法找到,BTW).
那可能吗?
我怎样才能做到这一点?
谢谢
解决方法:
可以获得当前客户在过去30天内购买的总物品数量.
以下是此功能based on this answer的代码:
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;
}
}
此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中.
该函数接受可选的user_id参数(如果需要).例如,对于具有56值的user_id,您将使用该函数,这样:
// For user ID: "56".
$number_of_items_in_last_month = current_customer_month_count('56');
该函数将获取$user_id = get_current_user_id();中的当前用户ID,如果您没有设置参数user_id值,这样:
// For current logged user.
$number_of_items_in_last_month = current_customer_month_count();
You can use this function in a conditional if statement to hide or replace the add-to-cart button through somme WooCommerce 07001 or 07002.
You could get helped in that task, asking a new question including this code, and providing more details about how you want to do it.
此代码经过测试和运行.
参考文献:Checking if customer has already bought something in WooCommerce
内容总结
以上是互联网集市为您收集整理的php – WooCommerce – 用户在一段时间内完成状态订单全部内容,希望文章能够帮你解决php – WooCommerce – 用户在一段时间内完成状态订单所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。