|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\SalesRule\Model\ResourceModel; |
| 9 | + |
| 10 | +use Exception; |
| 11 | +use Magento\Framework\App\ResourceConnection; |
| 12 | +use Magento\Framework\EntityManager\MetadataPool; |
| 13 | +use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; |
| 14 | +use Magento\SalesRule\Api\Data\RuleInterface; |
| 15 | + |
| 16 | +class GetAppliedCartRules |
| 17 | +{ |
| 18 | + /** |
| 19 | + * GetAppliedCartRules constructor |
| 20 | + * |
| 21 | + * @param ResourceConnection $resourceConnection |
| 22 | + * @param MetadataPool $metadataPool |
| 23 | + */ |
| 24 | + public function __construct( |
| 25 | + private readonly ResourceConnection $resourceConnection, |
| 26 | + private readonly MetadataPool $metadataPool |
| 27 | + ) { |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Get rule names from specified rule ids |
| 32 | + * |
| 33 | + * @param string $ruleIds |
| 34 | + * @param ContextInterface $context |
| 35 | + * @return array |
| 36 | + * @throws Exception |
| 37 | + */ |
| 38 | + public function execute(string $ruleIds, ContextInterface $context): array |
| 39 | + { |
| 40 | + $connection = $this->resourceConnection->getConnection(); |
| 41 | + $linkField = $this->metadataPool->getMetadata(RuleInterface::class)->getLinkField(); |
| 42 | + |
| 43 | + return $connection->fetchAll( |
| 44 | + $connection->select() |
| 45 | + ->from(['sr' => $this->resourceConnection->getTableName('salesrule')]) |
| 46 | + ->reset('columns') |
| 47 | + ->columns(['name']) |
| 48 | + ->join( |
| 49 | + ['srw' => $this->resourceConnection->getTableName('salesrule_website')], |
| 50 | + "sr.rule_id = srw.$linkField", |
| 51 | + [] |
| 52 | + ) |
| 53 | + ->where('sr.is_active = ?', 1) |
| 54 | + ->where('sr.rule_id IN (?)', explode(',', $ruleIds)) |
| 55 | + ->where( |
| 56 | + 'srw.website_id = ?', |
| 57 | + (int)$context->getExtensionAttributes()->getStore()->getWebsiteId() |
| 58 | + ) |
| 59 | + ) ?? []; |
| 60 | + } |
| 61 | +} |
0 commit comments