Skip to content

Commit 738df37

Browse files
Merge pull request #9 from magento-commerce/imported-magento-commerce-data-export-8
[Imported] 27 product variants
2 parents 60445ea + c8d3f0d commit 738df37

File tree

60 files changed

+2193
-92
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2193
-92
lines changed

app/code/Magento/CatalogDataExporter/composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"magento/module-directory": "*",
2727
"magento/module-data-exporter": "*",
2828
"magento/module-customer": "*",
29-
"magento/module-downloadable": "*"
29+
"magento/module-downloadable": "*",
30+
"magento/module-indexer": "*"
3031
}
3132
}

app/code/Magento/CatalogDataExporter/etc/et_schema.xml

-12
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
provider="Magento\CatalogDataExporter\Model\Provider\ProductMetadata">
2020
<using field="id" />
2121
</field>
22-
<field name="variants" type="ProductVariant" repeated="true"
23-
provider="Magento\CatalogDataExporter\Model\Provider\ProductVariants">
24-
<using field="product_id" />
25-
</field>
2622
</record>
2723
<!-- TODO: delete deprecated "ImageDeprecated" type. use "Image" instead -->
2824
<record name="ImageDeprecated">
@@ -251,14 +247,6 @@
251247
<field name="qty" type="Float"/>
252248
<field name="price" type="Float"/>
253249
</record>
254-
<record name="ProductVariant">
255-
<!-- variant identifier following the convention :prefix:/:parentId:/:entityId: -->
256-
<field name="id" type="ID" />
257-
<!-- parent_id:option_id/optionValue.uid -->
258-
<field name="option_values" type="String" repeated="true" />
259-
<!-- link to product id that represents variant, for custom option must be null-->
260-
<field name="product_id" type="String"/>
261-
</record>
262250
<record name="Price">
263251
<field name="regularPrice" type="Float"/>
264252
<field name="finalPrice" type="Float"/>

