<?php declare(strict_types=1);
namespace Wexo\Checkout;
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\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\CustomFieldEntity;
/**
* Class WexoCheckout
* @package Wexo\Checkout
*/
class WexoCheckout extends Plugin
{
const CHECKOUT_ADDITIONAL_FIELDS = 'checkout_additional_fields';
/**
* @param InstallContext $installContext
*/
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->createCustomFieldSet($installContext->getContext());
$this->createSaleschannelCustomFieldSet($installContext->getContext());
}
/**
* @param UpdateContext $updateContext
*/
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext);
$this->createCustomFieldSet($updateContext->getContext());
$this->createSaleschannelCustomFieldSet($updateContext->getContext());
}
/**
* @param UninstallContext $uninstallContext
*/
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if (!$uninstallContext->keepUserData()) {
$context = $uninstallContext->getContext();
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('name', self::CHECKOUT_ADDITIONAL_FIELDS)
);
/** @var CustomFieldEntity $first */
if ($first = $customFieldSetRepository->search($criteria, $context)->first()) {
$customFieldSetRepository->delete(
[
[
'id' => $first->getId()
]
],
$context
);
}
}
}
/**
* @param Context $context
* @return EntitySearchResult
*/
private function getSalesChannels(Context $context)
{
$salesChannelRepository = $this->container->get('sales_channel.repository');
return $this->container->get('sales_channel.repository')->search(new Criteria(), $context);
}
/**
* @param Context $context
* @return bool
*/
private function createSalesChannelCustomFieldSet(Context $context)
{
$salesChannels = $this->getSalesChannels($context);
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
if (!$salesChannels->getTotal()) {
return false;
}
foreach ($salesChannels as $channel) {
$channelId = $channel->getId();
if ($this->fieldExists(
self::CHECKOUT_ADDITIONAL_FIELDS . '_' . $channelId,
$customFieldSetRepository,
$context
)
) {
continue;
}
$customFieldSetRepository->create([
[
'name' => self::CHECKOUT_ADDITIONAL_FIELDS . '_' . $channelId,
'config' => [
'label' => [
'da-DK' => 'Yderligere checkout felter - ' . $channel->getName(),
'de-DE' => 'Zusätzliche Checkout-Felder - ' . $channel->getName(),
'en-GB' => 'Additional checkout fields - ' . $channel->getName()
]
],
'relations' => [[
'entityName' => 'order'
]]
]
], $context);
}
return true;
}
/**
* @param Context $context
*/
private function createCustomFieldSet(Context $context)
{
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
if ($this->fieldExists(self::CHECKOUT_ADDITIONAL_FIELDS, $customFieldSetRepository, $context)) {
return;
}
$customFieldSetRepository->create([
[
'name' => self::CHECKOUT_ADDITIONAL_FIELDS,
'config' => [
'label' => [
'da-DK' => 'Yderligere checkout felter',
'de-DE' => 'Zusätzliche Checkout-Felder',
'en-GB' => 'Additional checkout fields'
]
],
'relations' => [[
'entityName' => 'order'
]]
]
], $context);
}
/**
* @param $name
* @param $repository
* @return bool
*/
private function fieldExists($name, $repository, $context)
{
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('name', $name)
);
if (!$repository->searchIds($criteria, $context)->firstId()) {
return false;
}
return true;
}
}