|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CatalogRule\Model\ResourceModel; |
| 9 | + |
| 10 | +use Magento\Framework\App\ResourceConnection; |
| 11 | +use Magento\Framework\EntityManager\MetadataPool; |
| 12 | +use Magento\CatalogRule\Api\Data\RuleInterface; |
| 13 | + |
| 14 | +class GetAppliedCatalogRules |
| 15 | +{ |
| 16 | + /** |
| 17 | + * GetAppliedCatalogRules constructor |
| 18 | + * |
| 19 | + * @param ResourceConnection $resourceConnection |
| 20 | + * @param MetadataPool $metadataPool |
| 21 | + */ |
| 22 | + public function __construct( |
| 23 | + private readonly ResourceConnection $resourceConnection, |
| 24 | + private readonly MetadataPool $metadataPool |
| 25 | + ) { |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Get applied catalog rules |
| 30 | + * |
| 31 | + * @param int $productId |
| 32 | + * @param int $websiteId |
| 33 | + * @return array |
| 34 | + */ |
| 35 | + public function execute(int $productId, int $websiteId): array |
| 36 | + { |
| 37 | + $connection = $this->resourceConnection->getConnection(); |
| 38 | + $linkField = $this->metadataPool->getMetadata(RuleInterface::class)->getLinkField(); |
| 39 | + |
| 40 | + return $connection->fetchAll( |
| 41 | + $connection->select() |
| 42 | + ->from( |
| 43 | + ['cr' => $this->resourceConnection->getTableName('catalogrule')], |
| 44 | + ['name'] |
| 45 | + ) |
| 46 | + ->join( |
| 47 | + ['crp' => $this->resourceConnection->getTableName('catalogrule_product')], |
| 48 | + 'crp.rule_id = cr.rule_id', |
| 49 | + ) |
| 50 | + ->join( |
| 51 | + ['crw' => $this->resourceConnection->getTableName('catalogrule_website')], |
| 52 | + "cr.rule_id = crw.$linkField", |
| 53 | + ) |
| 54 | + ->reset('columns') |
| 55 | + ->columns(['name']) |
| 56 | + ->distinct(true) |
| 57 | + ->where('cr.is_active = ?', 1) |
| 58 | + ->where('crp.product_id = ?', $productId) |
| 59 | + ->where('crw.website_id = ?', $websiteId) |
| 60 | + ) ?? []; |
| 61 | + } |
| 62 | +} |
0 commit comments