custom/plugins/EasyCreditRatenkauf/src/Service/FlexpriceService.php line 45

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) NETZKOLLEKTIV GmbH <kontakt@netzkollektiv.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Netzkollektiv\EasyCredit\Service;
  8. use Shopware\Core\Framework\Rule\Collector\RuleConditionRegistry;
  9. use Shopware\Core\Framework\Rule\Container\AndRule;
  10. use Shopware\Core\Checkout\Cart\Rule\CartRuleScope;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Shopware\Core\Checkout\Cart\CartService;
  17. use Shopware\Core\Checkout\Cart\Cart;
  18. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  19. use Shopware\Core\Framework\Uuid\Uuid;
  20. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  21. use Netzkollektiv\EasyCredit\Service\RuleEvaluator;
  22. use Netzkollektiv\EasyCredit\Service\WebshopInfoService;
  23. class FlexpriceService {
  24.     private EntityRepository $ruleRepository;
  25.     private RuleEvaluator $ruleEvaluator;
  26.     private WebshopInfoService $webshopInfoService;
  27.     public function __construct(
  28.         EntityRepository $ruleRepository,
  29.         RuleEvaluator $ruleEvaluator,
  30.         WebshopInfoService $webshopInfoService
  31.     ) {
  32.         $this->ruleRepository $ruleRepository;
  33.         $this->ruleEvaluator $ruleEvaluator;
  34.         $this->webshopInfoService $webshopInfoService;
  35.     }
  36.     public function isEnabled(SalesChannelContext $salesChannelContext) {
  37.         try {
  38.             return $this->webshopInfoService
  39.                 ->getWebshopInfo($salesChannelContext->getSalesChannel()->getId())
  40.                 ->getFlexprice() === true;
  41.         } catch (\Throwable $e) {
  42.             return false;
  43.         }
  44.     }
  45.     protected function getFlexpriceRule(Context $context)
  46.     {
  47.         $criteria = new Criteria();
  48.         $criteria->addFilter(new ContainsFilter('moduleTypes.types''easycredit-flexprice'));
  49.         $rule $this->ruleRepository->search($criteria$context)->first();
  50.         return $rule;
  51.     }
  52.     public function shouldDisableFlexprice (SalesChannelContext $salesChannelContextCart $cart) {
  53.         if (!$this->isEnabled($salesChannelContext)) {
  54.             return false;
  55.         }
  56.         $rule $this->getFlexpriceRule($salesChannelContext->getContext());
  57.         if (!$rule) {
  58.             return false;
  59.         }
  60.         return $this->ruleEvaluator->evaluateRule(
  61.             $rule,
  62.             $cart,
  63.             $salesChannelContext
  64.         );
  65.     }
  66.     public function shouldDisableFlexpriceForProduct(SalesChannelContext $salesChannelContextSalesChannelProductEntity $product$quantity 1) {
  67.         if (!$this->isEnabled($salesChannelContext)) {
  68.             return false;
  69.         }
  70.         $cart $this->ruleEvaluator->getCartForProduct($product$salesChannelContext$quantity);
  71.         return $this->shouldDisableFlexprice($salesChannelContext$cart);
  72.     }
  73. }