custom/plugins/NetiNextOrderAmountHandler/src/NetiNextOrderAmountHandler.php line 24

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextOrderAmountHandler;
  4. use Doctrine\DBAL\Connection;
  5. use NetInventors\NetiNextOrderAmountHandler\Service\CompilerPass;
  6. use NetInventors\NetiNextOrderAmountHandler\Service\PaymentHandler\FreePayment;
  7. use Shopware\Core\Framework\Plugin;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  13. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  14. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  15. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  16. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  17. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  18. use Shopware\Core\Framework\Uuid\Uuid;
  19. use Symfony\Component\DependencyInjection\ContainerBuilder;
  20. class NetiNextOrderAmountHandler extends Plugin
  21. {
  22.     public const RULE_NAME 'Free | OrderAmountHandler';
  23.     public function install(InstallContext $context): void
  24.     {
  25.         $this->addPaymentMethod($context->getContext());
  26.     }
  27.     public function update(UpdateContext $updateContext): void
  28.     {
  29.         $this->addPaymentMethod($updateContext->getContext());
  30.     }
  31.     public function uninstall(UninstallContext $context): void
  32.     {
  33.         $this->setPaymentMethodIsActive(false$context->getContext());
  34.     }
  35.     public function activate(ActivateContext $context): void
  36.     {
  37.         $this->setPaymentMethodIsActive(true$context->getContext());
  38.         parent::activate($context);
  39.     }
  40.     public function deactivate(DeactivateContext $context): void
  41.     {
  42.         $this->setPaymentMethodIsActive(false$context->getContext());
  43.         parent::deactivate($context);
  44.     }
  45.     private function addPaymentMethod(Context $context): void
  46.     {
  47.         $paymentMethodExists $this->getPaymentMethodId($context);
  48.         if (null === $paymentMethodExists) {
  49.             /** @var PluginIdProvider $pluginIdProvider */
  50.             $pluginIdProvider $this->container->get(PluginIdProvider::class);
  51.             $pluginId         $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
  52.             $examplePaymentData = [
  53.                 'handlerIdentifier' => FreePayment::class,
  54.                 'name'              => 'Free',
  55.                 'description'       => 'Can only be used when the invoice amount is zero.',
  56.                 'pluginId'          => $pluginId,
  57.                 'afterOrderEnabled' => false,
  58.             ];
  59.             /** @var EntityRepositoryInterface $paymentRepository */
  60.             $paymentRepository $this->container->get('payment_method.repository');
  61.             $paymentRepository->create([ $examplePaymentData ], $context);
  62.         }
  63.         $this->createAndAssignPaymentMethodRule($context);
  64.     }
  65.     private function setPaymentMethodIsActive(bool $activeContext $context): void
  66.     {
  67.         /** @var EntityRepositoryInterface $paymentRepository */
  68.         $paymentRepository $this->container->get('payment_method.repository');
  69.         $paymentMethodId   $this->getPaymentMethodId($context);
  70.         if ($paymentMethodId) {
  71.             $paymentMethod = [
  72.                 'id'     => $paymentMethodId,
  73.                 'active' => $active,
  74.             ];
  75.             $paymentRepository->update([ $paymentMethod ], $context);
  76.         }
  77.     }
  78.     private function getPaymentMethodId(Context $context): ?string
  79.     {
  80.         /** @var EntityRepositoryInterface $paymentRepository */
  81.         $paymentRepository $this->container->get('payment_method.repository');
  82.         $criteria          = new Criteria();
  83.         $criteria->addFilter(new EqualsFilter('handlerIdentifier'FreePayment::class));
  84.         return $paymentRepository->searchIds($criteria$context)->firstId();
  85.     }
  86.     private function createAndAssignPaymentMethodRule(Context $context): void
  87.     {
  88.         /** @var EntityRepositoryInterface $ruleRepository */
  89.         $ruleRepository $this->container->get('rule.repository');
  90.         // Check if rule exists
  91.         $criteria = new Criteria();
  92.         $criteria->addFilter(new EqualsFilter('name'self::RULE_NAME));
  93.         $ruleId $ruleRepository->searchIds($criteria$context)->firstId();
  94.         // Create rule
  95.         if (null === $ruleId) {
  96.             $ruleId Uuid::randomHex();
  97.             $ruleRepository->create(
  98.                 [
  99.                     [
  100.                         'id'         => $ruleId,
  101.                         'name'       => self::RULE_NAME,
  102.                         'priority'   => 100,
  103.                         'conditions' => [
  104.                             [
  105.                                 'type'     => 'orContainer',
  106.                                 'children' => [
  107.                                     [
  108.                                         'type'     => 'andContainer',
  109.                                         'children' => [
  110.                                             [
  111.                                                 'type'  => 'cartCartAmount',
  112.                                                 'value' => [ 'amount' => 0'operator' => '<=' ],
  113.                                             ],
  114.                                             [
  115.                                                 'type'  => 'cartLineItemsInCartCount',
  116.                                                 'value' => [ 'count' => 0'operator' => '>' ],
  117.                                             ],
  118.                                         ],
  119.                                     ],
  120.                                 ],
  121.                             ],
  122.                         ],
  123.                     ],
  124.                 ],
  125.                 $context
  126.             );
  127.         }
  128.         // Assign rule
  129.         /** @var EntityRepositoryInterface $paymentRepository */
  130.         $paymentRepository $this->container->get('payment_method.repository');
  131.         $paymentMethodId   $this->getPaymentMethodId($context);
  132.         $paymentMethod = [
  133.             'id'                 => $paymentMethodId,
  134.             'availabilityRuleId' => $ruleId,
  135.         ];
  136.         $paymentRepository->update([ $paymentMethod ], $context);
  137.     }
  138.     public function build(ContainerBuilder $container): void
  139.     {
  140.         parent::build($container);
  141.         $container->addCompilerPass(new CompilerPass());
  142.     }
  143. }