Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Created July 13, 2021 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurozumi/074b19da21f78688591a5a991db2064a to your computer and use it in GitHub Desktop.
Save kurozumi/074b19da21f78688591a5a991db2064a to your computer and use it in GitHub Desktop.
購入完了ページで会員に会員グループを設定する処理
<?php
/**
* This file is part of CustomerGroupProduct
*
* Copyright(c) Akira Kurozumi <info@a-zumi.net>
*
* https://a-zumi.net
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\CustomerGroupProduct\EventListener;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Entity\Customer;
use Eccube\Entity\Order;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ShoppingCompleteListener implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* ShoppingCompleteListener constructor.
* @param EntityManagerInterface $entityManager
*/
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
EccubeEvents::FRONT_SHOPPING_COMPLETE_INITIALIZE => 'onFrontShoppingCompleteInitialize'
];
}
/**
* @param EventArgs $event
*/
public function onFrontShoppingCompleteInitialize(EventArgs $event): void
{
/** @var Order $order */
$order = $event->getArgument('Order');
$customer = $order->getCustomer();
if ($customer instanceof Customer) {
foreach ($order->getOrderItems() as $orderItem) {
if ($orderItem->isProduct()) {
$product = $orderItem->getProduct();
if ($product->hasRegisterGroup()) {
$customer->getGroups()->clear();
$customer->addGroup($product->getRegisterGroup());
}
}
}
$this->entityManager->persist($customer);
$this->entityManager->flush();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment