<?php
declare(strict_types=1);
namespace fourtwosix\ProductShippingPrice;
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\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\Uuid\Uuid;
class fourtwosixProductShippingPrice extends Plugin
{
public function install(InstallContext $context): void
{
$repo = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', 'shipping_price'));
if (!$repo->search($criteria, $context->getContext())->first()) {
$repo->create([
[
'name' => 'shipping_price',
'config' => [
'translated' => true,
'label' => [
'en-GB' => "Shipping Price Configuration",
'de-DE' => "Versandpreiskonfiguration"
]
],
'relations' => [[
'entityName' => 'product'
]],
'customFields' => [
[
'id' => Uuid::randomHex(),
'name' => 'shipping_price', 'type' => 'number', 'numberType' => 'float',
'config' => [
'translated' => true,
'min' => '0',
'label' => [
'en-GB' => "Product Shipping Price",
'de-DE' => "Produktversandpreis"
],
'helpText' => [
'en-GB' => "Set the price for the shipping of the product",
'de-DE' => "Legen Sie den Preis für den Versand des Produkts fest"
]
]
]
]
]
], $context->getContext());
}
$this->createActiveSwitch($context);
}
public function update(UpdateContext $context): void
{
$this->createActiveSwitch($context);
}
public function createActiveSwitch($context)
{
/* Fetching the custom field set id */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$fieldSetCriteria = new Criteria();
$fieldSetCriteria->addFilter(new EqualsFilter('name', 'shipping_price'));
$customFieldSetId = $customFieldSetRepository->search($fieldSetCriteria, $context->getContext())->first()->getId();
/*If the new switch custom field doesn't exist, create it */
$customFiledRepository = $this->container->get('custom_field.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', 'shipping_price_deactivate'));
if (!$customFiledRepository->search($criteria, $context->getContext())->first()) {
$customFieldSetRepository->update([
[
'id' => $customFieldSetId,
'customFields' => [
[
'name' => 'shipping_price_deactivate',
'type' => 'switch',
'config' => [
'translated' => true,
'label' => [
'en-GB' => "Deactivate",
'de-DE' => "Deaktivieren"
],
"helpText" => [
'en-GB' => "Turn this switch on to deactivate shipping prices for this product",
'de-DE' => "Schalten Sie den Schalter ein, um die Versandpreise für dieses Produkt zu deaktivieren"
]
]
]
]
]
], $context->getContext());
}
}
public function uninstall(UninstallContext $context): void
{
if (!$context->keepUserData()) {
$repo = $this->container->get('custom_field_set.repository');
$result = $repo->search((new Criteria())->addFilter(
new EqualsAnyFilter('name', [
'shipping_price',
])
),
Context::createDefaultContext()
);
if ($result != null) {
foreach ($result as $attribute) {
$id = $attribute->getId();
if (!is_null($id)) {
$repo->delete(
[['id' => $id]],
$context->getContext()
);
}
}
}
}
}
}