<?php
declare(strict_types=1);
/**
* @copyright 2020 Crehler Sp. z o. o.
* @link https://crehler.com/
* @support support@crehler.com
*
* @author Mateusz FlasiĆski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\Catalog\Subscriber;
use Crehler\Catalog\Struct\CatalogProductTypeStruct;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use function Symfony\Component\String\u;
class ProductLoadSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return ['product.loaded' => 'onProductLoaded'];
}
public function onProductLoaded(EntityLoadedEvent $event)
{
// /** @var ProductEntity $entity */
// foreach ($event->getEntities() as $entity) {
// if ($entity->getProductNumber() === 'PLA-ZIM-0006') {
// dd($entity);
// dd(1);
// }
// }
/** @var SalesChannelProductEntity $product */
foreach ($event->getEntities() as $product) {
$struct = new CatalogProductTypeStruct();
$foreignKeys = $product->getExtension('foreignKeys');
$struct->setArrangement($foreignKeys->offsetGet('arrangementId') !== null);
$struct->setInspirations($foreignKeys->offsetGet('inspirationId') !== null);
$product->addExtension(u($struct->getApiAlias())->camel()->toString(), $struct);
}
}
}