custom/plugins/scha1HidePriceInProducts/src/scha1HidePriceInProducts.php line 22

Open in your IDE?
  1. <?php
  2.   /* 
  3.    * To change this license header, choose License Headers in Project Properties.
  4.    * To change this template file, choose Tools | Templates
  5.    * and open the template in the editor.
  6.    */
  7.   declare(strict_types=1);
  8.   namespace scha1\scha1HidePriceInProducts;
  9.   use Shopware\Core\Framework\Plugin;
  10.   use Shopware\Core\Framework\Plugin\Context\InstallContext;
  11.   use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  12.   use Shopware\Core\System\CustomField\CustomFieldTypes;
  13.   use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14.   use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  15.   use Doctrine\DBAL\Connection;
  16.   /**
  17.    * 
  18.    */
  19.   class scha1HidePriceInProducts extends Plugin {
  20.     /**
  21.      * 
  22.      * @param \scha1\scha1HidePriceInProducts\UninstallContext $uninstallContext
  23.      */
  24.     public function uninstall(UninstallContext $uninstallContext): void {
  25.       
  26.       /**
  27.        * 
  28.        */
  29.       if ($uninstallContext->keepUserData()) {
  30.         parent::uninstall($uninstallContext);
  31.         return;
  32.       }
  33.       
  34.       $customFieldSetRepository $this->container->get('custom_field_set.repository');
  35.       
  36.       //price
  37.       $scha1_showprice = new Criteria();  
  38.       $scha1_showprice->addFilter(new EqualsFilter('name''scha1_showprice'));
  39.       $showprice $customFieldSetRepository->searchIds($scha1_showprice$uninstallContext->getContext());
  40.       if($showprice->getTotal() > && !$uninstallContext->keepUserData()) {
  41.         $data $showprice->getDataOfId($showprice->firstId());
  42.         $customFieldSetRepository->delete([$data], $uninstallContext->getContext()); 
  43.       }
  44.       $this->removeConfiguration();
  45.       
  46.       $scha1_showpricetext = new Criteria();  
  47.       $scha1_showpricetext->addFilter(new EqualsFilter('name''scha1_showpricetext'));
  48.       $showpricetext $customFieldSetRepository->searchIds($scha1_showpricetext$uninstallContext->getContext());
  49.       if($showpricetext->getTotal() > && !$uninstallContext->keepUserData()) {
  50.         $data $showpricetext->getDataOfId($showpricetext->firstId());
  51.         $customFieldSetRepository->delete([$data], $uninstallContext->getContext()); 
  52.       }
  53.       $this->removeConfiguration();
  54.       
  55.       //price set
  56.       $scha1_hidePriceInProducts = new Criteria();  
  57.       $scha1_hidePriceInProducts->addFilter(new EqualsFilter('name''scha1_hidePriceInProducts'));
  58.       $hidePriceInProducts $customFieldSetRepository->searchIds($scha1_hidePriceInProducts$uninstallContext->getContext());
  59.       if($hidePriceInProducts->getTotal() > && !$uninstallContext->keepUserData()) {
  60.         $data $hidePriceInProducts->getDataOfId($hidePriceInProducts->firstId());
  61.         $customFieldSetRepository->delete([$data], $uninstallContext->getContext()); 
  62.       }
  63.       $this->removeConfiguration();
  64.       
  65.       
  66.       //category
  67.       $scha1_showpriceincategory = new Criteria();  
  68.       $scha1_showpriceincategory->addFilter(new EqualsFilter('name''scha1_showpriceincategory'));
  69.       $showpriceincategory $customFieldSetRepository->searchIds($scha1_showpriceincategory$uninstallContext->getContext());
  70.       if($showpriceincategory->getTotal() > && !$uninstallContext->keepUserData()) {
  71.         $data $showpriceincategory->getDataOfId($showpriceincategory->firstId());
  72.         $customFieldSetRepository->delete([$data], $uninstallContext->getContext()); 
  73.       }
  74.       $this->removeConfiguration();
  75.       
  76.       //category set
  77.       $scha1_showPriceInCategory = new Criteria();  
  78.       $scha1_showPriceInCategory->addFilter(new EqualsFilter('name''scha1_showPriceInCategory'));
  79.       $showPriceInCategory $customFieldSetRepository->searchIds($scha1_showPriceInCategory$uninstallContext->getContext());
  80.       if($showPriceInCategory->getTotal() > && !$uninstallContext->keepUserData()) {
  81.         $data $showPriceInCategory->getDataOfId($showPriceInCategory->firstId());
  82.         $customFieldSetRepository->delete([$data], $uninstallContext->getContext()); 
  83.       }
  84.       $this->removeConfiguration();
  85.       
  86.       $this->removeSnippets();
  87.       parent::uninstall($uninstallContext);
  88.     }
  89.     
  90.     
  91.     /**
  92.      * 
  93.      */
  94.     private function removeConfiguration() {
  95.       $connection $this->container->get(Connection::class);
  96.       $qb $connection->createQueryBuilder();
  97.       $qb->delete('system_config''s');
  98.       $qb->where('s.configuration_key LIKE :configuration_key');
  99.       $qb->setParameter('configuration_key''%scha1HidePriceInProducts%');
  100.     }
  101.     
  102.     
  103.     /**
  104.      * 
  105.      */
  106.     private function removeSnippets() {
  107.       $connection $this->container->get(Connection::class);
  108.       $qb $connection->createQueryBuilder();
  109.       $qb->delete('snippet''s');
  110.       $qb->where('s.translation_key LIKE :translation_key');
  111.       $qb->setParameter('translation_key''%scha1_showprice%');
  112.     }
  113.     
  114.     
  115.     /**
  116.      * 
  117.      * @param \scha1\scha1HidePriceInProducts\InstallContext $installContext
  118.      */
  119.     public function install(InstallContext $installContext): void {
  120.       $customFieldSetProduct $this->container->get('custom_field_set.repository');
  121.       $customFieldSetProduct->create([[
  122.         'name' => 'scha1_hidePriceInProducts',
  123.         'config' => ['label' => ['de-DE' => 'Ausblenden der Preise''en-GB' => 'Hide Price in Products'], 'customFieldPosition' => 1],
  124.         'customFields' => [
  125.           ['name' => 'scha1_showprice''type' => CustomFieldTypes::BOOL'config' => ['label' => ['de-DE' => 'Preis ausblenden''en-GB' => 'Hide Price'], 'customFieldPosition' => 1]],
  126.           ['name' => 'scha1_showpricetext''type' => CustomFieldTypes::HTML'config' => ['label' => ['de-DE' => 'Text anstelle des Preises''en-GB' => 'Text instead of the price'], 'componentName' => 'sw-text-editor''customFieldType' => 'textEditor''customFieldPosition' => 2]],
  127. //          ['name' => 'scha1_requestprice', 'type' => CustomFieldTypes::BOOL, 'config' => ['label' => ['de-DE' => 'Preis anfragen', 'en-GB' => 'Request Price']]],
  128.         ],
  129.         'relations' => [['entityName' => 'product']],
  130.         ]] ,$installContext->getContext());
  131.       
  132.       $customFieldSetProduct->create([[
  133.         'name' => 'scha1_showPriceInCategory',
  134.         'config' => ['label' => ['de-DE' => 'Preise für diese Kategorie einblenden''en-GB' => 'show prices in categories'], 'customFieldPosition' => 1],
  135.         'customFields' => [
  136.           ['name' => 'scha1_showpriceincategory''type' => CustomFieldTypes::BOOL'config' => ['label' => ['de-DE' => 'Preise einblenden''en-GB' => 'show prices for this category'], 'customFieldPosition' => 1]],
  137.         ],
  138.         'relations' => [['entityName' => 'category']]
  139.         ]], 
  140.         $installContext->getContext());
  141.       parent::install($installContext);
  142.     }
  143.   }