<?php declare(strict_types=1);
namespace Wato\DigitecGalaxusExport;
use Shopware\Core\Content\ProductExport\ProductExportEntity;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Context;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
class WatoDigitecGalaxusExport extends Plugin
{
const TEMPLATE_NAMES = array(
'ProductData',
'ProductProperties',
'ProductAccessory',
'ProductStockPricing',
);
public function install(InstallContext $installContext): void {
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetRepository->create([
[
'name' => 'wato_digitec_galaxus_export',
'config' => [
'label' => [
'de-DE' => 'WATO Digitec Galaxus Export'
]
],
'customFields' => [
[
'name' => 'wato_digitec_galaxus_export_tares',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'TARES'
],
'type' => 'text',
'customFieldType' => 'text',
'placeholder' => [
'de-DE' => 'Schweizer Zolltarifnummer'
],
'customFieldPosition' => 1
]
],
[
'name' => 'wato_digitec_galaxus_export_taric',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'TARIC'
],
'type' => 'text',
'customFieldType' => 'text',
'placeholder' => [
'de-DE' => 'Warencode für den EU-Raum'
],
'customFieldPosition' => 2
]
]
],
'relations' => [
[
'id' => '01230000000000000000000000000000',
'entityName' => 'product'
]
]
]
], $installContext->getContext());
// use existing storefront configuration
$salesChannelRepository = $this->container->get('sales_channel.repository');
$store = $salesChannelRepository->search(
(new Criteria())->addFilter(
new EqualsFilter('type.id', Defaults::SALES_CHANNEL_TYPE_STOREFRONT)
)->addAssociation('domains'),
$installContext->getContext()
)->first();
if (!$store) {
return;
}
$domain = $store->getDomains() ? $store->getDomains()->first() : null;
if (!$domain) {
return;
}
$data = array();
foreach (static::TEMPLATE_NAMES as $template) {
$data[] = [
'name' => 'Galaxus ' . $template . ' Export',
'typeId' => Defaults::SALES_CHANNEL_TYPE_PRODUCT_COMPARISON,
'languageId' => $store->getLanguageId(),
'customerGroupId' => $store->getCustomerGroupId(),
'currencyId' => $store->getCurrencyId(),
'paymentMethodId' => $store->getPaymentMethodId(),
'shippingMethodId' => $store->getShippingMethodId(),
'countryId' => $store->getCountryId(),
'navigationCategoryId' => $store->getNavigationCategoryId(),
'navigationCategoryIdVersionId' => Defaults::LIVE_VERSION,
'accessKey' => 'WDGE' . strtoupper(str_replace(['+', '/', '='], ['-', '_', ''], base64_encode(random_bytes(16)))),
'productExports' => [
[
'productStreamId' => $this->getProductStream($template, $installContext->getContext())->getId(),
'storefrontSalesChannelId' => $store->getId(),
'interval' => 60,
'salesChannelDomainId' => $domain->getId(),
'currencyId' => $store->getCurrencyId(),
'fileName' => $template . '.csv',
'accessKey' => 'WDGE' . strtoupper(str_replace(['+', '/', '='], ['-', '_', ''], base64_encode(random_bytes(16)))),
'encoding' => ProductExportEntity::ENCODING_UTF8,
'fileFormat' => ProductExportEntity::FILE_FORMAT_CSV,
'headerTemplate' => file_get_contents(__DIR__ . '/Resources/Exports/' . $template . '_Header.txt'),
'bodyTemplate' => file_get_contents(__DIR__ . '/Resources/Exports/' . $template . '_Template.txt'),
'footerTemplate' => '',
'includeVariants' => true,
'generateByCronjob' => true,
]
]
];
}
$salesChannelRepository->create($data, $installContext->getContext());
}
public function uninstall(UninstallContext $uninstallContext): void {
$salesChannelRepository = $this->container->get('sales_channel.repository');
$productStreamRepository = $this->container->get('product_stream.repository');
foreach (static::TEMPLATE_NAMES as $templateName) {
$salesChannel = $salesChannelRepository->search(
(new Criteria())->addFilter(
new EqualsFilter('name', 'Galaxus ' . $templateName . ' Export')
)->addAssociation('domains'),
$uninstallContext->getContext()
)->first();
if ($salesChannel) {
$salesChannelRepository->delete(
array(array('id' => $salesChannel->getId())),
$uninstallContext->getContext()
);
}
$productStream = $this->getProductStream($templateName, $uninstallContext->getContext(), false);
if ($productStream) {
$productStreamRepository->delete(
array(array('id' => $productStream->getId())),
$uninstallContext->getContext()
);
}
}
$this->removeCustomField($uninstallContext);
}
protected function getProductStream($templateName, $context, $create = true)
{
$productStreamRepository = $this->container->get('product_stream.repository');
$entities = $productStreamRepository->search(
(new Criteria())->addFilter(new EqualsFilter('name', 'Galaxus ' . $templateName)),
$context
);
if ($entities->first() === null && $create) {
$productStreamRepository->upsert([[
'name' => 'Galaxus ' . $templateName,
'filters' => [
[
'type' => 'multi',
'operator' => 'OR',
'queries' => [
[
'type' => 'multi',
'operator' => 'AND',
'queries' => [
[
'type' => 'equals',
'field' => 'active',
'value' => '1'
],
[
'type' => 'multi',
'operator' => 'OR',
'queries' => [
[
'type' => 'range',
'field' => 'stock',
'parameters' => [
'gt' => 0
]
],
[
'type' => 'equals',
'field' => 'isCloseout',
'value' => '0'
]
]
]
]
]
]
]
]
]], $context);
$entities = $productStreamRepository->search(
(new Criteria())->addFilter(new EqualsFilter('name', 'Galaxus ' . $templateName)),
$context
);
}
return $entities->first();
}
private function removeCustomField(UninstallContext $uninstallContext)
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$fieldIds = $this->customFieldsExist($uninstallContext->getContext());
if ($fieldIds) {
$customFieldSetRepository->delete(array_values($fieldIds->getData()), $uninstallContext->getContext());
}
}
private function customFieldsExist(Context $context): ?IdSearchResult
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsAnyFilter('name', ['wato_digitec_galaxus_export']));
$ids = $customFieldSetRepository->searchIds($criteria, $context);
return $ids->getTotal() > 0 ? $ids : null;
}
}