Created
July 13, 2021 06:04
-
-
Save kurozumi/074b19da21f78688591a5a991db2064a to your computer and use it in GitHub Desktop.
購入完了ページで会員に会員グループを設定する処理
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This file is part of CustomerGroupProduct | |
* | |
* Copyright(c) Akira Kurozumi <[email protected]> | |
* | |
* 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