Commerceで歴代購入数/金額を集計する方法

カテゴリ Commerce コアバージョン 8.6.x 関連モジュール Commerce Payment Order
購入件数/購入額/購入時刻を得る
    $ltv_query = \Drupal::database()->select('commerce_payment', 'p')
      ->condition('p.state', 'completed');
    $ltv_query->join('commerce_order', 'o', "o.order_id = p.order_id");
    $ltv_query->condition('o.uid', $this->account->id());
    $order_count_query = clone $ltv_query;
    $order_count_query->condition('p.payment_gateway', 'THIS_GATEWAY', '');
    $last_order_query = clone $order_count_query;
    $last_order_query
      ->fields('p', ['amount__number', 'completed'])
      ->orderBy('p.completed', 'DESC')
      ->range(0, 1);
    $last_order = $last_order_query->execute()->fetchObject();
    $last_order_amount = $last_order->amount__number ?? 0;
    $last_order_at = $last_order->completed ?? REQUEST_TIME;

    $order_count = $order_count_query->countQuery()->execute()->fetchField(); // 購入件数(THIS_GATEWAY以外)
    $ltv = $ltv_query->countQuery()->execute()->fetchField(); // 購入件数
    $last_order_amount = floor($last_order_amount); // 直近の購入額
    $last_order_at = floor((REQUEST_TIME - $last_order_at) / (60*60*24)); // 直近の購入時刻

OTHER FAQ

Drupal開発・運用の疑問/質問の答えはここに

無料ユーザー登録すると質問できます。

カテゴリ昇順で並び替える Core Ver. 関連モジュール タイトル
コンテンツの作成 7.15 Mailhandler メールを通じてnodeの投稿をするには
コンテンツの作成 7.50 Inline Entity Form Drupal6のフィールドグループのようなモジュールはありませんか?
コンテンツの作成 8.9.x node ノード保存時の前回値の参照方法
コンテンツの作成 7.50 Expanding Textareas Textareaフォームの行サイズを自動調整するモジュール
コンテンツの作成 8.4x node node保存前に色々やる方法
コンテンツの作成 6.x Views カルーセルのように回転するコンテンツを作りたい
コンテンツの作成 8.9.x vim nkf ファイルのエンコーディングを確認、変更する方法
コンテンツの作成 7.23 maxlength テキストフィールドの最大長のチェック&カウントダウン
コンテンツの作成 8.4x json jQueryにjsonで渡す方法
コンテンツの作成 7.38 Webform Webformでテーブル形式にしたい
コンテンツの作成 8.9.x CSS キャプション付きの(右寄/左寄)画像を画像サイズを基準にきれいに表示する方法
コンテンツの作成 8.4x archiver zipファイル圧縮・解凍する方法
コンテンツの作成 7.38 node 代理投稿を可能にしたい
コンテンツの作成 7.34 CCK ノード入力フォームへパラメータを渡す
コンテンツの作成 7.26 PHP ノード内の一部(フィールド)を変更する
コンテンツの作成 7.22 node Views ノードに付いたコメントに関する情報を得る
コンテンツの作成 7.31 CKEditor CKEditorの新規生成ダイアログ内の初期値の変更
コンテンツの作成 7.34 Entityreference prepopulate 新規ノード作成時のリファレンス方法
コンテンツの作成 8.4x コンテンツ作成時のフォームレイアウトの変更
コンテンツの作成 7.38 CKEditor CKEditorの「ソース」に入力したタグが消える

ページ