<?php declare(strict_types=1);
namespace zenit\PlatformDataBadges\Subscriber;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Event\ThemeCompilerEnrichScssVariablesEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ThemeVariablesSubscriber implements EventSubscriberInterface
{
/**
* @var string
*/
private $configPath = 'zenitPlatformDataBadges.config.';
/**
* @var SystemConfigService
*/
protected $systemConfig;
// add the `SystemConfigService` to your constructor
public function __construct(SystemConfigService $systemConfig)
{
$this->systemConfig = $systemConfig;
}
public static function getSubscribedEvents(): array
{
return [
ThemeCompilerEnrichScssVariablesEvent::class => 'onAddVariables'
];
}
public function onAddVariables(ThemeCompilerEnrichScssVariablesEvent $event)
{
$shopId = $event->getSalesChannelId();
/**
* @var string $variantBadgeColor
* @var string $stockBadgeColor
* @var string $salesBadgeColor
* @var string $discountBadgeColor
* @var string $closeoutBadgeColor
* @var string $shippingfreeBadgeColor
* @var string $topsellerBadgeColor
* @var string $newBadgeColor
* @var string $releaseBadgeColor
* @var string $preorderBadgeColor
* @var string $manufacturerBadgeColor
* @var string $custom1BadgeColor
* @var string $custom2BadgeColor
* @var string $custom3BadgeColor
*/
$variantBadgeColor = $this->systemConfig->get($this->configPath . 'variantBadgeColor', $shopId);
$stockBadgeColor = $this->systemConfig->get($this->configPath . 'stockBadgeColor', $shopId);
$salesBadgeColor = $this->systemConfig->get($this->configPath . 'salesBadgeColor', $shopId);
$discountBadgeColor = $this->systemConfig->get($this->configPath . 'discountBadgeColor', $shopId);
$closeoutBadgeColor = $this->systemConfig->get($this->configPath . 'closeoutBadgeColor', $shopId);
$shippingfreeBadgeColor = $this->systemConfig->get($this->configPath . 'shippingfreeBadgeColor', $shopId);
$topsellerBadgeColor = $this->systemConfig->get($this->configPath . 'topsellerBadgeColor', $shopId);
$newBadgeColor = $this->systemConfig->get($this->configPath . 'newBadgeColor', $shopId);
$releaseBadgeColor = $this->systemConfig->get($this->configPath . 'releaseBadgeColor', $shopId);
$preorderBadgeColor = $this->systemConfig->get($this->configPath . 'preorderBadgeColor', $shopId);
$manufacturerBadgeColor = $this->systemConfig->get($this->configPath . 'manufacturerBadgeColor', $shopId);
$custom1BadgeColor = $this->systemConfig->get($this->configPath . 'custom1BadgeColor', $shopId);
$custom2BadgeColor = $this->systemConfig->get($this->configPath . 'custom2BadgeColor', $shopId);
$custom3BadgeColor = $this->systemConfig->get($this->configPath . 'custom3BadgeColor', $shopId);
if (isset($variantBadgeColor) && !empty($variantBadgeColor)) {
$event->addVariable('zen-data-badges-variant-color', $variantBadgeColor);
} else {
$event->addVariable('zen-data-badges-variant-color', '#bcc1c7');
};
if (isset($stockBadgeColor) && !empty($stockBadgeColor)) {
$event->addVariable('zen-data-badges-stock-color', $stockBadgeColor);
} else {
$event->addVariable('zen-data-badges-stock-color', '#5f7285');
};
if (isset($salesBadgeColor) && !empty($salesBadgeColor)) {
$event->addVariable('zen-data-badges-sales-color', $salesBadgeColor);
} else {
$event->addVariable('zen-data-badges-sales-color', '#e52427');
};
if (isset($discountBadgeColor) && !empty($discountBadgeColor)) {
$event->addVariable('zen-data-badges-discount-color', $discountBadgeColor);
} else {
$event->addVariable('zen-data-badges-discount-color', '#e52427');
};
if (isset($closeoutBadgeColor) && !empty($closeoutBadgeColor)) {
$event->addVariable('zen-data-badges-close-out-color', $closeoutBadgeColor);
} else {
$event->addVariable('zen-data-badges-close-out-color', '#e74c3c');
};
if (isset($shippingfreeBadgeColor) && !empty($shippingfreeBadgeColor)) {
$event->addVariable('zen-data-badges-shipping-free-color', $shippingfreeBadgeColor);
} else {
$event->addVariable('zen-data-badges-shipping-free-color', '#2ecc71');
};
if (isset($topsellerBadgeColor) && !empty($topsellerBadgeColor)) {
$event->addVariable('zen-data-badges-topseller-color', $topsellerBadgeColor);
} else {
$event->addVariable('zen-data-badges-topseller-color', '#ffbd5d');
};
if (isset($newBadgeColor) && !empty($newBadgeColor)) {
$event->addVariable('zen-data-badges-new-color', $newBadgeColor);
} else {
$event->addVariable('zen-data-badges-new-color', '#3cc261');
};
if (isset($releaseBadgeColor) && !empty($releaseBadgeColor)) {
$event->addVariable('zen-data-badges-release-color', $releaseBadgeColor);
} else {
$event->addVariable('zen-data-badges-release-color', '#ffbd5d');
};
if (isset($preorderBadgeColor) && !empty($preorderBadgeColor)) {
$event->addVariable('zen-data-badges-preorder-color', $preorderBadgeColor);
} else {
$event->addVariable('zen-data-badges-preorder-color', '#ffbd5d');
};
if (isset($manufacturerBadgeColor) && !empty($manufacturerBadgeColor)) {
$event->addVariable('zen-data-badges-manufacturer-color', $manufacturerBadgeColor);
} else {
$event->addVariable('zen-data-badges-manufacturer-color', '#5f7285');
};
if (isset($custom1BadgeColor) && !empty($custom1BadgeColor)) {
$event->addVariable('zen-data-badges-custom-first-color', $custom1BadgeColor);
} else {
$event->addVariable('zen-data-badges-custom-first-color', '#f1c40f');
};
if (isset($custom2BadgeColor) && !empty($custom2BadgeColor)) {
$event->addVariable('zen-data-badges-custom-second-color', $custom2BadgeColor);
} else {
$event->addVariable('zen-data-badges-custom-second-color', '#f1c40f');
};
if (isset($custom3BadgeColor) && !empty($custom3BadgeColor)) {
$event->addVariable('zen-data-badges-custom-third-color', $custom3BadgeColor);
} else {
$event->addVariable('zen-data-badges-custom-third-color', '#f1c40f');
};
/**
* @var string $listingsFontSize
* @var string $listingsRadiusLeft
* @var string $listingsRadiusRight
* @var string $listingsPadding
* @var string $listingsMarginLeft
*/
$listingsFontSize = $this->systemConfig->get($this->configPath . 'listingsFontSize', $shopId);
$listingsRadiusLeft = $this->systemConfig->get($this->configPath . 'listingsRadiusLeft', $shopId) ?? '0';
$listingsRadiusRight = $this->systemConfig->get($this->configPath . 'listingsRadiusRight', $shopId) ?? '0';
$listingsPadding = $this->systemConfig->get($this->configPath . 'listingsPadding', $shopId) ?? '0';
$listingsPaddingVertical = ($listingsPadding >= 5) ? (int)$listingsPadding - 5 : '0';
$listingsMarginLeft = ($listingsRadiusLeft > 0) ? '10' : '0';
$event->addVariable('zen-data-badges-listings-font-size', $listingsFontSize . 'px');
$event->addVariable('zen-data-badges-listings-height', (int)$listingsFontSize + 5 . 'px');
$event->addVariable('zen-data-badges-listings-radius-left', $listingsRadiusLeft . 'px');
$event->addVariable('zen-data-badges-listings-radius-right', $listingsRadiusRight . 'px');
$event->addVariable('zen-data-badges-listings-padding-horizontal', $listingsPadding . 'px');
$event->addVariable('zen-data-badges-listings-padding-vertical', $listingsPaddingVertical. 'px');
$event->addVariable('zen-data-badges-listings-margin-left', $listingsMarginLeft . 'px');
/**
* @var string $detailsFontSize
* @var string $detailsRadiusLeft
* @var string $detailsRadiusRight
* @var string $detailsPadding
* @var string $detailsMarginLeft
*/
$detailsFontSize = $this->systemConfig->get($this->configPath . 'detailsFontSize', $shopId);
$detailsRadiusLeft = $this->systemConfig->get($this->configPath . 'detailsRadiusLeft', $shopId) ?? '0';
$detailsRadiusRight = $this->systemConfig->get($this->configPath . 'detailsRadiusRight', $shopId) ?? '0';
$detailsPadding = $this->systemConfig->get($this->configPath . 'detailsPadding', $shopId) ?? '0';
$detailsPaddingVertical = ($detailsPadding >= 5) ? (int)$detailsPadding - 5 : '0';
$detailsMarginLeft = ($detailsRadiusLeft > 0) ? '10' : '0';
$event->addVariable('zen-data-badges-details-font-size', $detailsFontSize . 'px');
$event->addVariable('zen-data-badges-details-height', (int)$detailsFontSize + 5 . 'px');
$event->addVariable('zen-data-badges-details-radius-left', $detailsRadiusLeft . 'px');
$event->addVariable('zen-data-badges-details-radius-right', $detailsRadiusRight . 'px');
$event->addVariable('zen-data-badges-details-padding-horizontal', $detailsPadding . 'px');
$event->addVariable('zen-data-badges-details-padding-vertical', $detailsPaddingVertical . 'px');
$event->addVariable('zen-data-badges-details-margin-left', $detailsMarginLeft . 'px');
}
}