EC-CUBE4 会員情報CSVへの保有ポイントの追加

EC-CUBE4ではEC-CUBE3系で無くなったポイント機能が標準で搭載されています。
このポイントですがデフォルトでは、会員情報のCSV出力では各会員の保有ポイントが出力されません。

今回、EC-CUBE4系で運営していたECサイトを閉じることになり、
別サービスに既に登録 or 移行して頂いたユーザーに別サービスで使える保有ポイント分のクーポンを発行する必要があったため、会員情報CSVへの保有ポイントの追加手順を備忘録として残しておきます。

前提

データベース構造が下記の状態であることを確認

  • 会員情報:dtb_customerテーブル
  • ポイント情報:dtb_customerテーブルのpointカラム

PHPファイルの修正

CustomerController.php末尾の方に下記のコードを追加
(export関数の下辺り)

/**
    * ポイント付き会員CSVの出力
    *
    * @Route("/%eccube_admin_route%/customer/export_with_point_service", name="admin_customer_export_with_point_service")
    *
    * @param Request $request
    *
    * @return StreamedResponse
    */
public function exportWithPointService(Request $request)
{
    // タイムアウトを無効にする
    set_time_limit(0);

    // SQL loggerを無効にする
    $em = $this->entityManager;
    $em->getConfiguration()->setSQLLogger(null);

    $response = new StreamedResponse();
    $response->setCallback(function () use ($request) {
        // CSV種別を元に初期化
        $this->csvExportService->initCsvType(CsvType::CSV_TYPE_CUSTOMER);

        // ヘッダ行の出力
        $this->csvExportService->exportHeader();
        
        // ポイント列のヘッダーを追加出力
        $fp = fopen('php://output', 'a');
        fwrite($fp, ',所持ポイント');
        fwrite($fp, "\n");
        fclose($fp);

        // 会員データ検索用のクエリビルダを取得
        $qb = $this->csvExportService->getCustomerQueryBuilder($request);

        // データ行の出力
        $this->csvExportService->setExportQueryBuilder($qb);
        $this->csvExportService->exportData(function ($entity, $csvService) use ($request) {
            $Csvs = $csvService->getCsvs();

            /** @var $Customer \Eccube\Entity\Customer */
            $Customer = $entity;

            $ExportCsvRow = new \Eccube\Entity\ExportCsvRow();

            // CSV出力項目と合致するデータを取得
            foreach ($Csvs as $Csv) {
                $ExportCsvRow->setData($csvService->getData($Csv, $Customer));

                $event = new EventArgs(
                    [
                        'csvService' => $csvService,
                        'Csv' => $Csv,
                        'Customer' => $Customer,
                        'ExportCsvRow' => $ExportCsvRow,
                    ],
                    $request
                );
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_CSV_EXPORT, $event);

                $ExportCsvRow->pushData();
            }

            // ポイントデータを追加
            $ExportCsvRow->setData($Customer->getPoint() ?? 0);
            $ExportCsvRow->pushData();

            // 出力
            $csvService->fputcsv($ExportCsvRow->getRow());
        });
    });

    $now = new \DateTime();
    $filename = 'customer_with_point_'.$now->format('YmdHis').'.csv';
    $response->headers->set('Content-Type', 'application/octet-stream');
    $response->headers->set('Content-Disposition', 'attachment; filename='.$filename);

    log_info('ポイント付き会員CSVファイル名', [$filename]);

    return $response;
}

twigファイルの修正

追加場所やデザインはお好みで
(下記は参考程度に…)

{# 既存のCSV出力ボタンの近くに追加 #}
<div class="btn-group" role="group">
    <a class="btn btn-ec-regular" href="{{ url('admin_customer_export') }}">
        <i class="fa fa-download me-1"></i>
        CSVダウンロード
    </a>
    <a class="btn btn-ec-regular" href="{{ url('admin_customer_export_with_point_service') }}">
        <i class="fa fa-download me-1"></i>
        ポイント付きCSV
    </a>
</div>

SQL直接実行

DBに直接アクセスできる場合は、下記みたいな感じのクエリ結果をCSVとしてエクスポートした方が手軽で良さそう(試してないので動作するかは不明)

SELECT 
    customer_id,
    name01,
    name02, 
    email,
    point,
    create_date
FROM dtb_customer 
WHERE del_flg = 0
ORDER BY customer_id;

サービスを立ち上げるのはもちろん大変ですが、サービスを終了させるのも意外と大変です。