<?php declare(strict_types=1);
namespace Proc\ProcOrderSimulate;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
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 ProcOrderSimulate extends Plugin
{
/**
* @var string
*/
private $customFieldSetName = 'custom_shippingCosts';
private $customFieldNames = [
'custom_shippingCosts_freeShippingThreshold',
'custom_shippingCosts_pickupOnly'
];
/**
* @var EntityRepositoryInterface
*/
private $customFieldSetRepository;
private $customFieldRepository;
/**
* @param InstallContext $installContext
*/
public function install(InstallContext $installContext): void
{
$this->initRepositories();
$context = $installContext->getContext();
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', $this->customFieldSetName));
$customFieldset = $this->customFieldSetRepository->search($criteria, $context);
/**
* Create custom field set with custom fields, when this custom field set doesn't exists yet.
*/
if ($customFieldset->getTotal() == 0) {
$this->createCustomFieldSet($context);
}
foreach ($this->customFieldNames as $customFieldName) {
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', $customFieldName));
$customField = $this->customFieldRepository->search($criteria, $context);
/**
* Create custom fields, when this custom field set already exists, but this custom fields don't
*/
if ($customField->getTotal() == 0) {
$this->createCustomField($context);
}
}
}
/**
* @param UninstallContext $uninstallContext
*/
public function uninstall(UninstallContext $uninstallContext): void
{
if ($uninstallContext->keepUserData()) {
parent::uninstall($uninstallContext);
return;
}
$context = $uninstallContext->getContext();
$this->initRepositories();
// dd($uninstallContext);
/**
* Remove custom field set and custom fields
*/
foreach ($this->customFieldNames as $customFieldName) {
$criteria = (new Criteria())->addFilter(new Equalsfilter('name', $customFieldName));
$customField = $this->customFieldRepository->search($criteria, $context);
if ($customField->getTotal() !== 0) {
$this->customFieldRepository->delete([['id' => $customField->first()->getId()]], $context);
}
}
$criteria = (new Criteria())->addFilter(new Equalsfilter('name', $this->customFieldSetName));
$customFieldset = $this->customFieldSetRepository->search($criteria, $context);
if ($customFieldset->getTotal() !== 0) {
$this->customFieldSetRepository->delete([['id' => $customFieldset->first()->getId()]], $context);
}
}
/**
* Initialize necessary repositories
*
* @return void
*/
private function initRepositories()
{
$this->customFieldSetRepository = $this->container->get('custom_field_set.repository');
$this->customFieldRepository = $this->container->get('custom_field.repository');
}
/**
* Create custom field set
*
* @param Context $context
* @return void
*/
private function createCustomFieldSet(Context $context)
{
$customFieldSet = [
'name' => $this->customFieldSetName,
'config' => [
'label' => [
'de-DE' => 'Frachtkosten',
'en-GB' => 'shipping costs'
],
'translated' => true,
],
'active' => true,
'relations' => [
['entityName' => 'order']
],
'customFields' => [
[
'name' => "{$this->customFieldSetName}_freeShippingThreshold",
'type' => CustomFieldTypes::INT,
'config' => [
'label' => [
'de-DE' => 'Frachtfreigrenze',
'en-GB' => 'Free shipping threshold'
],
'customFieldPosition' => 1,
'customFieldType' => 'int',
'componentName' => 'sw-number-field'
]
],
[
'name' => "{$this->customFieldSetName}_pickupOnly",
'type' => CustomFieldTypes::BOOL,
'config' => [
'label' => [
'de-DE' => 'Nur Abholung',
'en-GB' => 'Pickup only'
],
'customFieldPosition' => 1,
'customFieldType' => 'bool',
'componentName' => 'sw-checkbox-field'
]
]
]
];
$this->customFieldSetRepository->upsert([$customFieldSet], $context);
}
/**
* Create custom fields
* @param Context $context
* @return void
*/
private function createCustomField(Context $context)
{
$customField = [
[
'name' => "{$this->customFieldSetName}_freeShippingThreshold",
'type' => CustomFieldTypes::INT,
'config' => [
'label' => [
'de-DE' => 'Frachtfreigrenze',
'en-GB' => 'Free shipping threshold'
],
'customFieldPosition' => 1,
'customFieldType' => 'int',
'componentName' => 'sw-number-field'
]
],
[
'name' => "{$this->customFieldSetName}_pickupOnly",
'type' => CustomFieldTypes::BOOL,
'config' => [
'label' => [
'de-DE' => 'Nur Abholung',
'en-GB' => 'Pickup only'
],
'customFieldPosition' => 1,
'customFieldType' => 'bool',
'componentName' => 'sw-checkbox-field'
]
]
];
$this->customFieldSetRepository->upsert([$customField], $context);
}
}