app/code/Magento/CatalogExport/Event/Data/Entity.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class Entity
1515
{
1616
/**
17-
* @var int
17+
* @var string
1818
*/
1919
private $entityId;
2020

@@ -26,21 +26,21 @@ class Entity
2626
/**
2727
* Get entity id.
2828
*
29-
* @return int
29+
* @return string
3030
*/
31-
public function getEntityId(): int
31+
public function getEntityId(): string
3232
{
3333
return $this->entityId;
3434
}
3535

3636
/**
3737
* Set entity id.
3838
*
39-
* @param int $entityId
39+
* @param string $entityId
4040
*
4141
* @return void
4242
*/
43-
public function setEntityId(int $entityId): void
43+
public function setEntityId(string $entityId): void
4444
{
4545
$this->entityId = $entityId;
4646
}

app/code/Magento/CatalogExport/Model/ChangedEntitiesMessageBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function __construct(
6565
*
6666
* @return \Magento\CatalogExport\Event\Data\ChangedEntities
6767
*/
68-
public function build(string $eventType, array $entities, string $scope): ChangedEntities
68+
public function build(string $eventType, array $entities, ?string $scope = null): ChangedEntities
6969
{
7070
$meta = $this->metaFactory->create();
7171
$meta->setScope($scope);

app/code/Magento/CatalogExport/Model/Indexer/EntityIndexerCallback.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,23 @@ public function __construct(
103103
public function execute(array $entityData, array $deleteIds) : void
104104
{
105105
foreach ($this->getDeleteEntitiesData($deleteIds) as $storeCode => $entities) {
106+
$scope = $storeCode ?: null;
106107
foreach (\array_chunk($entities, $this->batchSize) as $chunk) {
107108
$this->publishMessage(
108109
$this->deletedEventType,
109110
$chunk,
110-
$storeCode
111+
$scope
111112
);
112113
}
113114
}
114115

115116
foreach ($this->getUpdateEntitiesData($entityData) as $storeCode => $entities) {
117+
$scope = $storeCode ?: null;
116118
foreach (\array_chunk($entities, $this->batchSize) as $chunk) {
117119
$this->publishMessage(
118120
$this->updatedEventType,
119121
$chunk,
120-
$storeCode
122+
$scope
121123
);
122124
}
123125
}
@@ -135,8 +137,8 @@ private function getDeleteEntitiesData(array $deleteIds): array
135137
$deleted = [];
136138
$feed = $this->feedPool->getFeed($this->feedIndexMetadata->getFeedName());
137139
foreach ($feed->getDeletedByIds($deleteIds) as $entity) {
138-
$deleted[$entity['storeViewCode']][] = [
139-
'entity_id' => (int)$entity[$this->feedIndexMetadata->getFeedIdentity()],
140+
$deleted[$entity['storeViewCode'] ?? null][] = [
141+
'entity_id' => (string)$entity[$this->feedIndexMetadata->getFeedIdentity()],
140142
];
141143
}
142144

@@ -154,8 +156,8 @@ private function getUpdateEntitiesData(array $entityData): array
154156
{
155157
$entitiesArray = [];
156158
foreach ($entityData as $entity) {
157-
$entitiesArray[$entity['storeViewCode']][] = [
158-
'entity_id' => (int)$entity[$this->feedIndexMetadata->getFeedIdentity()],
159+
$entitiesArray[$entity['storeViewCode'] ?? null][] = [
160+
'entity_id' => (string)$entity[$this->feedIndexMetadata->getFeedIdentity()],
159161
'attributes' => $entity['attributes'] ?? [],
160162
];
161163
}
@@ -168,11 +170,11 @@ private function getUpdateEntitiesData(array $entityData): array
168170
*
169171
* @param string $eventType
170172
* @param array $entities
171-
* @param string $scope
173+
* @param string|null $scope
172174
*
173175
* @return void
174176
*/
175-
private function publishMessage(string $eventType, array $entities, string $scope): void
177+
private function publishMessage(string $eventType, array $entities, ?string $scope): void
176178
{
177179
$message = $this->messageBuilder->build($eventType, $entities, $scope);
178180

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogExport\Model;
9+
10+
use Magento\CatalogExportApi\Api\Data\ProductVariant;
11+
use Magento\CatalogExportApi\Api\Data\ProductVariantFactory;
12+
use Magento\CatalogExportApi\Api\ProductVariantRepositoryInterface;
13+
use Magento\DataExporter\Model\FeedPool;
14+
use Magento\Framework\App\DeploymentConfig;
15+
use Psr\Log\LoggerInterface;
16+
17+
/**
18+
* Product variant entity repository
19+
*/
20+
class ProductVariantRepository implements ProductVariantRepositoryInterface
21+
{
22+
/**
23+
* Constant value for setting max items in response
24+
*/
25+
private const MAX_ITEMS_IN_RESPONSE = 250;
26+
27+
/**
28+
* @var ProductVariantFactory
29+
*/
30+
private $productVariantFactory;
31+
32+
/**
33+
* @var DtoMapper
34+
*/
35+
private $dtoMapper;
36+
37+
/**
38+
* @var DeploymentConfig
39+
*/
40+
private $deploymentConfig;
41+
42+
/**
43+
* @var LoggerInterface
44+
*/
45+
private $logger;
46+
47+
/**
48+
* @var FeedPool
49+
*/
50+
private $feedPool;
51+
52+
/**
53+
* @param FeedPool $feedPool
54+
* @param ProductVariantFactory $productVariantFactory
55+
* @param DtoMapper $dtoMapper
56+
* @param DeploymentConfig $deploymentConfig
57+
* @param LoggerInterface $logger
58+
*/
59+
public function __construct(
60+
FeedPool $feedPool,
61+
ProductVariantFactory $productVariantFactory,
62+
DtoMapper $dtoMapper,
63+
DeploymentConfig $deploymentConfig,
64+
LoggerInterface $logger
65+
) {
66+
$this->feedPool = $feedPool;
67+
$this->dtoMapper = $dtoMapper;
68+
$this->productVariantFactory = $productVariantFactory;
69+
$this->deploymentConfig = $deploymentConfig;
70+
$this->logger = $logger;
71+
}
72+
73+
/**
74+
* @inheritdoc
75+
*/
76+
public function get(array $ids): array
77+
{
78+
if (count($ids) > $this->getMaxItemsInResponse()) {
79+
throw new \InvalidArgumentException(
80+
'Max items in the response can\'t exceed '
81+
. $this->getMaxItemsInResponse()
82+
. '.'
83+
);
84+
}
85+
86+
$productsVariants = [];
87+
$feedData = $this->feedPool->getFeed('variants')->getFeedByIds($ids);
88+
if (empty($feedData['feed'])) {
89+
$this->logger->error(
90+
\sprintf('Cannot find products variants data in catalog feed with ids "%s"', \implode(',', $ids))
91+
);
92+
return $productsVariants;
93+
}
94+
95+
foreach ($feedData['feed'] as $feedItem) {
96+
$productVariant = $this->productVariantFactory->create();
97+
$feedItem = $this->cleanUpNullValues($feedItem);
98+
$this->dtoMapper->populateWithArray(
99+
$productVariant,
100+
$feedItem,
101+
ProductVariant::class
102+
);
103+
$productsVariants[] = $productVariant;
104+
}
105+
return $productsVariants;
106+
}
107+
108+
/**
109+
* @inheritdoc
110+
*/
111+
public function getByProductIds(array $productIds): array
112+
{
113+
if (count($productIds) > $this->getMaxItemsInResponse()) {
114+
throw new \InvalidArgumentException(
115+
'Max items in the response can\'t exceed '
116+
. $this->getMaxItemsInResponse()
117+
. '.'
118+
);
119+
}
120+
121+
$productsVariants = [];
122+
$feedData = $this->feedPool->getFeed('variants')->getFeedByProductIds($productIds);
123+
if (empty($feedData['feed'])) {
124+
$this->logger->error(
125+
\sprintf(
126+
'Cannot find products variants data in catalog feed with product ids "%s"',
127+
\implode(',', $productIds)
128+
)
129+
);
130+
return $productsVariants;
131+
}
132+
133+
foreach ($feedData['feed'] as $feedItem) {
134+
$productVariant = $this->productVariantFactory->create();
135+
$feedItem = $this->cleanUpNullValues($feedItem);
136+
$this->dtoMapper->populateWithArray(
137+
$productVariant,
138+
$feedItem,
139+
ProductVariant::class
140+
);
141+
$productsVariants[] = $productVariant;
142+
}
143+
return $productsVariants;
144+
}
145+
146+
/**
147+
* Get max items in response
148+
*
149+
* @return int
150+
*/
151+
private function getMaxItemsInResponse(): int
152+
{
153+
try {
154+
$maxItemsInResponse = (int)$this->deploymentConfig->get('catalog_export/max_items_in_response');
155+
} catch (\Exception $e) {
156+
$this->logger->error(
157+
\sprintf('Cannot retrieve catalog export max items in response for product variants. ' . $e)
158+
);
159+
return self::MAX_ITEMS_IN_RESPONSE;
160+
}
161+
return $maxItemsInResponse ?: self::MAX_ITEMS_IN_RESPONSE;
162+
}
163+
164+
/**
165+
* Unset null values in provided array recursively
166+
*
167+
* @param array $array
168+
* @return array
169+
*/
170+
private function cleanUpNullValues(array $array): array
171+
{
172+
$result = [];
173+
foreach ($array as $key => $value) {
174+
if ($value === null || $value === '') {
175+
continue;
176+
}
177+
178+
$result[$key] = is_array($value) ? $this->cleanUpNullValues($value) : $value;
179+
}
180+
return $result;
181+
}
182+
}

0 commit comments

Comments
 (0)