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.16 PHP 好きなところにノードを埋め込みたい
コンテンツの管理 7.15 Views Bulk Operation 標準のコンテンツ編集/ユーザ一覧ページが物足りません
コンテンツの管理 7.15 Pagenation 一つの長いページをBookなどのように連続して読ませることはできますか?
コンテンツの管理 8.6.x Commerce Commerce orderからすべての商品名を取得する方法
コンテンツの管理 8.9.x 写真の変更を行いたい
コンテンツの管理 6.x Views Flag Calendar 空き室予約のような仕組みは実現できますか?
コンテンツの管理 7.50 Administration menu 標準のユーザー管理ページ
コンテンツの管理 8.9.x Dialog Modal Canvas drupal 8 の標準機能のダイアログ/ポップアップを表示する方法
コンテンツの管理 7.38 Block Views node ノードページの編集タブをコンテキストメニューにしたい
コンテンツの管理 8.4x Action カスタムアクションを作成する方法
コンテンツの作成 7.38 node 代理投稿を可能にしたい
コンテンツの作成 8.9.x CSS キャプション付きの(右寄/左寄)画像を画像サイズを基準にきれいに表示する方法
コンテンツの作成 7.34 Entityreference prepopulate 新規ノード作成時のリファレンス方法
コンテンツの作成 7.34 CCK ノード入力フォームへパラメータを渡す
コンテンツの作成 7.15 Automatic_Nodetitles Automatic_Entity_Label ノード投稿フォームにタイトル入力フォームを表示しない方法
コンテンツの作成 7.26 PHP ノード内の一部(フィールド)を変更する
コンテンツの作成 7.15 Mailhandler メールを通じてnodeの投稿をするには
コンテンツの作成 8.4x コンテンツ作成時のフォームレイアウトの変更
コンテンツの作成 7.31 CKEditor CKEditorの新規生成ダイアログ内の初期値の変更
コンテンツの作成 8.4x PhpSpreadsheet Excelを読み書きする方法

ページ