<?php
/*
* Plugin Name : DeliveryFreeProduct
*
* Copyright (C) 2016 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\DeliveryFreeProduct42\Event;
use Eccube\Entity\Cart;
use Eccube\Entity\CartItem;
use Eccube\Event\TemplateEvent;
use Plugin\DeliveryFreeProduct42\Entity\DeliveryFreeConfig;
use Plugin\DeliveryFreeProduct42\Service\UtilService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CartEvent implements EventSubscriberInterface
{
/** @var UtilService */
private $deliveryFreeService;
/**
* DeliveryFreeProductEvent constructor.
* @param UtilService $deliveryFreeService
*/
public function __construct(UtilService $deliveryFreeService)
{
$this->deliveryFreeService = $deliveryFreeService;
}
public static function getSubscribedEvents()
{
return [
'Cart/index.twig' => 'onRenderCart',
];
}
public function onRenderCart(TemplateEvent $event)
{
$parameters = $event->getParameters();
/** @var Cart[] $Carts */
$Carts = $parameters['Carts'];
$arrDeliveryFree = [];
$originalDeliveryFree = $parameters['is_delivery_free'];
$condition = $this->deliveryFreeService->getConfig(DeliveryFreeConfig::DEFAULT_NAME);
$needAddTemplate = false;
$isCartFree = [];
foreach ($Carts as $key => $Cart) {
// default
$arrDeliveryFree[$Cart->getCartKey()] = $originalDeliveryFree[$Cart->getCartKey()];
if(!$originalDeliveryFree[$Cart->getCartKey()]) {
$count = 0;
$total = 0;
/** @var CartItem $cart_item */
foreach ($Cart->getCartItems() as $cart_item) {
if (!$cart_item->isProduct()) {
continue;
}
$total++;
if($cart_item->getProductClass()->getProduct()->getPlgDeliveryfreeproduct() == 1)$count++;
}
$deliv_free_flg = false;
if ($condition == DeliveryFreeConfig::MUST_ALL) {
if ($total == $count) {
$deliv_free_flg = true;
$needAddTemplate = true;
}
} else {
if ($count > 0) {
$deliv_free_flg = true;
$needAddTemplate = true;
}
}
$arrDeliveryFree[$Cart->getCartKey()] = $deliv_free_flg;
$isCartFree[$key] = $deliv_free_flg;
}
}
$parameters['is_delivery_free'] = $arrDeliveryFree;
$parameters['is_cart_free'] = $isCartFree;
$event->setParameters($parameters);
if ($needAddTemplate) {
$event->addSnippet("@DeliveryFreeProduct42/default/Cart/delivery_free_message.twig");
}
}
}