<?php declare(strict_types=1);
namespace Acris\ProductVoucherCode;
use Doctrine\DBAL\Connection;
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\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\System\Snippet\SnippetEntity;
class AcrisProductVoucherCodeCS extends Plugin
{
const CUSTOM_FIELD_SET_NAME = 'acris_product_voucher_code';
const CUSTOM_FIELD_PRODUCT_VOUCHER_NAME = 'acris_product_voucher_code_buyable_only';
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)), $context)->count() > 0) return;
$customFieldSet->create([[
'name' => self::CUSTOM_FIELD_SET_NAME,
'config' => [
'label' => [
'de-DE' => 'ACRIS Produkte über Gutscheincode einfügen',
'en-GB' => 'ACRIS Insert products via voucher code'
]
],
'relations' => [
[
'entityName' => 'product'
]
],
'customFields' => [
['name' => self::CUSTOM_FIELD_PRODUCT_VOUCHER_NAME, 'type' => CustomFieldTypes::BOOL,
'config' => [
'componentName' => 'sw-field',
'type' => 'switch',
'customFieldType' => 'switch',
'customFieldPosition' => 1,
'label' => [
'de-DE' => 'Das Produkt kann nur über einen Gutschein gekauft werden.',
'en-GB' => 'The product can only be purchased via a voucher.'
],
'helpText' => [
'de-DE' => 'Wenn aktiv, kann das Produkt nur über einene Gutscheincode gekauft werden. Eine Mengenänderung im Warenkorb ist ebenfalls nur über einen Gutschein möglich. Zusätzlich kann beim Produkt auch die Sichtbarkeit im Verkaufskanal eingeschränkt werden, damit der Artikel zwar im Verkaufskanal verfügbar ist, jedoch nicht gefunden werden kann.',
'en-GB' => 'If active, the product can only be purchased using a voucher code. A quantity change in the shopping basket is also only possible via a voucher. In addition, the visibility of the product in the sales channel can be restricted so that the item is available in the sales channel but cannot be found.'
]
]],
],
]], $context);
}
public function uninstall(UninstallContext $context): void
{
if ($context->keepUserData()) {
return;
}
$this->removeCustomFields($context->getContext());
$this->cleanupDatabase();
}
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)), $context)->firstId();
if($id) $customFieldSet->delete([['id' => $id]], $context);
}
private function cleanupDatabase(): void
{
$connection = $this->container->get(Connection::class);
$connection->executeStatement('ALTER TABLE `product` DROP COLUMN `acrisProductVoucherCodes`');
$connection->executeStatement('ALTER TABLE `promotion` DROP COLUMN `acrisProductVoucherCodes`');
$connection->executeStatement('DROP TABLE IF EXISTS acris_product_voucher_code');
}
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_PRODUCT_VOUCHER_NAME)
]));
/** @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);
}
}
}
}