<?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\Event\EventArgs;
use Plugin\DeliveryFreeProduct42\Entity\DeliveryFreeConfig;
use Plugin\DeliveryFreeProduct42\Service\UtilService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class DeliveryCoolEvent implements EventSubscriberInterface
{
/** @var UtilService */
private $deliveryFreeService;
/**
* DeliveryFreeProductEvent constructor.
* @param UtilService $deliveryFreeService
*/
public function __construct(UtilService $deliveryFreeService)
{
$this->deliveryFreeService = $deliveryFreeService;
}
public static function getSubscribedEvents()
{
return [
'deliverycool.delivery_free.condition' => 'hookDeliveryCoolDeliveryFree',
];
}
public function hookDeliveryCoolDeliveryFree(EventArgs $event)
{
$isFree = $event->getArgument('isFree');
$Shipping = $event->getArgument('Shipping');
$range = $this->deliveryFreeService->getConfig(DeliveryFreeConfig::RANGE_NAME);
$condition = $this->deliveryFreeService->getConfig(DeliveryFreeConfig::DEFAULT_NAME);
if($range == DeliveryFreeConfig::BY_SHIPPING){
$count = 0;
$total = 0;
foreach($Shipping->getProductOrderItems() as $orderItem){
$total++;
if($orderItem->getProduct()->getPlgDeliveryfreeproduct() == 1)$count++;
}
if($condition == DeliveryFreeConfig::MUST_ALL){
if($total == $count)$isFree = true;
}else{
if($count > 0)$isFree = true;
}
}elseif($range == DeliveryFreeConfig::BY_ALL){
$Order = $Shipping->getOrder();
$count = 0;
$total = 0;
/** @var OrderItem $orderDetail */
foreach($Order->getProductOrderItems() as $orderItem){
$total++;
if($orderItem->getProduct()->getPlgDeliveryfreeproduct() == 1)$count++;
}
if($condition == DeliveryFreeConfig::MUST_ALL){
if($total == $count)$isFree = true;
}else{
if($count > 0)$isFree = true;
}
}
$event->setArgument('isFree',$isFree);
}
}