<?php declare(strict_types=1);
namespace Acris\CheckoutMethodPreselect;
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\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\System\Snippet\SnippetEntity;
class AcrisCheckoutMethodPreselectCS extends Plugin
{
/**
* @deprecated CUSTOM_FIELD_SET_NAME_SHIPPING_PAYMENT
*/
const CUSTOM_FIELD_SET_NAME_SHIPPING_PAYMENT = 'acris_checkout_payment_shipping_preselect';
const CUSTOM_FIELD_SET_NAME_SHIPPING = 'acris_checkout_shipping_preselect';
const CUSTOM_FIELD_SET_NAME_PAYMENT = 'acris_checkout_payment_preselect';
const CUSTOM_FIELD_PAYMENT_PRESELECT_PRIORITY = 'acris_checkout_payment_preselect_priority';
const CUSTOM_FIELD_SHIPPING_PRESELECT_PRIORITY = 'acris_checkout_shipping_preselect_priority';
public function uninstall(UninstallContext $context): void
{
parent::uninstall($context);
if ($context->keepUserData()) {
return;
}
$this->removeCustomFields($context->getContext(), [self::CUSTOM_FIELD_SET_NAME_SHIPPING_PAYMENT, self::CUSTOM_FIELD_SET_NAME_SHIPPING, self::CUSTOM_FIELD_SET_NAME_PAYMENT]);
}
public function install(InstallContext $context): void
{
$this->removeCustomFields($context->getContext(), [self::CUSTOM_FIELD_SET_NAME_SHIPPING_PAYMENT]);
$this->addCustomFields($context->getContext());
}
public function update(UpdateContext $updateContext): void
{
if((version_compare($updateContext->getCurrentPluginVersion(), '3.0.0', '<') && version_compare($updateContext->getUpdatePluginVersion(), '3.0.0', '>='))) {
$this->removeCustomFields($updateContext->getContext(), [self::CUSTOM_FIELD_SET_NAME_SHIPPING_PAYMENT, self::CUSTOM_FIELD_SET_NAME_SHIPPING, self::CUSTOM_FIELD_SET_NAME_PAYMENT]);
$this->addCustomFields($updateContext->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)), $context)->count() == 0) {
$customFieldSet->create([[
'name' => self::CUSTOM_FIELD_SET_NAME_SHIPPING,
'config' => [
'label' => [
'en-GB' => 'Shipping preselect in checkout process',
'de-DE' => 'Vorauswahl der Versandart im Bestellprozess'
]
],
'relations' => [
[
'entityName' => 'shipping_method'
]
],
'customFields' => [
['name' => self::CUSTOM_FIELD_SHIPPING_PRESELECT_PRIORITY, 'type' => CustomFieldTypes::INT,
'config' => [
'componentName' => 'sw-field',
'type' => 'number',
'numberType' => 'int',
'customFieldType' => 'number',
'customFieldPosition' => 1,
'label' => [
'en-GB' => 'Shpping preselection priority',
'de-DE' => 'Versandart Vorauswahl Priorität'
],
'helpText' => [
'de-DE' => 'Je höher die Priorität, desto weiter Vorne wird die Versandart bei der automatischen Auswahl gereiht.',
'en-GB' => 'The higher the priority, the further forward the shipping method is ranked in the automatic selection.'
]
]]
],
]], $context);
}
if($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter('name', self::CUSTOM_FIELD_SET_NAME_PAYMENT)), $context)->count() == 0) {
$customFieldSet->create([[
'name' => self::CUSTOM_FIELD_SET_NAME_SHIPPING,
'config' => [
'label' => [
'en-GB' => 'Payment preselect in checkout process',
'de-DE' => 'Vorauswahl der Zahlungsart im Bestellprozess'
]
],
'relations' => [
[
'entityName' => 'payment_method'
]
],
'customFields' => [
['name' => self::CUSTOM_FIELD_PAYMENT_PRESELECT_PRIORITY, 'type' => CustomFieldTypes::INT,
'config' => [
'componentName' => 'sw-field',
'type' => 'number',
'numberType' => 'int',
'customFieldType' => 'number',
'customFieldPosition' => 1,
'label' => [
'en-GB' => 'Payment preselection priority',
'de-DE' => 'Zahlungsart Vorauswahl Priorität'
],
'helpText' => [
'de-DE' => 'Je höher die Priorität, desto weiter Vorne wird die Zahlungsart bei der automatischen Auswahl gereiht.',
'en-GB' => 'The higher the priority, the further forward the payment method is ranked in the automatic selection.'
]
]]
],
]], $context);
}
}
private function removeCustomFields(Context $context, array $setNames): void
{
/* Check for snippets if they exist for custom fields */
$this->checkForExistingCustomFieldSnippets($context);
$customFieldSet = $this->container->get('custom_field_set.repository');
foreach ($setNames as $setName) {
$id = $customFieldSet->searchIds((new Criteria())->addFilter(new EqualsFilter('name', $setName)), $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.' . self::CUSTOM_FIELD_PAYMENT_PRESELECT_PRIORITY),
new EqualsFilter('translationKey', 'customFields.' . self::CUSTOM_FIELD_SHIPPING_PRESELECT_PRIORITY)
]));
/** @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);
}
}
}
}