|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magefan ([email protected]). All rights reserved. |
| 4 | + * Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement). |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magefan\Community\Model; |
| 10 | + |
| 11 | +use Magefan\Community\Api\GetParentProductIdsInterface; |
| 12 | +use Magento\Framework\App\ResourceConnection; |
| 13 | + |
| 14 | + |
| 15 | +class GetParentProductIds implements GetParentProductIdsInterface |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var ResourceConnection |
| 19 | + */ |
| 20 | + protected $resourceConnection; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var \Magento\Framework\DB\Adapter\AdapterInterface |
| 24 | + */ |
| 25 | + private $connection; |
| 26 | + |
| 27 | + |
| 28 | + /** |
| 29 | + * GetParentProductIds constructor. |
| 30 | + * @param ResourceConnection $resourceConnection |
| 31 | + */ |
| 32 | + public function __construct( |
| 33 | + ResourceConnection $resourceConnection |
| 34 | + ) { |
| 35 | + $this->resourceConnection = $resourceConnection; |
| 36 | + $this->connection = $resourceConnection->getConnection(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @param array $productIds |
| 41 | + * @return array |
| 42 | + */ |
| 43 | + public function execute(array $productIds): array |
| 44 | + { |
| 45 | + $parentProductIds = []; |
| 46 | + |
| 47 | + /* Fix for configurable, bundle, grouped */ |
| 48 | + if ($productIds) { |
| 49 | + $productTable = $this->resourceConnection->getTableName('catalog_product_entity'); |
| 50 | + $entityIdColumn = $this->connection->tableColumnExists($productTable, 'row_id') ? 'row_id' : 'entity_id'; |
| 51 | + |
| 52 | + $select = $this->connection->select() |
| 53 | + ->from( |
| 54 | + ['main_table' => $this->resourceConnection->getTableName('catalog_product_relation')], |
| 55 | + [] |
| 56 | + )->join( |
| 57 | + ['e' => $productTable], |
| 58 | + 'e.' . $entityIdColumn . ' = main_table.parent_id', |
| 59 | + ['e.' .$entityIdColumn] |
| 60 | + ) |
| 61 | + ->where('main_table.child_id IN (?)', $productIds) |
| 62 | + ->where('e.entity_id IS NOT NULL'); |
| 63 | + |
| 64 | + foreach ($this->connection->fetchAll($select) as $product) { |
| 65 | + $parentProductIds[$product[$entityIdColumn]] = $product[$entityIdColumn]; |
| 66 | + } |
| 67 | + } |
| 68 | + /* End fix */ |
| 69 | + |
| 70 | + return $parentProductIds; |
| 71 | + } |
| 72 | +} |
0 commit comments