<?php declare(strict_types=1);
namespace fourtwosix\PreOrder;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
class fourtwosixPreOrderProduct extends Plugin
{
public function install(InstallContext $context): void {
//create custom field for pre order flag
$repo = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', 'preorder'));
if(!$repo->search($criteria, $context->getContext())->first()){
$id = Uuid::randomHex();
//create set and custom field
$attributeSet = [
'id' => $id,
'name' => 'preorder',
'active' => true,
'config' => [
'translated' => true,
'label' => [
'en-GB' => "Pre Order Configuration",
'de-DE' => "Konfiguration vorbestellen",
'it-IT' => "Configurazione Pre Ordine"
]
],
'customFields' => [
[
'id' => Uuid::randomHex(),
'name' => 'preorder_enabled',
'type' => 'bool',
'active' => true,
'config' => [
'customFieldType' => 'switch',
'customFieldPosition' => 1,
'label' => [
'en-GB' => "Pre order product enabled",
'de-DE' => "Produkt vorbestellen",
'it-IT' => "Pre ordine prodotto attivo"
],
'helpText' => [
'en-GB' => "Enable product pre order based on release date",
'de-DE' => "Produktvorbestellung basierend auf dem Veröffentlichungsdatum aktivieren",
'it-IT' => "Attiva la possibilità di pre ordinare un prodotto basato sulla data di rilascio"
]
]
]
],
'relations' => [
[
'entityName' => 'product',
]
],
];
$result = $repo->create([$attributeSet], Context::createDefaultContext());
}
}
public function uninstall(UninstallContext $context): void
{
if(!$context->keepUserData()){
$repo = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', 'preorder'));
$result = $repo->search($criteria, Context::createDefaultContext());
if($result != null){
$attributeSet = $result->first();
if($attributeSet != null){
$id = $attributeSet->getId();
if($id != null){
$repo->delete([['id' => $id]], Context::createDefaultContext());
}
}
}
}
}
}