<?php declare(strict_types=1);
namespace Ott\BrandCategorySeoUrlFix\Subscriber;
use Doctrine\DBAL\Connection;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
use Shopware\Core\Framework\Event\ProgressFinishedEvent;
use Shopware\Core\Framework\Uuid\Uuid;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Content\Category\Event\CategoryIndexerEvent;
class EntityIndexerSubscriber implements EventSubscriberInterface
{
private const MOEBELFIRST_SALES_CHANNEL_ID = 'c1aedaadc2d54272bc6b5450488a895e';
private const BRAND_CATEGORY_ID = '981e2967dbf141f1a574231eeb721ac9';
private Connection $connection;
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
public static function getSubscribedEvents(): array
{
return [
ProgressFinishedEvent::class => 'onProgressFinishedEvent',
CategoryIndexerEvent::class => 'onCategoryIndexerEvent',
];
}
public function onProgressFinishedEvent(ProgressFinishedEvent $event): void
{
if ('category.indexer' !== $event->getMessage()) {
return;
}
$this->fixSeoUrl();
}
public function onCategoryIndexerEvent(CategoryIndexerEvent $event): void
{
if (\in_array(self::BRAND_CATEGORY_ID, $event->getIds())) {
$this->fixSeoUrl();
}
}
private function fixSeoUrl(): void
{
$statement = <<<'SQL'
DELETE FROM seo_url WHERE seo_path_info = 'marken/'
SQL;
$this->connection->prepare($statement)->execute();
$statement = <<<'SQL'
INSERT INTO seo_url
(
id,
language_id,
sales_channel_id,
foreign_key,
route_name,
path_info,
seo_path_info,
is_canonical,
is_modified,
is_deleted,
custom_fields,
created_at,
updated_at
)
VALUES
(
UNHEX(:id),
UNHEX(:languageId),
UNHEX(:salesChannelId),
UNHEX(:foreignKey),
'frontend.cbax.manufacturer.index',
'/cbax/manufacturer/index',
'marken/',
1,
0,
0,
NULL,
NOW(),
NOW()
)
SQL;
$preparedStatement = $this->connection->prepare($statement);
$preparedStatement->bindValue('id', Uuid::randomHex(), \PDO::PARAM_STR);
$preparedStatement->bindValue('languageId', DEFAULTS::LANGUAGE_SYSTEM, \PDO::PARAM_STR);
$preparedStatement->bindValue('salesChannelId', self::MOEBELFIRST_SALES_CHANNEL_ID, \PDO::PARAM_STR);
$preparedStatement->bindValue('foreignKey', self::BRAND_CATEGORY_ID, \PDO::PARAM_STR);
$preparedStatement->execute();
}
}