Skip to content

Commit e989a1e

Browse files
committed
MC-20636: Order Details : Order Details by Order Number
- refactor to move bundle resolver and match bundle values to children real values
1 parent 296788e commit e989a1e

File tree

4 files changed

+119
-51
lines changed

4 files changed

+119
-51
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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\SalesGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
13+
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use Magento\GraphQl\Model\Query\ContextInterface;
17+
use Magento\SalesGraphQl\Model\Resolver\OrderItem\DataProvider as OrderItemProvider;
18+
use Magento\Sales\Api\Data\OrderItemInterface;
19+
use Magento\Framework\Serialize\Serializer\Json;
20+
21+
/**
22+
* Resolve bundle options items for order item
23+
*/
24+
class BundleOptions implements ResolverInterface
25+
{
26+
/**
27+
* Serializer
28+
*
29+
* @var Json
30+
*/
31+
private $serializer;
32+
33+
/**
34+
* @var ValueFactory
35+
*/
36+
private $valueFactory;
37+
38+
/**
39+
* @var OrderItemProvider
40+
*/
41+
private $orderItemProvider;
42+
43+
/**
44+
* @param ValueFactory $valueFactory
45+
* @param OrderItemProvider $orderItemProvider
46+
* @param Json $serializer
47+
*/
48+
public function __construct(
49+
ValueFactory $valueFactory,
50+
OrderItemProvider $orderItemProvider,
51+
Json $serializer
52+
) {
53+
$this->valueFactory = $valueFactory;
54+
$this->orderItemProvider = $orderItemProvider;
55+
$this->serializer = $serializer;
56+
57+
}
58+
59+
/**
60+
* @inheritDoc
61+
*/
62+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
63+
{
64+
/** @var ContextInterface $context */
65+
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
66+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
67+
}
68+
69+
return $this->valueFactory->create(function () use ($value) {
70+
if (!isset($value['model']) || !($value['model'] instanceof OrderItemInterface)) {
71+
throw new LocalizedException(__('"model" value should be specified'));
72+
}
73+
/** @var OrderItemInterface $orderItem */
74+
$orderItem = $value['model'];
75+
return $this->getBundleOptions($orderItem);
76+
});
77+
}
78+
79+
80+
/**
81+
* Format bundle options and values from a parent bundle order item
82+
*
83+
* @param OrderItemInterface $item
84+
* @return array
85+
*/
86+
private function getBundleOptions(OrderItemInterface $item): array
87+
{
88+
$bundleOptions = [];
89+
if ($item->getProductType() === 'bundle') {
90+
$options = $item->getProductOptions();
91+
if (isset($options['bundle_options'])) {
92+
//loop through options
93+
foreach ($options['bundle_options'] as $bundleOptionKey => $bundleOption) {
94+
$bundleOptions[$bundleOptionKey]['label'] = $bundleOption['label'] ?? '';
95+
$bundleOptions[$bundleOptionKey]['id'] = isset($bundleOption['option_id']) ?
96+
base64_encode($bundleOption['option_id']) : null;
97+
$bundleOptions[$bundleOptionKey]['items'] = [];
98+
foreach ($bundleOption['value'] ?? [] as $bundleOptionValueKey => $bundleOptionValue) {
99+
// Find the item assign to the option
100+
/** @var OrderItemInterface $childrenOrderItem */
101+
foreach ($item->getChildrenItems() ?? [] as $childrenOrderItem) {
102+
$childOrderItemOptions = $childrenOrderItem->getProductOptions();
103+
$bundleChildAttributes = $this->serializer
104+
->unserialize($childOrderItemOptions['bundle_selection_attributes']);
105+
// Value Id is missing from parent, so we have to match the child to parent option
106+
if (isset($bundleChildAttributes['option_id'])
107+
&& $bundleChildAttributes['option_id'] == $bundleOption['option_id']) {
108+
$bundleOptions[$bundleOptionKey]['items'][] = $this->orderItemProvider
109+
->getOrderItemById((int)$childrenOrderItem->getItemId());
110+
}
111+
}
112+
}
113+
}
114+
}
115+
}
116+
return $bundleOptions;
117+
}
118+
}

