<?php
/**
* Copyright(c) 2019 SYSTEM_KD
* Date: 2019/08/18
*/
namespace Plugin\PinpointSaleDx\EventSubscriber;
use Doctrine\Common\Annotations\Annotation\Required;
use Eccube\Entity\BaseInfo;
use Eccube\Entity\Cart;
use Eccube\Entity\CartItem;
use Eccube\Event\TemplateEvent;
use Eccube\Repository\BaseInfoRepository;
use Plugin\PinpointSaleDx\Config\ConfigSetting;
use Plugin\PinpointSaleDx\Config\ConfigSupportTrait;
use Plugin\PinpointSaleDx\Service\PlgConfigService\ConfigService;
use Plugin\PinpointSaleDx\Service\TwigRenderService\TwigRenderService;
use Plugin\PinpointSaleDx\Service\PinpointSaleDxService;
use Plugin\PinpointSaleDx\Service\PluginLinkService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CartEventSubscriber implements EventSubscriberInterface
{
/** @var TwigRenderService */
protected $twigRenderService;
/** @var ConfigService */
protected $configService;
/** @var BaseInfo */
protected $baseInfo;
/** @var PinpointSaleDxService */
protected $pinpointSaleService;
/** @var PluginLinkService */
protected $pluginLinkService;
/** @var \Plugin\QuantityDiscountDx\Service\ConfigService */
protected $quantityDiscountConfigService;
use ConfigSupportTrait;
/**
* CartEventSubscriber constructor.
* @param TwigRenderService $twigRenderService
* @param ConfigService $configService
* @param BaseInfoRepository $baseInfoRepository
* @param PinpointSaleDxService $pinpointSaleService
* @param PluginLinkService $pluginLinkService
* @throws \Exception
*/
public function __construct(
TwigRenderService $twigRenderService,
ConfigService $configService,
BaseInfoRepository $baseInfoRepository,
PinpointSaleDxService $pinpointSaleService,
PluginLinkService $pluginLinkService
)
{
$this->twigRenderService = $twigRenderService;
$this->configService = $configService;
$this->baseInfo = $baseInfoRepository->get();
$this->pinpointSaleService = $pinpointSaleService;
$this->pluginLinkService = $pluginLinkService;
}
/**
* @Required
*
* @param \Plugin\QuantityDiscountDx\Service\ConfigService|null $configService
* @return void
*/
public function setQuantityDiscountConfig(?\Plugin\QuantityDiscountDx\Service\ConfigService $configService)
{
$this->quantityDiscountConfigService = $configService;
}
/**
* カートテンプレート
*
* @param TemplateEvent $event
*/
public function onTemplateCartIndex(TemplateEvent $event)
{
// 送料無料判別設定
if (Configsetting::DISCOUNT_BEFORE_PRICE
== $this->configService->getKeyInteger(Configsetting::SETTING_KEY_DELIVERY_FEE_MODE)) {
$Carts = $event->getParameter('Carts');
$least = $event->getParameter('least');
$isDeliveryFree = $event->getParameter('is_delivery_free');
/** @var Cart $Cart */
foreach ($Carts as $Cart) {
if ($this->baseInfo->getDeliveryFreeAmount()) {
$subTotal = 0;
/** @var CartItem $cartItem */
foreach ($Cart->getCartItems() as $cartItem) {
$pinpointSaleItem = $this->pinpointSaleService->getPinpointSaleDxItem($cartItem->getProductClass());
$subTotal += $cartItem->getPriceIncTax() * $cartItem->getQuantity();
if ($pinpointSaleItem) {
// タイムセール値引き分
$discount = $pinpointSaleItem->getDiscountPriceIncTax() * $cartItem->getQuantity();
$subTotal += $discount;
}
}
if (!$isDeliveryFree[$Cart->getCartKey()] && $this->baseInfo->getDeliveryFreeAmount() <= $subTotal) {
$isDeliveryFree[$Cart->getCartKey()] = true;
} else {
$least[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeAmount() - $subTotal;
}
}
}
$event->setParameter('least', $least);
$event->setParameter('is_delivery_free', $isDeliveryFree);
}
// タイムセール名称設定
$this->setDiscountName($event);
if (!$this->configService->isKeyBool(ConfigSetting::SETTING_KEY_CART_VIEW)) {
return;
}
$this->twigRenderService->initRenderService($event);
$eachChild = $this->twigRenderService
->eachChildBuilder()
->findThis()
->targetFindAndIndexKey('#pinpoint_sale_price_', 'pinpointSaleIndex')
->setInsertModeAppend();
$this->twigRenderService
->eachBuilder()
->find('.ec-cartRow')
->find('.ec-cartRow__summary')
->setEachIndexKey('pinpointSaleIndex')
->each($eachChild->build());
$this->twigRenderService->addSupportSnippet(
'@PinpointSaleDx/default/Cart/index_ex.twig'
);
}
public function onCartIndexHook(TemplateEvent $event)
{
// まとめ買いプラグインとの競合解消
if ($this->pluginLinkService->isActivePlugin('QuantityDiscountDx')) {
if ((Configsetting::DISCOUNT_BEFORE_PRICE
== $this->configService->getKeyInteger(Configsetting::SETTING_KEY_DELIVERY_FEE_MODE))
&&
($this->quantityDiscountConfigService::DISCOUNT_BEFORE_PRICE
== $this->quantityDiscountConfigService->getKeyInteger($this->quantityDiscountConfigService::SETTING_KEY_DELIVERY_FEE_MODE))) {
$Carts = $event->getParameter('Carts');
$least = $event->getParameter('least');
$isDeliveryFree = $event->getParameter('is_delivery_free');
/** @var Cart $Cart */
foreach ($Carts as $Cart) {
if ($this->baseInfo->getDeliveryFreeAmount()) {
$subTotal = 0;
/** @var CartItem $cartItem */
foreach ($Cart->getCartItems() as $cartItem) {
$pinpointSaleItem = $this->pinpointSaleService->getPinpointSaleDxItem($cartItem->getProductClass());
if ($pinpointSaleItem) {
// タイムセール値引き分
$subTotal += $cartItem->getProductClass()->getPrice02IncTax() * $cartItem->getQuantity();
$discount = $pinpointSaleItem->getDiscountPriceIncTax() * $cartItem->getQuantity();
$subTotal += $discount;
} else {
$subTotal += $cartItem->getPrice() * $cartItem->getQuantity();
}
}
if (!$isDeliveryFree[$Cart->getCartKey()] && $this->baseInfo->getDeliveryFreeAmount() <= $subTotal) {
$isDeliveryFree[$Cart->getCartKey()] = true;
} else {
$least[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeAmount() - $subTotal;
}
}
}
$event->setParameter('least', $least);
$event->setParameter('is_delivery_free', $isDeliveryFree);
}
}
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * ['eventName' => 'methodName']
* * ['eventName' => ['methodName', $priority]]
* * ['eventName' => [['methodName1', $priority], ['methodName2']]]
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
'Cart/index.twig' => [
['onTemplateCartIndex'],
['onCartIndexHook', -10],
],
];
}
}