我需要在Woocommerce用户ID来获得在上个月用户的完成购买。WooCommerce - 获取完成状态的订单由用户在一段时间内
用户有水平(金,银):
- 金卡会员可以购买每月4项;
- 银会员每月可以购买1件物品。
我需要将项目添加到购物车前进行检查。我不想只为这个功能使用一个插件(无法找到,顺便说一句)。
这可能吗?
我该如何做到这一点?
感谢
我需要在Woocommerce用户ID来获得在上个月用户的完成购买。WooCommerce - 获取完成状态的订单由用户在一段时间内
用户有水平(金,银):
我需要将项目添加到购物车前进行检查。我不想只为这个功能使用一个插件(无法找到,顺便说一句)。
这可能吗?
我该如何做到这一点?
感谢
很可能得到总项目计数在过去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
参数(如果需要)。例如,对于user_id
与56
值,你将使用的功能,这样一来:
// For user ID: "56".
$number_of_items_in_last_month = current_customer_month_count('56');
的功能将得到当前用户ID在$user_id = get_current_user_id();
,如果你不设置参数user_id
值,这样一来:
// For current logged user.
$number_of_items_in_last_month = current_customer_month_count();
您可以在有条件使用此功能if语句来隐藏或更换添加到购物车按钮,在索姆河WooCommerce hooks或related templates。
你可以在这个任务中得到帮助,询问包括这个代码在内的一个新问题,并提供关于你如何去做的更多细节。
此代码已经过测试并可正常工作。
参考文献:Checking if customer has already bought something in WooCommerce
与此问题无关,但是,如何根据这些计划在结帐页面上简单添加运费价值?如果用户是黄金,添加shippin 4 $如果不是免费送货!我在woocommerce文档中看到计算运送功能,但不起作用! :(或者我不知道如何使用它 – Amino
“我不想使用插件只是这个功能”你将不得不写一个。你应该从[本教程]开始(https://www.skyverge.com/blog/get-all-woocommerce-orders-for-a-customer/)。订单是一个简单的'WP_Query',你可以使用日期参数进一步细化。虽然订单与订购商品不同,但我不确定您需要什么。发布您的代码,以便我们可以指导您。 – helgatheviking