2016-08-30 15 views
3

昨年のユーザーの購入をWoocommerceのUSER IDで取得する必要があります。WooCommerce - 一定期間内にユーザーが完了したステータス注文

ユーザーがレベル(ゴールド、シルバー)を持っている:

  • ゴールド会員は毎月4つのアイテムを購入することができます。
  • シルバーメンバーは1ヶ月に1アイテムを購入できます。

カートに商品を追加する前に、これを確認する必要があります。私はちょうどこの機能(これは、BTW)を見つけることができませんでしたプラグインを使用したくない。

これは可能ですか?
どうすればいいですか?

おかげ

+0

あなたがものを書くために必要があるとしている「私は、この機能のためのプラグインを使用する必要はありません」。 [this tutorial](https://www.skyverge.com/blog/get-all-woocommerce-orders-for-a-customer/)から始めてください。注文は単純な 'WP_Query'であり、日付パラメータを使用して詳細を絞り込むことができます。オーダーはオーダーアイテムとは異なりますので、必要なものはわかりません。私たちはあなたを導くことができるようにコードを投稿してください。 – helgatheviking

答えて

6

それは、過去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; 
    } 
} 

このコードは、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルに入るか、また、どのプラグインファイル内:ここ

この機能based on this answerのコードがあります。

この関数では、オプションのuser_id引数(必要な場合)を受け入れることができます。 user_id56値についてたとえば、あなたは、このように機能を使用します。

// 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

+0

この問題に関連していませんが、これらのプランに基づいてチェックアウトページに送料値を追加する方法はありますか?もしユーザーが金であれば、送料が無料であればshippin 4 $を追加してください!私はwoocommerceドキュメントではなく、動作していない計算輸送機能を参照してください! :(または私はそれをどのように使用するか分からない – Amino

関連する問題