10
10
use Magento \Customer \Api \CustomerRepositoryInterface ;
11
11
use Magento \Customer \Api \Data \CustomerInterface ;
12
12
use Magento \Customer \Model \Config \Share ;
13
+ use Magento \Framework \App \ObjectManager ;
13
14
use Magento \Framework \App \RequestInterface ;
14
15
use Magento \Framework \Data \OptionSourceInterface ;
15
16
use Magento \Framework \Escaper ;
17
+ use Magento \Sales \Api \CreditmemoRepositoryInterface ;
18
+ use Magento \Sales \Api \InvoiceRepositoryInterface ;
19
+ use Magento \Sales \Api \OrderRepositoryInterface ;
20
+ use Magento \Sales \Api \ShipmentRepositoryInterface ;
16
21
use Magento \Store \Model \Group ;
17
22
use Magento \Store \Model \System \Store as SystemStore ;
18
23
use Magento \Store \Model \Website ;
@@ -52,25 +57,61 @@ class Options implements OptionSourceInterface
52
57
*/
53
58
private $ options ;
54
59
60
+ /**
61
+ * @var OrderRepositoryInterface
62
+ */
63
+ private $ orderRepository ;
64
+
65
+ /**
66
+ * @var InvoiceRepositoryInterface
67
+ */
68
+ private $ invoiceRepository ;
69
+
70
+ /**
71
+ * @var ShipmentRepositoryInterface
72
+ */
73
+ private $ shipmentRepository ;
74
+
75
+ /**
76
+ * @var CreditmemoRepositoryInterface
77
+ */
78
+ private $ creditmemoRepository ;
79
+
55
80
/**
56
81
* @param CustomerRepositoryInterface $customerRepository
57
82
* @param Escaper $escaper
58
83
* @param RequestInterface $request
59
84
* @param Share $share
60
85
* @param SystemStore $systemStore
86
+ * @param OrderRepositoryInterface|null $orderRepository
87
+ * @param InvoiceRepositoryInterface|null $invoiceRepository
88
+ * @param ShipmentRepositoryInterface|null $shipmentRepository
89
+ * @param CreditmemoRepositoryInterface|null $creditmemoRepository
61
90
*/
62
91
public function __construct (
63
92
CustomerRepositoryInterface $ customerRepository ,
64
93
Escaper $ escaper ,
65
94
RequestInterface $ request ,
66
95
Share $ share ,
67
- SystemStore $ systemStore
96
+ SystemStore $ systemStore ,
97
+ ?OrderRepositoryInterface $ orderRepository = null ,
98
+ ?InvoiceRepositoryInterface $ invoiceRepository = null ,
99
+ ?ShipmentRepositoryInterface $ shipmentRepository = null ,
100
+ ?CreditmemoRepositoryInterface $ creditmemoRepository = null
68
101
) {
69
102
$ this ->customerRepository = $ customerRepository ;
70
103
$ this ->escaper = $ escaper ;
71
104
$ this ->request = $ request ;
72
105
$ this ->share = $ share ;
73
106
$ this ->systemStore = $ systemStore ;
107
+ $ this ->orderRepository = $ orderRepository
108
+ ?? ObjectManager::getInstance ()->get (OrderRepositoryInterface::class);
109
+ $ this ->invoiceRepository = $ invoiceRepository
110
+ ?? ObjectManager::getInstance ()->get (InvoiceRepositoryInterface::class);
111
+ $ this ->shipmentRepository = $ shipmentRepository
112
+ ?? ObjectManager::getInstance ()->get (ShipmentRepositoryInterface::class);
113
+ $ this ->creditmemoRepository = $ creditmemoRepository
114
+ ?? ObjectManager::getInstance ()->get (CreditmemoRepositoryInterface::class);
74
115
}
75
116
76
117
/**
@@ -82,7 +123,7 @@ public function toOptionArray(): array
82
123
return $ this ->options ;
83
124
}
84
125
85
- $ customerId = ( int ) $ this ->request -> getParam ( ' id ' );
126
+ $ customerId = $ this ->getCustomerId ( );
86
127
$ this ->options = $ this ->generateCurrentOptions ($ customerId );
87
128
88
129
return $ this ->options ;
@@ -167,4 +208,30 @@ private function fillStoreGroupOptions(Website $website, CustomerInterface $cust
167
208
168
209
return $ groups ;
169
210
}
211
+
212
+ /**
213
+ * Get Customer id from request param.
214
+ *
215
+ * @return int
216
+ */
217
+ private function getCustomerId (): int
218
+ {
219
+ $ customerId = $ this ->request ->getParam ('id ' );
220
+ if (!$ customerId ) {
221
+ $ orderId = $ this ->request ->getParam ('order_id ' );
222
+ $ shipmentId = $ this ->request ->getParam ('shipment_id ' );
223
+ $ creditmemoId = $ this ->request ->getParam ('creditmemo_id ' );
224
+ $ invoiceId = $ this ->request ->getParam ('invoice_id ' );
225
+ if ($ invoiceId ) {
226
+ $ orderId = $ this ->invoiceRepository ->get ($ invoiceId )->getOrderId ();
227
+ } elseif ($ shipmentId ) {
228
+ $ orderId = $ this ->shipmentRepository ->get ($ shipmentId )->getOrderId ();
229
+ } elseif ($ creditmemoId ) {
230
+ $ orderId = $ this ->creditmemoRepository ->get ($ creditmemoId )->getOrderId ();
231
+ }
232
+ $ customerId = $ this ->orderRepository ->get ($ orderId )->getCustomerId ();
233
+ }
234
+
235
+ return (int )$ customerId ;
236
+ }
170
237
}
0 commit comments