EC-CUBE4で会員登録時に会員登録ポイントを付与させる方法
EC-CUBE4ではポイント機能は標準で対応されるようになりましたが、商品購入時にしかポイントが付与されません。
EC-CUBE2系をお使いの方であれば、会員登録ポイントは付与できますので同じような機能をEC-CUBE4でも実現させてみます。
変更させる箇所は以下の通りです。
今回はDBで会員登録ポイントを管理するのではなく、設定ファイルで管理させてます。
- app/config/eccube/packages/eccube.yaml
eccube_point_entry_point: 200 # ポイント機能;会員登録で獲得するポイント数
記述する箇所は最下行で構いません。この例だと200ポイントを付与させるようにしています。
- src/Eccube/Controller/EntryController.php
use Eccube\Service\PointHelper; ・ ・ ・ /** * @var PointHelper */ protected $pointHelper; ・ ・ ・ /** * EntryController constructor. * * @param CartService $cartService * @param CustomerStatusRepository $customerStatusRepository * @param MailService $mailService * @param BaseInfoRepository $baseInfoRepository * @param CustomerRepository $customerRepository * @param EncoderFactoryInterface $encoderFactory * @param ValidatorInterface $validatorInterface * @param TokenStorageInterface $tokenStorage * @param PointHelper $pointHelper */ public function __construct( CartService $cartService, CustomerStatusRepository $customerStatusRepository, MailService $mailService, BaseInfoRepository $baseInfoRepository, CustomerRepository $customerRepository, EncoderFactoryInterface $encoderFactory, ValidatorInterface $validatorInterface, TokenStorageInterface $tokenStorage, PointHelper $pointHelper ) { $this->customerStatusRepository = $customerStatusRepository; $this->mailService = $mailService; $this->BaseInfo = $baseInfoRepository->get(); $this->customerRepository = $customerRepository; $this->encoderFactory = $encoderFactory; $this->recursiveValidator = $validatorInterface; $this->tokenStorage = $tokenStorage; $this->cartService = $cartService; $this->pointHelper = $pointHelper; } ・ ・ ・ // 会員登録ポイントの付与 case 'complete': log_info('会員登録開始'); $encoder = $this->encoderFactory->getEncoder($Customer); $salt = $encoder->createSalt(); $password = $encoder->encodePassword($Customer->getPassword(), $salt); $secretKey = $this->customerRepository->getUniqueSecretKey(); $Customer ->setSalt($salt) ->setPassword($password) ->setSecretKey($secretKey) ->setPoint(0); $this->entityManager->persist($Customer); $this->entityManager->flush(); // 会員登録ポイントの付与 // 所有ポイントが更新されるので再度flushする必要あり $this->pointHelper->addEntryPoint($Customer); $this->entityManager->persist($Customer); $this->entityManager->flush();
PointHelperを利用して会員登録ポイントを付与させる処理を追加します。
- src/Eccube/Service/PointHelper.php
・ ・ ・ /** * @var EccubeConfig */ private $eccubeConfig; ・ ・ ・ /** * PointHelper constructor. * * @param BaseInfoRepository $baseInfoRepository * @param EntityManagerInterface $entityManager */ public function __construct(BaseInfoRepository $baseInfoRepository, EntityManagerInterface $entityManager, EccubeConfig $eccubeConfig) { $this->baseInfoRepository = $baseInfoRepository; $this->entityManager = $entityManager; $this->eccubeConfig = $eccubeConfig; } ・ ・ ・ // 会員登録時ポイントを付与する public function addEntryPoint(Customer $Customer) { $point = $this->eccubeConfig['eccube_point_entry_point']; $pointHistory = new PointHistory(); $pointHistory->setRecordType(PointHistory::TYPE_ADD); $pointHistory->setRecordEvent(PointHistory::EVENT_ENTRY); $pointHistory->setPoint($point); $pointHistory->setCustomer($Customer); $em = $this->entityManager; $em->persist($pointHistory); $em->flush($pointHistory); // ポイントの付与 $Customer->setPoint($Customer->getPoint() + $point); }
PointHelper.phpに会員登録時にポイントを付与する関数を追加します。
今回はかなり省略して変更点を記述していますので、もう少し詳しく書いて欲しければコメントしてください。