Skip to content

Commit 97b17ed

Browse files
author
DEMO
committed
10439 add Iterfaces to get Parent Product Ids and Websites Map
1 parent 47f0020 commit 97b17ed

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
namespace Magefan\Community\Api;
8+
9+
/**
10+
* Return parent ids by child ids
11+
*
12+
* @api
13+
* @since 2.1.19
14+
*/
15+
interface GetParentProductIdsInterface
16+
{
17+
/**
18+
* @api
19+
* @param array $productIds
20+
* @return array
21+
*/
22+
public function execute(array $productIds) : array;
23+
}

Api/GetWebsitesMapInterface.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
namespace Magefan\Community\Api;
8+
9+
/**
10+
* Return Websites Map
11+
*
12+
* @api
13+
* @since 2.1.19
14+
*/
15+
interface GetWebsitesMapInterface
16+
{
17+
/**
18+
* @api
19+
* @return array
20+
*/
21+
public function execute() : array;
22+
}

Model/GetParentProductIds.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
}

Model/GetWebsitesMap.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\GetWebsitesMapInterface;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
14+
15+
class GetWebsitesMap implements GetWebsitesMapInterface
16+
{
17+
/**
18+
* @var StoreManagerInterface
19+
*/
20+
private $storeManager;
21+
22+
/**
23+
* @var
24+
*/
25+
private $websitesMap;
26+
27+
/**
28+
* GetWebsitesMap constructor.
29+
* @param StoreManagerInterface $storeManager
30+
*/
31+
public function __construct(
32+
StoreManagerInterface $storeManager
33+
) {
34+
$this->storeManager = $storeManager;
35+
}
36+
37+
/**
38+
* @return array
39+
*/
40+
public function execute(): array
41+
{
42+
if ($this->websitesMap === null) {
43+
$this->websitesMap = [];
44+
$websites = $this->storeManager->getWebsites();
45+
foreach ($websites as $website) {
46+
// Continue if website has no store to be able to create catalog rule for website without store
47+
if ($website->getDefaultStore() === null) {
48+
continue;
49+
}
50+
$this->websitesMap[$website->getId()] = $website->getDefaultStore()->getId();
51+
}
52+
}
53+
54+
return $this->websitesMap;
55+
}
56+
}

etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<preference for="Magefan\Community\Api\GetModuleVersionInterface" type="Magefan\Community\Model\GetModuleVersion" />
1010
<preference for="Magefan\Community\Api\GetCategoryByProductInterface" type="Magefan\Community\Model\GetCategoryByProduct"/>
11+
12+
<preference for="Magefan\Community\Api\GetParentProductIdsInterface" type="Magefan\Community\Model\GetParentProductIds"/>
13+
<preference for="Magefan\Community\Api\GetWebsitesMapInterface" type="Magefan\Community\Model\GetWebsitesMap"/>
1114
</config>

0 commit comments

Comments
 (0)