custom/static-plugins/SamsonCustomer/src/SamsonCustomer.php line 36

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Samson;
  3. if (file_exists(dirname(__DIR__) . '/vendor/autoload.php')) {
  4.     require_once dirname(__DIR__) . '/vendor/autoload.php';
  5. }
  6. /***
  7.  *
  8.  * This file is part of the "SAMSON Shop" project.
  9.  *
  10.  * For the full copyright and license information, please read the
  11.  * LICENSE.txt file that was distributed with this source code.
  12.  *
  13.  *  (c) 2020
  14.  *
  15.  ***/
  16. use Samson\MessageQueue\Middleware\SentryMiddlewareCompilerPass;
  17. use Shopware\Core\Framework\Plugin;
  18. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  19. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  20. use Shopware\Storefront\Framework\ThemeInterface;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  22. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  23. use Symfony\Component\DependencyInjection\ContainerBuilder;
  24. /**
  25.  * Bootstrap file for SAMSON plugin.
  26.  *
  27.  * Class SamsonCustomer
  28.  * @package Samson
  29.  * @author Artur Seitz <artur.seitz@dkd.de>
  30.  */
  31. class SamsonCustomer extends Plugin implements ThemeInterface
  32. {
  33.     public function build(ContainerBuilder $container): void
  34.     {
  35.         parent::build($container);
  36.         $container->addCompilerPass(new SentryMiddlewareCompilerPass());
  37.     }
  38.     /** {@inheritDoc} */
  39.     public function update(UpdateContext $updateContext): void
  40.     {
  41.         parent::update($updateContext);
  42.         $this->updateStructure();
  43.         $currentVersion $updateContext->getCurrentPluginVersion();
  44.         if (version_compare($currentVersion'1.1.3') < 0) {
  45.             $this->createSalesCategory($updateContext);
  46.         }
  47.     }
  48.     private function createSalesCategory(UpdateContext $updateContext):void {
  49.         $context $updateContext->getContext();
  50.         $criteria = (new Criteria())
  51.             ->setLimit(1)
  52.             ->addFilter(new EqualsFilter('parentId'null));
  53.         $categoryRepository $this->container->get('category.repository');
  54.         $productStreamRepository $this->container->get('product_stream.repository');
  55.         $productStreamFilterRepository $this->container->get('product_stream_filter.repository');
  56.         $main $categoryRepository->search($criteria$context);
  57.         if ($main->count() == 0) {
  58.             return;
  59.         }
  60.         $main $main->first()->getId();
  61.         $productStreamId $productStreamRepository->create([[
  62.             'name' => 'Products for Sale',
  63.         ]], $context)->getEvents()->first()->getIds()[0];
  64.         $orFilterId $productStreamFilterRepository->create([[
  65.             'type' => 'multi',
  66.             'operator' => 'OR',
  67.             'productStreamId' => $productStreamId
  68.         ]], $context)->getEvents()->first()->getIds()[0];
  69.         $andFilterId $productStreamFilterRepository->create([[
  70.             'type' => 'multi',
  71.             'operator' => 'AND',
  72.             'productStreamId' => $productStreamId,
  73.             'parentId' => $orFilterId
  74.         ]], $context)->getEvents()->first()->getIds()[0];
  75.         $productStreamFilterRepository->create([[
  76.             'type' => 'equals',
  77.             'field' => 'customFields.samson_product_is_on_sale',
  78.             'value' => '1',
  79.             'productStreamId' => $productStreamId,
  80.             'parentId' => $andFilterId
  81.         ]], $context);
  82.         $categoryRepository->create([[
  83.             'name' => 'Sales',
  84.             'type' => 'page',
  85.             'productAssignmentType' => 'product_stream',
  86.             'parentId' => $main,
  87.             'productStreamId' => $productStreamId
  88.         ]], $context);
  89.     }
  90.     /** {@inheritDoc} */
  91.     public function activate(ActivateContext $activateContext): void
  92.     {
  93.         parent::activate($activateContext);
  94.         $this->updateStructure();
  95.     }
  96.     /**
  97.      * Creates new custom fields and property groups.
  98.      */
  99.     protected function updateStructure(): void
  100.     {
  101.         /* @todo */
  102.     }
  103. }