-
Notifications
You must be signed in to change notification settings - Fork 22
/
BundleProductOptions.php
172 lines (152 loc) · 5.3 KB
/
BundleProductOptions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\BundleProductDataExporter\Model\Provider\Product;
use Magento\BundleProductDataExporter\Model\Query\BundleProductOptionsQuery;
use Magento\BundleProductDataExporter\Model\Query\BundleProductOptionValuesQuery;
use Magento\CatalogDataExporter\Model\Provider\Product\OptionProviderInterface;
use Magento\DataExporter\Exception\UnableRetrieveData;
use Magento\Framework\App\ResourceConnection;
use Magento\DataExporter\Model\Logging\CommerceDataExportLoggerInterface as LoggerInterface;
use Throwable;
/**
* Class which provides bundle product options and option values
*/
class BundleProductOptions implements OptionProviderInterface
{
/**
* @var ResourceConnection
*/
private $resourceConnection;
/**
* @var BundleProductOptionsQuery
*/
private $bundleProductOptionsQuery;
/**
* @var BundleProductOptionValuesQuery
*/
private $bundleProductOptionValuesQuery;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var BundleItemOptionUid
*/
private $bundleItemOptionUid;
/**
* @param ResourceConnection $resourceConnection
* @param BundleProductOptionsQuery $bundleProductOptionsQuery
* @param BundleProductOptionValuesQuery $bundleProductOptionValuesQuery
* @param BundleItemOptionUid $bundleItemOptionUid
* @param LoggerInterface $logger
*/
public function __construct(
ResourceConnection $resourceConnection,
BundleProductOptionsQuery $bundleProductOptionsQuery,
BundleProductOptionValuesQuery $bundleProductOptionValuesQuery,
BundleItemOptionUid $bundleItemOptionUid,
LoggerInterface $logger
) {
$this->resourceConnection = $resourceConnection;
$this->bundleProductOptionsQuery = $bundleProductOptionsQuery;
$this->bundleProductOptionValuesQuery = $bundleProductOptionValuesQuery;
$this->logger = $logger;
$this->bundleItemOptionUid = $bundleItemOptionUid;
}
/**
* @inheritDoc
*/
public function get(array $values) : array
{
$output = [];
$queryArguments = [];
$productIds = [];
foreach ($values as $value) {
$productIds[$value['productId']] = $value['productId'];
$queryArguments[$value['storeViewCode']][$value['productId']] = $value['productId'];
}
try {
foreach ($queryArguments as $storeViewCode => $productIds) {
$optionValues = $this->getOptionValues($productIds, $storeViewCode);
$cursor = $this->resourceConnection->getConnection()->query(
$this->bundleProductOptionsQuery->getQuery($productIds, $storeViewCode)
);
while ($row = $cursor->fetch()) {
$output[] = $this->formatBundleOptionsRow($row, $optionValues);
}
}
} catch (Throwable $exception) {
$this->logger->error($exception->getMessage(), ['exception' => $exception]);
throw new UnableRetrieveData('Unable to retrieve bundle product options data');
}
return $output;
}
/**
* Get bundle products option values
*
* @param int[] $productIds
* @param string $storeViewCode
*
* @return array
*/
private function getOptionValues(array $productIds, string $storeViewCode) : array
{
$output = [];
$cursor = $this->resourceConnection->getConnection()->query(
$this->bundleProductOptionValuesQuery->getQuery($productIds, $storeViewCode)
);
while ($row = $cursor->fetch()) {
$output[$row['parent_id']][$row['option_id']][] = $this->formatBundleValuesRow($row);
}
return $output;
}
/**
* Format bundle item options row
*
* @param array $row
* @param array $optionValues
*
* @return array
*/
private function formatBundleOptionsRow(array $row, array $optionValues) : array
{
return [
'productId' => $row['product_id'],
'storeViewCode' => $row['store_view_code'],
'optionsV2' => [
'type' => BundleItemOptionUid::OPTION_TYPE,
'id' => $row['option_id'],
'renderType' => $row['render_type'],
'required' => $row['required'],
'label' => $row['label'],
'sortOrder' => $row['sort_order'],
'values' => $optionValues[$row['parent_id']][$row['option_id']] ?? [],
],
];
}
/**
* Format bundle item values row
*
* @param array $row
*
* @return array
*/
private function formatBundleValuesRow(array $row) : array
{
return [
'id' => $this->bundleItemOptionUid->resolve($row['option_id'], $row['id'], $row['qty']),
'sku' => $row['sku'],
'label' => $row['label'],
'qty' => $row['qty'],
'priceType' => $row['price_type'] ? 'percent' : 'fixed',
'price' => $row['price_value'],
'sortOrder' => $row['sort_order'],
'isDefault' => $row['default'],
'qtyMutability' => (bool)$row['qty_mutability'],
];
}
}