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ファイルに入るか、また、どのプラグインファイル内:ここ
この機能based on this answerのコードがあります。
この関数では、オプションのuser_id
引数(必要な場合)を受け入れることができます。 とuser_id
56
値についてたとえば、あなたは、このように機能を使用します。
// 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();
条件付きifステートメントでこの関数を使用して、somme WooCommerce hooksまたはrelated templatesを使用してカートに追加ボタンを表示または置き換えることができます。
あなたはこのタスクを支援し、このコードを含む新しい質問をし、あなたがそれをやりたいことの詳細を提供することができます。
このコードはテスト済みであり、動作します。
参考文献:Checking if customer has already bought something in WooCommerce
あなたがものを書くために必要があるとしている「私は、この機能のためのプラグインを使用する必要はありません」。 [this tutorial](https://www.skyverge.com/blog/get-all-woocommerce-orders-for-a-customer/)から始めてください。注文は単純な 'WP_Query'であり、日付パラメータを使用して詳細を絞り込むことができます。オーダーはオーダーアイテムとは異なりますので、必要なものはわかりません。私たちはあなたを導くことができるようにコードを投稿してください。 – helgatheviking