<?php
/*
* Plugin Name : CustomerRank
*
* Copyright (C) BraTech Co., Ltd. All Rights Reserved.
* http://www.bratech.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\CustomerRank42\Event;
use Eccube\Event\TemplateEvent;
use Eccube\Service\CartService;
use Plugin\CustomerRank42\Repository\CustomerRankRepository;
use Plugin\CustomerRank42\Service\CustomerRankService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CartEvent implements EventSubscriberInterface
{
private $cartService;
private $customerRankService;
/**
* CustomerRankController constructor.
* @param CustomerRankRepository $customerRankRepository
*/
public function __construct(
CartService $cartService,
CustomerRankService $customerRankService
)
{
$this->cartService = $cartService;
$this->customerRankService = $customerRankService;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Cart/index.twig' => 'onTemplateCart',
];
}
public function onTemplateCart(TemplateEvent $event)
{
$parameters = $event->getParameters();
$least = $parameters['least'];
$isDeliveryFree = $parameters['is_delivery_free'];
$CustomerRank = $this->customerRankService->getCustomerRank(false);
if(!is_null($CustomerRank)){
$Carts = $this->cartService->getCarts();
if (strlen($CustomerRank->getDeliveryFreeCondition()) > 0) {
foreach ($Carts as $Cart) {
$isDeliveryFree[$Cart->getCartKey()] = false;
if ($CustomerRank->getDeliveryFreeCondition() <= $Cart->getTotalPrice()) {
$isDeliveryFree[$Cart->getCartKey()] = true;
} else {
$least[$Cart->getCartKey()] = $CustomerRank->getDeliveryFreeCondition() - $Cart->getTotalPrice();
}
}
}
}
$parameters['least'] = $least;
$parameters['is_delivery_free'] = $isDeliveryFree;
$event->setParameters($parameters);
}
}