<?php
/*
* Plugin Name : DeliveryPlus4
*
* 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\DeliveryPlus42\Event;
use Eccube\Event\TemplateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductEvent implements EventSubscriberInterface
{
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Product/list.twig' => 'onTemplateProductList',
'Product/detail.twig' => 'onTemplateProductDetail',
];
}
public function onTemplateProductDetail(TemplateEvent $event)
{
$parameters = $event->getParameters();
$Product = $parameters['Product'];
$class_categories[$Product->getId()]['__unselected']['#'] = [
'weight' => '',
'size' => '',
];
foreach($Product->getProductClasses() as $ProductClass){
if(!$ProductClass->isVisible())continue;
$ClassCategory1 = $ProductClass->getClassCategory1();
$ClassCategory2 = $ProductClass->getClassCategory2();
if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
continue;
}
$class_category_id1 = $ClassCategory1 ? (string) $ClassCategory1->getId() : '__unselected2';
$class_category_id2 = $ClassCategory2 ? (string) $ClassCategory2->getId() : '';
$class_categories[$Product->getId()][$class_category_id1]['#'] = [
'weight' => '',
'size' => '',
];
$class_categories[$Product->getId()][$class_category_id1]['#'.$class_category_id2] = [
'weight' => number_format($ProductClass->getDeliveryplusWeight(),2),
'size' => number_format($ProductClass->getDeliveryplusSize(),2),
];
}
$parameters['DeliveryPlusParams'] = $class_categories;
$event->setParameters($parameters);
$twig = '@DeliveryPlus42/default/Product/product_js.twig';
$event->addSnippet($twig);
}
public function onTemplateProductList(TemplateEvent $event)
{
$parameters = $event->getParameters();
$pagination = $parameters['pagination'];
$class_categories = [];
foreach($pagination as $Product){
$class_categories[$Product->getId()]['__unselected']['#'] = [
'weight' => '',
'size' => '',
];
foreach($Product->getProductClasses() as $ProductClass){
if(!$ProductClass->isVisible())continue;
$ClassCategory1 = $ProductClass->getClassCategory1();
$ClassCategory2 = $ProductClass->getClassCategory2();
if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
continue;
}
$class_category_id1 = $ClassCategory1 ? (string) $ClassCategory1->getId() : '__unselected2';
$class_category_id2 = $ClassCategory2 ? (string) $ClassCategory2->getId() : '';
$class_categories[$Product->getId()][$class_category_id1]['#'] = [
'weight' => '',
'size' => '',
];
$class_categories[$Product->getId()][$class_category_id1]['#'.$class_category_id2] = [
'weight' => number_format($ProductClass->getDeliveryplusWeight(),2),
'size' => number_format($ProductClass->getDeliveryplusSize(),2),
];
}
}
$parameters['DeliveryPlusParams'] = $class_categories;
$event->setParameters($parameters);
$twig = '@DeliveryPlus42/default/Product/product_js.twig';
$event->addSnippet($twig);
}
}