購入件数/購入額/購入時刻を得る
$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)); // 直近の購入時刻