31
31
use Magento \Store \Model \StoreManagerInterface ;
32
32
use Meta \BusinessExtension \Helper \GraphAPIAdapter ;
33
33
use Meta \BusinessExtension \Model \System \Config as SystemConfig ;
34
+ use Meta \Sales \Plugin \ShippingData ;
34
35
use Meta \Sales \Plugin \ShippingMethodTypes ;
35
36
use Meta \Sales \Helper \ShippingHelper ;
36
37
@@ -75,6 +76,11 @@ class OrderMapper
75
76
*/
76
77
private OrderItemMapper $ orderItemMapper ;
77
78
79
+ /**
80
+ * @var ShippingData
81
+ */
82
+ private ShippingData $ shippingData ;
83
+
78
84
/**
79
85
* @var ShippingHelper
80
86
*/
@@ -88,6 +94,7 @@ class OrderMapper
88
94
* @param OrderPaymentInterfaceFactory $paymentFactory
89
95
* @param OrderAddressInterfaceFactory $orderAddressFactory
90
96
* @param OrderItemMapper $orderItemMapper
97
+ * @param ShippingData $shippingData
91
98
* @param ShippingHelper $shippingHelper
92
99
*/
93
100
public function __construct (
@@ -98,6 +105,7 @@ public function __construct(
98
105
OrderPaymentInterfaceFactory $ paymentFactory ,
99
106
OrderAddressInterfaceFactory $ orderAddressFactory ,
100
107
OrderItemMapper $ orderItemMapper ,
108
+ ShippingData $ shippingData ,
101
109
ShippingHelper $ shippingHelper
102
110
) {
103
111
$ this ->storeManager = $ storeManager ;
@@ -107,6 +115,7 @@ public function __construct(
107
115
$ this ->paymentFactory = $ paymentFactory ;
108
116
$ this ->orderAddressFactory = $ orderAddressFactory ;
109
117
$ this ->orderItemMapper = $ orderItemMapper ;
118
+ $ this ->shippingData = $ shippingData ;
110
119
$ this ->shippingHelper = $ shippingHelper ;
111
120
}
112
121
@@ -130,8 +139,8 @@ public function map(array $data, int $storeId): Order
130
139
->setAccessToken ($ accessToken );
131
140
132
141
$ channel = ucfirst ($ data ['channel ' ]);
133
- $ shippingOptionName = $ data ['selected_shipping_option ' ]['name ' ];
134
- $ shippingReferenceId = $ data ['selected_shipping_option ' ]['reference_id ' ];
142
+ $ metaShippingOptionName = $ data ['selected_shipping_option ' ]['name ' ];
143
+ $ magentoShippingReferenceID = $ data ['selected_shipping_option ' ]['reference_id ' ];
135
144
$ billingAddress = $ this ->getOrderBillingAddress ($ data );
136
145
$ shippingAddress = clone $ billingAddress ;
137
146
$ shippingAddress
@@ -158,13 +167,16 @@ public function map(array $data, int $storeId): Order
158
167
159
168
$ this ->applyTotalsToOrder ($ order , $ data , $ storeId );
160
169
161
- $ shippingMethod = $ this ->getShippingMethod ($ shippingOptionName , $ shippingReferenceId , $ storeId );
162
- $ shippingDescription = $ this ->getShippingMethodLabel ($ shippingOptionName , $ storeId );
170
+ $ shippingMethod = $ this ->getShippingMethod ($ metaShippingOptionName , $ magentoShippingReferenceID , $ storeId );
171
+ $ shippingDescription = $ this ->getShippingDescription ($ metaShippingOptionName , $ shippingMethod , $ storeId );
172
+ // This should never happen, as it means Meta has passed a shipping method with no equivalent in Magento.
173
+ // @todo strictly handle this edge case by canceling the entire Meta order if this happens.
174
+ $ fallbackShippingDescription = $ metaShippingOptionName . " - {$ shippingMethod }" ;
163
175
164
176
$ order ->setStoreId ($ storeId )
165
177
// @todo have to set shipping method like this
166
178
->setShippingMethod ($ shippingMethod )
167
- ->setShippingDescription ($ shippingDescription ?? $ shippingOptionName . " / { $ shippingMethod }" )
179
+ ->setShippingDescription ($ shippingDescription ?? $ fallbackShippingDescription )
168
180
->setPayment ($ payment );
169
181
170
182
// @todo implement paging and tax for order items
@@ -191,10 +203,7 @@ public function map(array $data, int $storeId): Order
191
203
*/
192
204
private function getShippingMethod (string $ shippingOptionName , string $ shippingReferenceId , int $ storeId ): ?string
193
205
{
194
- $ static_shipping_options = [ShippingMethodTypes::FREE_SHIPPING ,
195
- ShippingMethodTypes::FLAT_RATE ,
196
- ShippingMethodTypes::TABLE_RATE ];
197
- if (in_array ($ shippingReferenceId , $ static_shipping_options )) {
206
+ if (in_array ($ shippingReferenceId , $ this ->getSyncableShippingMethodTypes ())) {
198
207
return $ shippingReferenceId ;
199
208
}
200
209
$ map = $ this ->systemConfig ->getShippingMethodsMap ($ storeId );
@@ -226,6 +235,49 @@ private function getShippingMethodLabel(string $shippingOptionName, int $storeId
226
235
return null ;
227
236
}
228
237
238
+ /**
239
+ * Get ShippingMethodDescription
240
+ *
241
+ * @param string $metaShippingTitle
242
+ * @param string $shippingMethod
243
+ * @param int $storeId
244
+ * @return string|null
245
+ */
246
+ private function getShippingDescription (string $ metaShippingTitle , string $ shippingMethod , int $ storeId ): ?string
247
+ {
248
+ $ shippingLabel = $ this ->getShippingMethodLabel ($ metaShippingTitle , $ storeId );
249
+ if ($ shippingLabel ) {
250
+ return $ shippingLabel ;
251
+ }
252
+
253
+ if (in_array ($ shippingMethod , $ this ->getSyncableShippingMethodTypes ())) {
254
+ $ this ->shippingData ->setStoreId ($ storeId );
255
+ [$ carrier ] = explode ('_ ' , $ shippingMethod );
256
+ // Possible values are string, '' and null. Falsey check is acceptable here.
257
+ if ($ carrier ) {
258
+ $ shippingMethodName = $ this ->shippingData ->getFieldFromModel ($ carrier , 'name ' );
259
+ $ shippingOptionTitle = $ this ->shippingData ->getFieldFromModel ($ carrier , 'title ' );
260
+ return $ shippingOptionTitle . ' - ' . $ shippingMethodName ;
261
+ }
262
+ }
263
+
264
+ return null ;
265
+ }
266
+
267
+ /**
268
+ * This function returns a list of shipping methods that can be synced to Meta
269
+ *
270
+ * @return array
271
+ */
272
+ public function getSyncableShippingMethodTypes (): array
273
+ {
274
+ return [
275
+ ShippingMethodTypes::FREE_SHIPPING ,
276
+ ShippingMethodTypes::FLAT_RATE ,
277
+ ShippingMethodTypes::TABLE_RATE
278
+ ];
279
+ }
280
+
229
281
/**
230
282
* Create a magento order billing address from facebook order data
231
283
*
0 commit comments