app/code/Magento/SalesGraphQl/Model/Resolver/OrderItem/DataProvider.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ private function fetch()
129129
/** @var OrderInterface $associatedOrder */
130130
$associatedOrder = $orderList[$orderItem->getOrderId()];
131131
$itemOptions = $this->optionsProcessor->getItemOptions($orderItem);
132-
$bundleOptions = $orderItem->getProductType() === 'bundle' ?
133-
$this->optionsProcessor->getBundleOptions($orderItem) : [];
134132

135133
$this->orderItemList[$orderItem->getItemId()] = [
136134
'id' => base64_encode($orderItem->getItemId()),
@@ -152,7 +150,6 @@ private function fetch()
152150
'quantity_invoiced' => $orderItem->getQtyInvoiced(),
153151
'quantity_canceled' => $orderItem->getQtyCanceled(),
154152
'quantity_returned' => $orderItem->getQtyReturned(),
155-
'bundle_options' => $bundleOptions,
156153
];
157154

158155
}

app/code/Magento/SalesGraphQl/Model/Resolver/OrderItem/OptionsProcessor.php

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,13 @@
77

88
namespace Magento\SalesGraphQl\Model\Resolver\OrderItem;
99

10-
use Magento\Framework\Serialize\Serializer\Json;
1110
use Magento\Sales\Api\Data\OrderItemInterface;
1211

1312
/**
1413
* Process order item options to format for GraphQl output
1514
*/
1615
class OptionsProcessor
1716
{
18-
/**
19-
* Serializer
20-
*
21-
* @var Json
22-
*/
23-
private $serializer;
24-
25-
/**
26-
* @param Json $serializer
27-
*/
28-
public function __construct(Json $serializer)
29-
{
30-
$this->serializer = $serializer;
31-
}
32-
3317
/**
3418
* Get Order item options.
3519
*
@@ -98,35 +82,4 @@ private function processAttributesInfo(array $attributesInfo): array
9882
}
9983
return ['selected_options' => $selectedOptions, 'entered_options' => []];
10084
}
101-
102-
/**
103-
*
104-
* @param \Magento\Sales\Api\Data\OrderItemInterface $item
105-
* @return array
106-
*/
107-
public function getBundleOptions(\Magento\Sales\Api\Data\OrderItemInterface $item): array
108-
{
109-
$bundleOptions = [];
110-
if ($item->getProductType() === 'bundle') {
111-
if ($item instanceof \Magento\Sales\Model\Order\Item) {
112-
$options = $item->getProductOptions();
113-
} else {
114-
$options = $item->getOrderItem()->getProductOptions();
115-
}
116-
if (isset($options['bundle_options'])) {
117-
//$bundleOptions = $this->serializer->unserialize($options['bundle_options']);
118-
foreach ($options['bundle_options'] as $bundleOptionKey => $bundleOption) {
119-
$bundleOptions[$bundleOptionKey]['items'] = $bundleOption['value'] ?? [];
120-
$bundleOptions[$bundleOptionKey]['label'] = $bundleOption['label'];
121-
foreach ($bundleOptions[$bundleOptionKey]['items'] as $bundleOptionValueKey => $bundleOptionValue) {
122-
$bundleOptions[$bundleOptionKey]['items'][$bundleOptionValueKey]['product_sku'] = $bundleOptionValue['title'];
123-
$bundleOptions[$bundleOptionKey]['items'][$bundleOptionValueKey]['product_name'] = $bundleOptionValue['title'];
124-
$bundleOptions[$bundleOptionKey]['items'][$bundleOptionValueKey]['quantity_ordered'] = $bundleOptionValue['qty'];
125-
$bundleOptions[$bundleOptionKey]['items'][$bundleOptionValueKey]['id'] = base64_encode((string)$bundleOptionValueKey);
126-
}
127-
}
128-
}
129-
}
130-
return $bundleOptions;
131-
}
13285
}

app/code/Magento/SalesGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ type OrderItem implements OrderItemInterface {
8282
}
8383

8484
type BundleOrderItem implements OrderItemInterface {
85-
bundle_options: [SelectedBundleOptionItems] @doc(description: "A list of bundle options that are assigned to the bundle product")
85+
bundle_options: [SelectedBundleOptionItems] @doc(description: "A list of bundle options that are assigned to the bundle product") @resolver(class: "Magento\\SalesGraphQl\\Model\\Resolver\\BundleOptions")
8686
}
8787

8888
type SelectedBundleOptionItems {

0 commit comments

Comments
 (0)