23
23
use Magento \Checkout \Model \Session as CheckoutSession ;
24
24
use Magento \Framework \App \Action \HttpGetActionInterface ;
25
25
use Magento \Framework \App \Request \Http ;
26
- use Magento \Framework \Controller \Result \Redirect ;
27
- use Magento \Framework \Controller \Result \RedirectFactory ;
26
+ use Magento \Framework \View \Result \PageFactory ;
28
27
use Magento \Framework \Exception \CouldNotSaveException ;
29
- use Magento \Framework \Exception \LocalizedException ;
30
28
use Magento \Quote \Api \CartRepositoryInterface ;
31
29
use Magento \Quote \Api \Data \CartItemInterfaceFactory ;
32
30
use Magento \Quote \Model \GuestCart \GuestCartItemRepository ;
40
38
use Magento \ConfigurableProduct \Model \Product \Type \Configurable as ConfigurableType ;
41
39
use Magento \Quote \Api \Data \ProductOptionInterfaceFactory ;
42
40
use Magento \ConfigurableProduct \Api \Data \ConfigurableItemOptionValueInterfaceFactory ;
43
- use Meta \BusinessExtension \Model \Api \CustomApiKey \Authenticator ;
44
41
use Meta \BusinessExtension \Helper \FBEHelper ;
45
42
use Meta \Sales \Helper \OrderHelper ;
46
43
use Exception ;
47
44
48
45
/**
46
+ * Handles the Meta Checkout URL endpoint.
47
+ * Creates a quote, adds products, applies coupons, and redirects to checkout.
48
+ *
49
49
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
50
50
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
51
51
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
52
+ * @SuppressWarnings(PHPMD.TooManyFields)
52
53
*/
53
54
class Index implements HttpGetActionInterface
54
55
{
@@ -71,7 +72,6 @@ class Index implements HttpGetActionInterface
71
72
* @var CartRepositoryInterface
72
73
*/
73
74
private CartRepositoryInterface $ quoteRepository ;
74
-
75
75
/**
76
76
* @var GuestCartItemRepository
77
77
*/
@@ -98,9 +98,9 @@ class Index implements HttpGetActionInterface
98
98
private Http $ httpRequest ;
99
99
100
100
/**
101
- * @var RedirectFactory
101
+ * @var PageFactory
102
102
*/
103
- private RedirectFactory $ resultRedirectFactory ;
103
+ private PageFactory $ resultPageFactory ;
104
104
105
105
/**
106
106
* @var FBEHelper
@@ -133,40 +133,40 @@ class Index implements HttpGetActionInterface
133
133
private ConfigurableItemOptionValueInterfaceFactory $ configurableItemOptionValueFactory ;
134
134
135
135
/**
136
- * @param QuoteFactory $quoteFactory
137
- * @param QuoteIdMaskFactory $quoteIdMaskFactory
138
- * @param AddressFactory $quoteAddressFactory
139
- * @param CartRepositoryInterface $quoteRepository
140
- * @param GuestCartItemRepository $guestCartItemRepository
141
- * @param CartItemInterfaceFactory $cartItemInterfaceFactory
142
- * @param GuestCouponManagement $guestCouponManagement
143
- * @param CheckoutSession $checkoutSession
144
- * @param Http $httpRequest
145
- * @param RedirectFactory $resultRedirectFactory
146
- * @param Authenticator $authenticator
147
- * @param FBEHelper $fbeHelper
148
- * @param OrderHelper $orderHelper
149
- * @param ProductRepositoryInterface $productRepository
150
- * @param ConfigurableType $configurableType
151
- * @param ProductOptionInterfaceFactory $productOptionInterfaceFactory
136
+ * Constructor
137
+ *
138
+ * @param QuoteFactory $quoteFactory
139
+ * @param QuoteIdMaskFactory $quoteIdMaskFactory
140
+ * @param AddressFactory $quoteAddressFactory
141
+ * @param CartRepositoryInterface $quoteRepository
142
+ * @param GuestCartItemRepository $guestCartItemRepository
143
+ * @param CartItemInterfaceFactory $cartItemInterfaceFactory
144
+ * @param GuestCouponManagement $guestCouponManagement
145
+ * @param CheckoutSession $checkoutSession
146
+ * @param Http $httpRequest
147
+ * @param PageFactory $resultPageFactory
148
+ * @param FBEHelper $fbeHelper
149
+ * @param OrderHelper $orderHelper
150
+ * @param ProductRepositoryInterface $productRepository
151
+ * @param ConfigurableType $configurableType
152
+ * @param ProductOptionInterfaceFactory $productOptionInterfaceFactory
152
153
* @param ConfigurableItemOptionValueInterfaceFactory $configurableItemOptionValueFactory
153
154
*/
154
155
public function __construct (
155
- QuoteFactory $ quoteFactory ,
156
- QuoteIdMaskFactory $ quoteIdMaskFactory ,
157
- AddressFactory $ quoteAddressFactory ,
158
- CartRepositoryInterface $ quoteRepository ,
159
- GuestCartItemRepository $ guestCartItemRepository ,
156
+ QuoteFactory $ quoteFactory ,
157
+ QuoteIdMaskFactory $ quoteIdMaskFactory ,
158
+ AddressFactory $ quoteAddressFactory ,
159
+ CartRepositoryInterface $ quoteRepository ,
160
+ GuestCartItemRepository $ guestCartItemRepository ,
160
161
CartItemInterfaceFactory $ cartItemInterfaceFactory ,
161
- GuestCouponManagement $ guestCouponManagement ,
162
- CheckoutSession $ checkoutSession ,
163
- Http $ httpRequest ,
164
- RedirectFactory $ resultRedirectFactory ,
165
- Authenticator $ authenticator ,
166
- FBEHelper $ fbeHelper ,
167
- OrderHelper $ orderHelper ,
162
+ GuestCouponManagement $ guestCouponManagement ,
163
+ CheckoutSession $ checkoutSession ,
164
+ Http $ httpRequest ,
165
+ PageFactory $ resultPageFactory ,
166
+ FBEHelper $ fbeHelper ,
167
+ OrderHelper $ orderHelper ,
168
168
ProductRepositoryInterface $ productRepository ,
169
- ConfigurableType $ configurableType ,
169
+ ConfigurableType $ configurableType ,
170
170
ProductOptionInterfaceFactory $ productOptionInterfaceFactory ,
171
171
ConfigurableItemOptionValueInterfaceFactory $ configurableItemOptionValueFactory
172
172
) {
@@ -179,7 +179,7 @@ public function __construct(
179
179
$ this ->guestCouponManagement = $ guestCouponManagement ;
180
180
$ this ->checkoutSession = $ checkoutSession ;
181
181
$ this ->httpRequest = $ httpRequest ;
182
- $ this ->resultRedirectFactory = $ resultRedirectFactory ;
182
+ $ this ->resultPageFactory = $ resultPageFactory ;
183
183
$ this ->fbeHelper = $ fbeHelper ;
184
184
$ this ->orderHelper = $ orderHelper ;
185
185
$ this ->productRepository = $ productRepository ;
@@ -189,18 +189,18 @@ public function __construct(
189
189
}
190
190
191
191
/**
192
- * Execute action based on request.
192
+ * Execute action based on request and return result
193
193
*
194
- * @return Redirect
195
- * @throws LocalizedException
194
+ * Processes incoming request to create cart with specified products and coupon.
195
+ *
196
+ * @return \Magento\Framework\Controller\ResultInterface|\Magento\Framework\View\Result\Page
197
+ * @throws CouldNotSaveException If quote cannot be created.
196
198
*/
197
199
public function execute ()
198
200
{
199
201
$ ebid = $ this ->httpRequest ->getParam ('ebid ' );
200
202
$ productsParam = $ this ->httpRequest ->getParam ('products ' );
201
203
$ coupon = $ this ->httpRequest ->getParam ('coupon ' );
202
- $ redirectPath = $ this ->httpRequest ->getParam ('redirect ' );
203
-
204
204
$ storeId = (int )$ this ->orderHelper ->getStoreIdByExternalBusinessId ($ ebid );
205
205
$ products = explode (', ' , $ productsParam );
206
206
@@ -216,15 +216,19 @@ public function execute()
216
216
}
217
217
218
218
$ this ->checkoutSession ->replaceQuote ($ quote );
219
- return $ this ->redirectToPath ($ redirectPath );
219
+
220
+ $ resultPage = $ this ->resultPageFactory ->create ();
221
+ $ resultPage ->addHandle ('checkout_index_index ' );
222
+ return $ resultPage ;
220
223
}
221
224
222
225
/**
223
- * Create a new quote.
226
+ * Create a new guest quote for the specified store.
227
+ *
228
+ * @param int $storeId The store ID for the quote.
224
229
*
225
- * @param int $storeId
226
- * @return Quote
227
- * @throws CouldNotSaveException
230
+ * @return Quote The created quote object.
231
+ * @throws CouldNotSaveException If the quote cannot be saved.
228
232
*/
229
233
private function createQuote (int $ storeId ): Quote
230
234
{
@@ -244,10 +248,11 @@ private function createQuote(int $storeId): Quote
244
248
}
245
249
246
250
/**
247
- * Get masked cart ID.
251
+ * Get the masked (hashed) ID for a given quote.
252
+ *
253
+ * @param Quote $quote The quote object.
248
254
*
249
- * @param Quote $quote
250
- * @return string
255
+ * @return string The masked quote ID.
251
256
*/
252
257
private function getMaskedCartId (Quote $ quote ): string
253
258
{
@@ -257,25 +262,32 @@ private function getMaskedCartId(Quote $quote): string
257
262
}
258
263
259
264
/**
260
- * Add product to cart.
265
+ * Add a product item to the guest cart.
266
+ *
267
+ * Handles simple and configurable products.
268
+ *
269
+ * @param string $productItem The product string in "sku:qty" format.
270
+ * @param string $cartId The masked cart ID.
271
+ * @param int $storeId The store ID.
261
272
*
262
- * @param string $productItem
263
- * @param string $cartId
264
- * @param int $storeId
265
273
* @return void
266
274
*/
267
275
private function addProductToCart (string $ productItem , string $ cartId , int $ storeId ): void
268
276
{
269
277
try {
270
- [$ sku , $ quantity ] = explode (': ' , $ productItem );
278
+ [$ sku_or_id , $ quantity ] = explode (': ' , $ productItem );
271
279
$ quantity = (int )$ quantity ;
280
+ try {
281
+ $ product = $ this ->productRepository ->get ($ sku_or_id , false , $ storeId , true );
282
+ } catch (Exception $ e ) {
283
+ $ product = $ this ->productRepository ->getById ($ sku_or_id );
284
+ }
272
285
273
- $ product = $ this ->productRepository ->get ($ sku , false , $ storeId , true );
274
-
275
- [$ parentSku , $ configurableOptions ] = $ this ->getConfigurableOptions ($ product , $ storeId );
286
+ [$ parentSku , $ configurableOptions ]
287
+ = $ this ->getConfigurableOptions ($ product , $ storeId );
276
288
277
289
$ cartItem = $ this ->cartItemInterfaceFactory ->create ();
278
- $ cartItem ->setSku ($ sku );
290
+ $ cartItem ->setSku ($ product -> getSku () );
279
291
$ cartItem ->setQty ($ quantity );
280
292
$ cartItem ->setQuoteId ($ cartId );
281
293
@@ -290,53 +302,53 @@ private function addProductToCart(string $productItem, string $cartId, int $stor
290
302
291
303
$ this ->guestCartItemRepository ->save ($ cartItem );
292
304
} catch (Exception $ e ) {
293
- $ this ->logExceptionToMeta ($ e , $ storeId , 'error_adding_item ' , [
294
- 'cart_id ' => $ cartId ,
295
- 'sku ' => $ sku ,
296
- 'quantity ' => $ quantity ,
297
- ]);
305
+ $ this ->logExceptionToMeta (
306
+ $ e ,
307
+ $ storeId ,
308
+ 'error_adding_item ' ,
309
+ [
310
+ 'cart_id ' => $ cartId ,
311
+ // can't always rely on sku_or_id being present, as it resides in the catch block
312
+ 'sku ' => $ productItem ,
313
+ 'quantity ' => $ quantity ,
314
+ ]
315
+ );
298
316
}
299
317
}
300
318
301
319
/**
302
- * Apply coupon to cart.
320
+ * Apply a coupon code to the guest cart.
321
+ *
322
+ * @param string $coupon The coupon code.
323
+ * @param string $cartId The masked cart ID.
324
+ * @param int $storeId The store ID.
303
325
*
304
- * @param string $coupon
305
- * @param string $cartId
306
- * @param int $storeId
307
326
* @return void
308
327
*/
309
328
private function applyCouponToCart (string $ coupon , string $ cartId , int $ storeId ): void
310
329
{
311
330
try {
312
331
$ this ->guestCouponManagement ->set ($ cartId , $ coupon );
313
332
} catch (Exception $ e ) {
314
- $ this ->logExceptionToMeta ($ e , $ storeId , 'error_adding_coupon ' , [
315
- 'cart_id ' => $ cartId ,
316
- 'coupon ' => $ coupon ,
317
- ]);
333
+ $ this ->logExceptionToMeta (
334
+ $ e ,
335
+ $ storeId ,
336
+ 'error_adding_coupon ' ,
337
+ [
338
+ 'cart_id ' => $ cartId ,
339
+ 'coupon ' => $ coupon ,
340
+ ]
341
+ );
318
342
}
319
343
}
320
344
321
345
/**
322
- * Redirect to path .
346
+ * Get configurable product options if the product is a child of a configurable product .
323
347
*
324
- * @param string|null $redirectPath
325
- * @return Redirect
326
- */
327
- private function redirectToPath (?string $ redirectPath ): Redirect
328
- {
329
- $ resultRedirect = $ this ->resultRedirectFactory ->create ();
330
- $ resultRedirect ->setPath ($ redirectPath ?: 'checkout ' );
331
- return $ resultRedirect ;
332
- }
333
-
334
- /**
335
- * Get configurable options for a product.
348
+ * @param ProductInterface $product The product object.
349
+ * @param int $storeId The store ID.
336
350
*
337
- * @param ProductInterface $product
338
- * @param int $storeId
339
- * @return array
351
+ * @return array The parent SKU (if applicable) and configurable options.
340
352
*/
341
353
private function getConfigurableOptions (ProductInterface $ product , int $ storeId ): array
342
354
{
@@ -369,12 +381,13 @@ private function getConfigurableOptions(ProductInterface $product, int $storeId)
369
381
}
370
382
371
383
/**
372
- * Log exception to Meta.
384
+ * Log an exception to Meta using the FBEHelper.
385
+ *
386
+ * @param Exception $e The exception to log.
387
+ * @param int $storeId The store ID associated with the exception.
388
+ * @param string $eventType Where the exception occurred.
389
+ * @param array $extraData Additional data to include in the log entry.
373
390
*
374
- * @param Exception $e
375
- * @param int $storeId
376
- * @param string $eventType
377
- * @param array $extraData
378
391
* @return void
379
392
*/
380
393
private function logExceptionToMeta (Exception $ e , int $ storeId , string $ eventType , array $ extraData = []): void
0 commit comments