Skip to content

Commit 7f3fb1d

Browse files
committed
MAGETWO-87003: Graphql Bundle product implementation
- refactoring
1 parent bc76f59 commit 7f3fb1d

File tree

2 files changed

+53
-38
lines changed

2 files changed

+53
-38
lines changed

app/code/Magento/BundleGraphQl/Model/Resolver/Products/DataProvider/Product/Formatter/BundleOptions.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,28 @@
99
use Magento\Catalog\Model\Product;
1010
use Magento\Bundle\Model\Product\Type as Bundle;
1111
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\FormatterInterface;
12+
use Magento\Framework\Exception\RuntimeException;
13+
use Magento\Framework\GraphQl\Query\EnumLookup;
1214

1315
/**
14-
* Post formatting data to set option for bundle product
16+
* Post formatting data to set main fields and options for bundle product
1517
*/
1618
class BundleOptions implements FormatterInterface
1719
{
20+
/**
21+
* @var EnumLookup
22+
*/
23+
private $enumLookup;
24+
25+
/**
26+
* BundleOptions constructor.
27+
* @param EnumLookup $enumLookup
28+
*/
29+
public function __construct(EnumLookup $enumLookup)
30+
{
31+
$this->enumLookup = $enumLookup;
32+
}
33+
1834
/**
1935
* Add bundle options and options to configurable types
2036
*
@@ -23,10 +39,40 @@ class BundleOptions implements FormatterInterface
2339
public function format(Product $product, array $productData = [])
2440
{
2541
if ($product->getTypeId() === Bundle::TYPE_CODE) {
42+
$productData = $this->formatBundleAttributes($productData);
2643
$extensionAttributes = $product->getExtensionAttributes();
2744
$productData['bundle_product_options'] = $extensionAttributes->getBundleProductOptions();
2845
}
2946

3047
return $productData;
3148
}
49+
50+
/**
51+
* Format bundle specific top level attributes from product
52+
*
53+
* @param array $product
54+
* @return array
55+
* @throws RuntimeException
56+
*/
57+
private function formatBundleAttributes(array $product)
58+
{
59+
if (isset($product['price_view'])) {
60+
$product['price_view']
61+
= $this->enumLookup->getEnumValueFromField('PriceViewEnum', $product['price_view']);
62+
}
63+
if (isset($product['shipment_type'])) {
64+
$product['ship_bundle_items']
65+
= $this->enumLookup->getEnumValueFromField('ShipBundleItemsEnum', $product['shipment_type']);
66+
}
67+
if (isset($product['price_view'])) {
68+
$product['dynamic_price'] = !(bool)$product['price_type'];
69+
}
70+
if (isset($product['sku_type'])) {
71+
$product['dynamic_sku'] = !(bool)$product['sku_type'];
72+
}
73+
if (isset($product['weight_type'])) {
74+
$product['dynamic_weight'] = !(bool)$product['weight_type'];
75+
}
76+
return $product;
77+
}
3278
}

app/code/Magento/BundleGraphQl/Model/Resolver/Products/Query/BundleProductPostProcessor.php

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
use Magento\Framework\Api\SearchCriteriaBuilder;
1212
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product;
1313
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\FormatterInterface;
14-
use Magento\Bundle\Model\Link;
14+
use Magento\Bundle\Api\Data\LinkInterface;
1515
use Magento\Bundle\Model\Option;
1616
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
17-
use Magento\Framework\Exception\RuntimeException;
1817
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1918
use Magento\Framework\GraphQl\Query\EnumLookup;
2019

2120
/**
22-
* Retrieves simple product data for child products, and formats configurable data
21+
* Retrieves simple product data for child products, and formats children data
2322
*/
2423
class BundleProductPostProcessor implements \Magento\Framework\GraphQl\Query\PostFetchProcessorInterface
2524
{
@@ -74,24 +73,23 @@ public function __construct(
7473
*
7574
* @param array $resultData
7675
* @return array
77-
* @throws RuntimeException
7876
*/
7977
public function process(array $resultData)
8078
{
8179
$childrenSkus = [];
8280
$bundleMap = [];
8381
foreach ($resultData as $productKey => $product) {
8482
if (isset($product['type_id']) && $product['type_id'] === Bundle::TYPE_CODE) {
85-
$resultData[$productKey] = $this->formatBundleAttributes($product);
8683
if (isset($product['bundle_product_options'])) {
8784
$bundleMap[$product['sku']] = [];
85+
/** @var Option $option */
8886
foreach ($product['bundle_product_options'] as $optionKey => $option) {
8987
$resultData[$productKey]['items'][$optionKey]
9088
= $option->getData();
91-
/** @var Link $link */
89+
/** @var LinkInterface $link */
9290
foreach ($option['product_links'] as $link) {
93-
$bundleMap[$product['sku']][] = $link['sku'];
94-
$childrenSkus[] = $link['sku'];
91+
$bundleMap[$product['sku']][] = $link->getSku();
92+
$childrenSkus[] = $link->getSku();
9593
$formattedLink = [
9694
'product' => new GraphQlNoSuchEntityException(
9795
__('Bundled product not found')
@@ -157,33 +155,4 @@ private function addChildData(array $childrenProducts, array $resultData, array
157155

158156
return $resultData;
159157
}
160-
161-
/**
162-
* Format bundle specific top level attributes from product
163-
*
164-
* @param array $product
165-
* @return array
166-
* @throws RuntimeException
167-
*/
168-
private function formatBundleAttributes(array $product)
169-
{
170-
if (isset($product['price_view'])) {
171-
$product['price_view']
172-
= $this->enumLookup->getEnumValueFromField('PriceViewEnum', $product['price_view']);
173-
}
174-
if (isset($product['shipment_type'])) {
175-
$product['ship_bundle_items']
176-
= $this->enumLookup->getEnumValueFromField('ShipBundleItemsEnum', $product['shipment_type']);
177-
}
178-
if (isset($product['price_view'])) {
179-
$product['dynamic_price'] = !(bool)$product['price_type'];
180-
}
181-
if (isset($product['sku_type'])) {
182-
$product['dynamic_sku'] = !(bool)$product['sku_type'];
183-
}
184-
if (isset($product['weight_type'])) {
185-
$product['dynamic_weight'] = !(bool)$product['weight_type'];
186-
}
187-
return $product;
188-
}
189158
}

0 commit comments

Comments
 (0)