custom/plugins/fourtwosixProductShippingPrice/src/fourtwosixProductShippingPrice.php line 17

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace fourtwosix\ProductShippingPrice;
  4. use Shopware\Core\Framework\Plugin;
  5. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  6. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  7. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  12. use Shopware\Core\Framework\Uuid\Uuid;
  13. class fourtwosixProductShippingPrice extends Plugin
  14. {
  15.     public function install(InstallContext $context): void
  16.     {
  17.         $repo $this->container->get('custom_field_set.repository');
  18.         $criteria = new Criteria();
  19.         $criteria->addFilter(new EqualsFilter('name''shipping_price'));
  20.         if (!$repo->search($criteria$context->getContext())->first()) {
  21.             $repo->create([
  22.                 [
  23.                     'name' => 'shipping_price',
  24.                     'config' => [
  25.                         'translated' => true,
  26.                         'label' => [
  27.                             'en-GB' => "Shipping Price Configuration",
  28.                             'de-DE' => "Versandpreiskonfiguration"
  29.                         ]
  30.                     ],
  31.                     'relations' => [[
  32.                         'entityName' => 'product'
  33.                     ]],
  34.                     'customFields' => [
  35.                         [
  36.                             'id' => Uuid::randomHex(),
  37.                             'name' => 'shipping_price''type' => 'number''numberType' => 'float',
  38.                             'config' => [
  39.                                 'translated' => true,
  40.                                 'min' => '0',
  41.                                 'label' => [
  42.                                     'en-GB' => "Product Shipping Price",
  43.                                     'de-DE' => "Produktversandpreis"
  44.                                 ],
  45.                                 'helpText' => [
  46.                                     'en-GB' => "Set the price for the shipping of the product",
  47.                                     'de-DE' => "Legen Sie den Preis für den Versand des Produkts fest"
  48.                                 ]
  49.                             ]
  50.                         ]
  51.                     ]
  52.                 ]
  53.             ], $context->getContext());
  54.         }
  55.         
  56.         $this->createActiveSwitch($context);
  57.     }
  58.     public function update(UpdateContext $context): void
  59.     {
  60.         $this->createActiveSwitch($context);
  61.     }
  62.     public function createActiveSwitch($context)
  63.     {
  64.         /* Fetching the custom field set id */
  65.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  66.         $fieldSetCriteria = new Criteria();
  67.         $fieldSetCriteria->addFilter(new EqualsFilter('name''shipping_price'));
  68.         $customFieldSetId $customFieldSetRepository->search($fieldSetCriteria$context->getContext())->first()->getId();
  69.         /*If the new switch custom field doesn't exist, create it */
  70.         $customFiledRepository $this->container->get('custom_field.repository');
  71.         
  72.         $criteria = new Criteria();
  73.         $criteria->addFilter(new EqualsFilter('name''shipping_price_deactivate'));
  74.         if (!$customFiledRepository->search($criteria$context->getContext())->first()) {
  75.             $customFieldSetRepository->update([
  76.                 [
  77.                     'id' => $customFieldSetId,
  78.                     'customFields' => [
  79.                         [
  80.                             'name' => 'shipping_price_deactivate'
  81.                             'type' => 'switch',
  82.                             'config' => [
  83.                                 'translated' => true,
  84.                                 'label' => [
  85.                                     'en-GB' => "Deactivate",
  86.                                     'de-DE' => "Deaktivieren"
  87.                                 ],
  88.                                 "helpText" => [
  89.                                     'en-GB' => "Turn this switch on to deactivate shipping prices for this product",
  90.                                     'de-DE' => "Schalten Sie den Schalter ein, um die Versandpreise für dieses Produkt zu deaktivieren"
  91.                                 ]
  92.                             ]
  93.                         ]
  94.                     ]
  95.                 ]
  96.             ], $context->getContext());
  97.         }
  98.     }
  99.     public function uninstall(UninstallContext $context): void
  100.     {
  101.         if (!$context->keepUserData()) {
  102.             $repo $this->container->get('custom_field_set.repository');
  103.             $result $repo->search((new Criteria())->addFilter(
  104.                     new EqualsAnyFilter('name', [
  105.                         'shipping_price',
  106.                     ])
  107.                 ),
  108.                 Context::createDefaultContext()
  109.             );
  110.             if ($result != null) {
  111.                 foreach ($result as $attribute) {
  112.                     $id $attribute->getId();
  113.                     if (!is_null($id)) {
  114.                         $repo->delete(
  115.                             [['id' => $id]],
  116.                             $context->getContext()
  117.                         );
  118.                     }
  119.                 }
  120.             }
  121.         }
  122.     }
  123. }