17
17
18
18
namespace Meta \BusinessExtension \Helper ;
19
19
20
+ use Magento \Catalog \Api \Data \ProductInterface ;
20
21
use Magento \Catalog \Api \ProductRepositoryInterface ;
21
22
use Magento \Customer \Api \CustomerMetadataInterface ;
23
+ use Magento \Framework \Exception \LocalizedException ;
22
24
use Magento \Store \Model \StoreManagerInterface ;
23
25
use Magento \Checkout \Model \Session as CheckoutSession ;
24
26
use Magento \Quote \Model \Quote ;
@@ -50,57 +52,57 @@ class MagentoDataHelper extends AbstractHelper
50
52
/**
51
53
* @var StoreManagerInterface
52
54
*/
53
- private $ storeManager ;
55
+ private StoreManagerInterface $ storeManager ;
54
56
55
57
/**
56
58
* @var CustomerMetadataInterface
57
59
*/
58
- private $ customerMetadata ;
60
+ private CustomerMetadataInterface $ customerMetadata ;
59
61
60
62
/**
61
63
* @var ProductRepositoryInterface
62
64
*/
63
- private $ productRepository ;
65
+ private ProductRepositoryInterface $ productRepository ;
64
66
65
67
/**
66
68
* @var ProductIdentifier
67
69
*/
68
- private $ productIdentifier ;
70
+ private ProductIdentifier $ productIdentifier ;
69
71
70
72
/**
71
73
* @var CheckoutSession
72
74
*/
73
- private $ checkoutSession ;
75
+ private CheckoutSession $ checkoutSession ;
74
76
75
77
/**
76
- * @var Quote
78
+ * @var Quote|null
77
79
*/
78
80
private $ quote ;
79
81
80
82
/**
81
83
* @var CustomerSession
82
84
*/
83
- private $ customerSession ;
85
+ private CustomerSession $ customerSession ;
84
86
85
87
/**
86
88
* @var CategoryRepositoryInterface
87
89
*/
88
- private $ categoryRepository ;
90
+ private CategoryRepositoryInterface $ categoryRepository ;
89
91
90
92
/**
91
93
* @var PricingHelper
92
94
*/
93
- private $ pricingHelper ;
95
+ private PricingHelper $ pricingHelper ;
94
96
95
97
/**
96
98
* @var AddressFactory
97
99
*/
98
- private $ addressFactory ;
100
+ private AddressFactory $ addressFactory ;
99
101
100
102
/**
101
103
* @var RegionFactory
102
104
*/
103
- private $ regionFactory ;
105
+ private RegionFactory $ regionFactory ;
104
106
105
107
/**
106
108
* MagentoDataHelper constructor
@@ -174,25 +176,30 @@ public function getLastName(): string
174
176
}
175
177
176
178
/**
177
- * Return currently logged in users' Date of Birth.
179
+ * Return the product by the given sku
178
180
*
179
- * @return string
181
+ * @param string $productSku
182
+ * @return ProductInterface | bool
180
183
*/
181
- public function getDateOfBirth (): string
184
+ public function getProductBySku ( string $ productSku )
182
185
{
183
- return $ this ->customerSession ->getCustomer ()->getDob ();
186
+ try {
187
+ return $ this ->productRepository ->get ($ productSku );
188
+ } catch (NoSuchEntityException $ e ) {
189
+ return false ;
190
+ }
184
191
}
185
192
186
193
/**
187
- * Return the product by the given sku
194
+ * Get Product by ID
188
195
*
189
- * @param string $productSku
190
- * @return \Magento\Catalog\Api\Data\ProductInterface | bool
196
+ * @param mixed $productId
197
+ * @return false|ProductInterface
191
198
*/
192
- public function getProductBySku ( $ productSku )
199
+ public function getProductById ( $ productId )
193
200
{
194
201
try {
195
- return $ this ->productRepository ->get ( $ productSku );
202
+ return $ this ->productRepository ->getById ( $ productId );
196
203
} catch (NoSuchEntityException $ e ) {
197
204
return false ;
198
205
}
@@ -201,7 +208,7 @@ public function getProductBySku($productSku)
201
208
/**
202
209
* Return the categories for the given product
203
210
*
204
- * @param \Magento\Catalog\Model\ Product $product
211
+ * @param Product $product
205
212
* @return string
206
213
*/
207
214
public function getCategoriesForProduct ($ product ): string
@@ -235,7 +242,7 @@ public function getContentType(Product $product): string
235
242
}
236
243
237
244
/**
238
- * Get content id
245
+ * Get Content IDs (Product IDs)
239
246
*
240
247
* @param Product $product
241
248
* @return bool|int|string
@@ -248,10 +255,10 @@ public function getContentId(Product $product)
248
255
/**
249
256
* Return the price for the given product
250
257
*
251
- * @param \Magento\Catalog\Model\ Product $product
258
+ * @param Product $product
252
259
* @return float
253
260
*/
254
- public function getValueForProduct ($ product ): float
261
+ public function getValueForProduct (Product $ product ): float
255
262
{
256
263
$ price = $ product ->getFinalPrice ();
257
264
return $ this ->pricingHelper ->currency ($ price , false , false );
@@ -261,7 +268,8 @@ public function getValueForProduct($product): float
261
268
* Return the currency used in the store
262
269
*
263
270
* @return string
264
- * @throws \Magento\Framework\Exception\NoSuchEntityException
271
+ * @throws NoSuchEntityException
272
+ * @throws LocalizedException
265
273
*/
266
274
public function getCurrency (): string
267
275
{
@@ -376,7 +384,7 @@ public function getOrderContentIds(): array
376
384
*
377
385
* @return float|null
378
386
*/
379
- public function getOrderTotal ()
387
+ public function getOrderTotal (): ? float
380
388
{
381
389
$ order = $ this ->checkoutSession ->getLastRealOrder ();
382
390
if (!$ order ) {
@@ -452,7 +460,7 @@ public function getCurrentCustomer(): ?Customer
452
460
/**
453
461
* Return the address of a given customer
454
462
*
455
- * @param object $customer
463
+ * @param mixed $customer
456
464
* @return Address
457
465
*/
458
466
public function getCustomerAddress ($ customer ): Address
@@ -461,42 +469,12 @@ public function getCustomerAddress($customer): Address
461
469
return $ this ->addressFactory ->create ()->load ($ customerAddressId );
462
470
}
463
471
464
- /**
465
- * Return the region's code for the given address
466
- *
467
- * @param object $address
468
- * @return string|null
469
- */
470
- public function getRegionCodeForAddress ($ address ): ?string
471
- {
472
- $ region = $ this ->regionFactory ->create ()->load ($ address ->getRegionId ());
473
- if ($ region ) {
474
- return $ region ->getCode ();
475
- } else {
476
- return null ;
477
- }
478
- }
479
-
480
- /**
481
- * Return the string representation of the customer gender
482
- *
483
- * @param object $customer
484
- * @return string|null
485
- */
486
- public function getGenderAsString ($ customer ): ?string
487
- {
488
- if ($ customer ->getGender ()) {
489
- return $ customer ->getResource ()->getAttribute ('gender ' )->getSource ()->getOptionText ($ customer ->getGender ());
490
- }
491
- return null ;
492
- }
493
-
494
472
/**
495
473
* Return all of the match keys that can be extracted from order information
496
474
*
497
475
* @return array
498
- * @throws \Magento\Framework\Exception\ LocalizedException
499
- * @throws \Magento\Framework\Exception\ NoSuchEntityException
476
+ * @throws LocalizedException
477
+ * @throws NoSuchEntityException
500
478
*/
501
479
public function getUserDataFromOrder (): array
502
480
{
@@ -537,8 +515,8 @@ public function getUserDataFromOrder(): array
537
515
* Return all of the match keys that can be extracted from user session
538
516
*
539
517
* @return array
540
- * @throws \Magento\Framework\Exception\ LocalizedException
541
- * @throws \Magento\Framework\Exception\ NoSuchEntityException
518
+ * @throws LocalizedException
519
+ * @throws NoSuchEntityException
542
520
*/
543
521
public function getUserDataFromSession (): array
544
522
{
@@ -576,7 +554,7 @@ public function getUserDataFromSession(): array
576
554
}
577
555
578
556
/**
579
- * Hash value
557
+ * Get Hash value
580
558
*
581
559
* @param string $string
582
560
* @return string
@@ -590,6 +568,8 @@ private function hashValue($string): string
590
568
* Get active quote
591
569
*
592
570
* @return Quote
571
+ * @throws LocalizedException
572
+ * @throws NoSuchEntityException
593
573
*/
594
574
public function getQuote (): Quote
595
575
{
0 commit comments