<?php declare(strict_types=1);
namespace K3n\AdditionalOrderFields;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
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;
class K3nAdditionalOrderFields extends Plugin
{
public function install(InstallContext $installContext): void
{
$this->createFields($installContext);
}
public function uninstall(UninstallContext $uninstallContext): void
{
if (!$uninstallContext->keepUserData()) {
$this->removeFields($uninstallContext);
}
}
private function createFields(InstallContext $installContext)
{
/**
* @var $customFieldSetRepository EntityRepositoryInterface
*/
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetRepository->upsert($this->getCustomFields(), $installContext->getContext());
}
private function getCustomFields()
{
return [
[
'id' => '0617768191c57ed143fcd50eda6e7017',
'name' => 'k3n_order_fields',
'active' => true,
'config' => [
'label' => [
'en-GB' => 'Additional order fields (k3n)',
'de-DE' => 'Zusätzliche Felder für Bestellungen (k3n)',
],
],
'customFields' => [
[
'id' => '9c5a6b742cd7da92f5c5dcffb7660249',
'name' => 'k3n_order_sales_consultant',
'active' => true,
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'en-GB' => 'Sales consultant',
'de-DE' => 'Verkaufsberater',
],
'customFieldType' => CustomFieldTypes::TEXT,
'customFieldPosition' => 1
],
],
],
'relations' => [
[
'id' => '292063964ad82b24a3d39ef3e8778d1b',
'entityName' => 'order',
],
],
],
];
}
private function removeFields(UninstallContext $uninstallContext)
{
/**
* @var $customFieldSetRepository EntityRepositoryInterface
*/
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetRepository->delete([['id' => '0617768191c57ed143fcd50eda6e7017']], $uninstallContext->getContext());
}
}