<?php declare(strict_types=1);
namespace Acris\ShippingMethodText;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\System\Snippet\SnippetEntity;
class AcrisShippingMethodTextCS extends Plugin
{
const CUSTOM_FIELD_SET_NAME_SHIPPING_COSTS_DISPLAY = 'acris_shipping_costs_text_display';
public function install(InstallContext $context): void
{
$this->addCustomFields($context->getContext());
}
private function addCustomFields(Context $context): void
{
/* Check for snippets if they exist for custom fields */
$this->checkForExistingCustomFieldSnippets($context);
$customFieldSet = $this->container->get('custom_field_set.repository');
if ($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter('name', self::CUSTOM_FIELD_SET_NAME_SHIPPING_COSTS_DISPLAY)), $context)->count() == 0) {
$customFieldSet->create([[
'name' => self::CUSTOM_FIELD_SET_NAME_SHIPPING_COSTS_DISPLAY,
'config' => [
'label' => [
'en-GB' => 'Shipping cost display settings',
'de-DE' => 'Einstellungen für die Anzeige der Versandkosten'
]
],
'relations' => [
[
'entityName' => 'shipping_method'
]
],
'customFields' => [
['name' => 'acris_shipping_costs_text_type', 'type' => CustomFieldTypes::SELECT,
'config' => [
'componentName' => 'sw-single-select',
'type' => CustomFieldTypes::SELECT,
'customFieldType' => CustomFieldTypes::SELECT,
'customFieldPosition' => 10,
'label' => [
'en-GB' => 'Display shipping costs',
'de-DE' => 'Anzeige Versandkosten'
],
'options' => [
[
'label' => [
'en-GB' => 'Show shipping costs (default)',
'de-DE' => 'Versandkosten anzeigen (Standard)'
],
'value' => 'default'
],
[
'label' => [
'en-GB' => 'Show text instead of shipping costs',
'de-DE' => 'Text anzeigen statt Versandkosten'
],
'value' => 'show_text'
]
]
]
],
['name' => 'acris_shipping_costs_text', 'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => CustomFieldTypes::TEXT,
'customFieldType' => CustomFieldTypes::TEXT,
'customFieldPosition' => 20,
'label' => [
'en-GB' => 'Shipping costs text',
'de-DE' => 'Versandkosten Text'
],
'placeholder' => [
'de-DE' => 'Lorem ipsum',
'en-GB' => 'Lorem ipsum'
],
'helpText' => [
'de-DE' => 'Wird im Warenkorb / Checkout angezeigt, wenn bei der Anzeige Versandkosten Text anzeigen statt Versandkosten ausgewählt wurde.',
'en-GB' => 'Is displayed in the shopping cart / checkout, if Show text instead of shipping costs is selected in the Display shipping costs settings.'
]
]
]
]
]], $context);
};
}
public function uninstall(UninstallContext $context): void
{
parent::uninstall($context);
if ($context->keepUserData()) {
return;
}
$this->removeCustomFields($context->getContext());
}
private function removeCustomFields(Context $context): void
{
/* Check for snippets if they exist for custom fields */
$this->checkForExistingCustomFieldSnippets($context);
$customFieldSet = $this->container->get('custom_field_set.repository');
$id = $customFieldSet->searchIds((new Criteria())->addFilter(new EqualsFilter('name', self::CUSTOM_FIELD_SET_NAME_SHIPPING_COSTS_DISPLAY)), $context)->firstId();
if($id) $customFieldSet->delete([['id' => $id]], $context);
}
private function checkForExistingCustomFieldSnippets(Context $context)
{
/** @var EntityRepositoryInterface $snippetRepository */
$snippetRepository = $this->container->get('snippet.repository');
$criteria = new Criteria();
$criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
new EqualsFilter('translationKey', 'customFields.' . 'acris_shipping_costs_text_type'),
new EqualsFilter('translationKey', 'customFields.' . 'acris_shipping_costs_text'),
new EqualsFilter('translationKey', 'customFields.' . 'acris_shipping_costs_text_instructions')
]));
/** @var EntitySearchResult $searchResult */
$searchResult = $snippetRepository->search($criteria, $context);
if ($searchResult->count() > 0) {
$snippetIds = [];
/** @var SnippetEntity $snippet */
foreach ($searchResult->getEntities()->getElements() as $snippet) {
$snippetIds[] = [
'id' => $snippet->getId()
];
}
if (!empty($snippetIds)) {
$snippetRepository->delete($snippetIds, $context);
}
}
}
}