7
7
8
8
namespace Magento \SalesGraphQl \Model \Resolver ;
9
9
10
+ use Magento \Framework \Api \ExtensibleDataInterface ;
10
11
use Magento \Framework \Exception \LocalizedException ;
11
12
use Magento \Framework \GraphQl \Config \Element \Field ;
12
13
use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
13
14
use Magento \Framework \GraphQl \Query \Resolver \ValueFactory ;
14
15
use Magento \Framework \GraphQl \Query \ResolverInterface ;
15
16
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
17
+ use Magento \Framework \Serialize \Serializer \Json ;
16
18
use Magento \GraphQl \Model \Query \ContextInterface ;
17
- use Magento \SalesGraphQl \ Model \ Resolver \ OrderItem \ DataProvider as OrderItemProvider ;
19
+ use Magento \Sales \ Api \ Data \ LineItemInterface ;
18
20
use Magento \Sales \Api \Data \OrderItemInterface ;
19
- use Magento \Sales \Api \Data \InvoiceItemInterface ;
20
- use Magento \Framework \Serialize \Serializer \Json ;
21
- use Magento \Framework \Api \ExtensibleDataInterface ;
21
+ use Magento \Sales \Model \Order ;
22
+ use Magento \SalesGraphQl \Model \Resolver \OrderItem \DataProvider as OrderItemProvider ;
22
23
23
24
/**
24
25
* Resolve bundle options items for order item
@@ -55,7 +56,6 @@ public function __construct(
55
56
$ this ->valueFactory = $ valueFactory ;
56
57
$ this ->orderItemProvider = $ orderItemProvider ;
57
58
$ this ->serializer = $ serializer ;
58
-
59
59
}
60
60
61
61
/**
@@ -69,37 +69,40 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
69
69
}
70
70
71
71
return $ this ->valueFactory ->create (function () use ($ value ) {
72
- if (!isset ($ value ['model ' ]) || !( $ value [ ' model ' ] instanceof OrderItemInterface) ) {
72
+ if (!isset ($ value ['model ' ])) {
73
73
throw new LocalizedException (__ ('"model" value should be specified ' ));
74
74
}
75
- /** @var ExtensibleDataInterface $item */
76
- $ item = $ value ['model ' ];
77
- return $ this ->getBundleOptions ($ item );
75
+ if ($ value ['model ' ] instanceof OrderItemInterface) {
76
+ /** @var ExtensibleDataInterface $item */
77
+ $ item = $ value ['model ' ];
78
+ return $ this ->getBundleOptions ($ item , null , null );
79
+ }
80
+ if ($ value ['model ' ] instanceof LineItemInterface) {
81
+ /** @var LineItemInterface $item */
82
+ $ item = $ value ['model ' ];
83
+ $ lineItemToOrderItemMap = $ value ['line_item_to_order_item_map ' ];
84
+ $ order = $ value ['order ' ];
85
+ // Have to pass down order and item to map to avoid refetching all data
86
+ return $ this ->getBundleOptions ($ item ->getOrderItem (), $ order , $ lineItemToOrderItemMap );
87
+ }
88
+ return null ;
78
89
});
79
90
}
80
91
81
-
82
92
/**
83
93
* Format bundle options and values from a parent bundle order item
84
94
*
85
95
* @param ExtensibleDataInterface $item
86
96
* @return array
87
97
*/
88
- private function getBundleOptions (ExtensibleDataInterface $ item ): array
89
- {
98
+ private function getBundleOptions (
99
+ OrderItemInterface $ item ,
100
+ Order $ order = null ,
101
+ array $ lineItemToOrderItemMap = null
102
+ ): array {
90
103
$ bundleOptions = [];
91
104
if ($ item ->getProductType () === 'bundle ' ) {
92
- $ options = [];
93
- if ($ item instanceof OrderItemInterface) {
94
- $ options = $ item ->getProductOptions ();
95
- } elseif ($ item instanceof InvoiceItemInterface) {
96
- $ orderItemArray = $ this ->orderItemProvider
97
- ->getOrderItemById ((int )$ item ->getOrderItemId ());
98
- /** @var OrderItemInterface $orderItem */
99
- $ orderItem = $ orderItemArray ['model ' ];
100
- $ options = $ orderItem ->getProductOptions ();
101
- }
102
-
105
+ $ options = $ item ->getProductOptions ();
103
106
if (isset ($ options ['bundle_options ' ])) {
104
107
//loop through options
105
108
foreach ($ options ['bundle_options ' ] as $ bundleOptionKey => $ bundleOption ) {
@@ -108,19 +111,23 @@ private function getBundleOptions(ExtensibleDataInterface $item): array
108
111
base64_encode ($ bundleOption ['option_id ' ]) : null ;
109
112
$ bundleOptions [$ bundleOptionKey ]['items ' ] = [];
110
113
foreach ($ bundleOption ['value ' ] ?? [] as $ bundleOptionValueKey => $ bundleOptionValue ) {
111
- // Find the item assign to the option
114
+ // Find the item assign to the option
112
115
/** @var OrderItemInterface $childrenOrderItem */
113
116
foreach ($ item ->getChildrenItems () ?? [] as $ childrenOrderItem ) {
114
- $ childOrderItemOptions = $ childrenOrderItem ->getProductOptions ();
115
- $ bundleChildAttributes = $ this ->serializer
116
- ->unserialize ($ childOrderItemOptions ['bundle_selection_attributes ' ]);
117
- // Value Id is missing from parent, so we have to match the child to parent option
118
- if (isset ($ bundleChildAttributes ['option_id ' ])
119
- && $ bundleChildAttributes ['option_id ' ] == $ bundleOption ['option_id ' ]) {
120
- $ bundleOptions [$ bundleOptionKey ]['items ' ][] = $ this ->orderItemProvider
121
- ->getOrderItemById ((int )$ childrenOrderItem ->getItemId ());
122
- }
123
- }
117
+ $ childOrderItemOptions = $ childrenOrderItem ->getProductOptions ();
118
+ $ bundleChildAttributes = $ this ->serializer
119
+ ->unserialize ($ childOrderItemOptions ['bundle_selection_attributes ' ]);
120
+ // Value Id is missing from parent, so we have to match the child to parent option
121
+ if (isset ($ bundleChildAttributes ['option_id ' ])
122
+ && $ bundleChildAttributes ['option_id ' ] == $ bundleOption ['option_id ' ]) {
123
+ $ bundleOptions [$ bundleOptionKey ]['item_ids ' ][] = $ childrenOrderItem ->getItemId ();
124
+ if ($ lineItemToOrderItemMap ) {
125
+ $ bundleOptions [$ bundleOptionKey ]['items ' ][] =
126
+ $ lineItemToOrderItemMap [$ childrenOrderItem ->getItemId ()];
127
+ }
128
+ }
129
+ }
130
+ $ bundleOptions [$ bundleOptionKey ]['order ' ] = $ order ;
124
131
}
125
132
}
126
133
}
0 commit comments