1
1
<?php
2
2
namespace Ometria \Api \Controller \V1 ;
3
+
4
+ use Magento \CatalogInventory \Api \StockRegistryInterface ;
5
+ use Magento \Framework \Exception \LocalizedException ;
3
6
use Ometria \Api \Helper \Format \V1 \Products as Helper ;
4
- use \Ometria \Api \Controller \V1 \Base ;
7
+ use Ometria \Api \Controller \V1 \Base ;
8
+
5
9
class Products extends Base
6
10
{
7
11
const PRODUCT_TYPE_IDX = 'magento_product_type ' ;
@@ -28,6 +32,9 @@ class Products extends Base
28
32
protected $ directoryHelper ;
29
33
protected $ storeUrlHelper ;
30
34
35
+ /** @var StockRegistryInterface */
36
+ private $ stockRegistry ;
37
+
31
38
protected $ storeIdCache =false ;
32
39
protected $ productTypeFactory ;
33
40
@@ -64,7 +71,8 @@ public function __construct(
64
71
\Magento \Framework \App \ResourceConnection $ resourceConnection ,
65
72
\Magento \Directory \Helper \Data $ directoryHelper ,
66
73
\Ometria \Api \Helper \StoreUrl $ storeUrlHelper ,
67
- \Magento \Catalog \Model \Product \TypeFactory $ productTypeFactory
74
+ \Magento \Catalog \Model \Product \TypeFactory $ productTypeFactory ,
75
+ StockRegistryInterface $ stockRegistry
68
76
) {
69
77
parent ::__construct ($ context );
70
78
$ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
@@ -86,6 +94,7 @@ public function __construct(
86
94
$ this ->directoryHelper = $ directoryHelper ;
87
95
$ this ->storeUrlHelper = $ storeUrlHelper ;
88
96
$ this ->productTypeFactory = $ productTypeFactory ;
97
+ $ this ->stockRegistry = $ stockRegistry ;
89
98
}
90
99
91
100
protected function getArrayKey ($ array , $ key )
@@ -159,6 +168,7 @@ protected function serializeItem($item)
159
168
}
160
169
161
170
$ tmp = $ this ->appendPricing ($ tmp ['id ' ], $ tmp );
171
+ $ tmp = $ this ->appendStock ($ tmp ['id ' ], $ tmp );
162
172
163
173
if (isset ($ item ['store_listings ' ])) {
164
174
$ tmp ['store_listings ' ] = $ item ['store_listings ' ];
@@ -402,69 +412,100 @@ protected function getProductListingsForStore(
402
412
'image_url ' => $ this ->getBaseImageUrl () . $ this ->getCustomAttribute ($ item ,$ this ->getImageUrlKey ())
403
413
);
404
414
405
- $ tmp = $ this ->appendPricing ($ id , $ tmp , $ store_currency , $ base_currency );
415
+ $ tmp = $ this ->appendPricing ($ id , $ tmp , $ storeId , $ base_currency, $ store_currency );
406
416
407
417
$ store_listings [$ id ][$ storeId ] = $ tmp ;
408
418
}
409
419
410
420
return $ store_listings ;
411
421
}
412
422
413
- protected function appendPricing ($ product_id , $ item , $ store_currency = null , $ base_currency= null ){
414
-
423
+ protected function appendPricing ($ product_id , $ item , $ storeId = null , $ base_currency = null , $ store_currency = null )
424
+ {
415
425
$ store_price = $ this ->getProductPrice (
416
426
$ product_id ,
417
- $ item ,
427
+ $ storeId ,
418
428
\Magento \Catalog \Pricing \Price \RegularPrice::PRICE_CODE ,
419
- $ store_currency ,
420
- $ base_currency );
429
+ $ base_currency ,
430
+ $ store_currency
431
+ );
432
+
433
+ if ($ store_price ) {
434
+ $ item ['price ' ] = $ store_price ;
435
+ }
421
436
422
437
$ store_special_price = $ this ->getProductPrice (
423
438
$ product_id ,
424
- $ item ,
439
+ $ storeId ,
425
440
\Magento \Catalog \Pricing \Price \SpecialPrice::PRICE_CODE ,
426
- $ store_currency ,
427
- $ base_currency );
441
+ $ base_currency ,
442
+ $ store_currency
443
+ );
428
444
429
- $ item ['price ' ] = $ store_price ;
445
+ if ($ store_special_price ) {
446
+ $ item ['special_price ' ] = $ store_special_price ;
447
+ }
430
448
431
449
if ($ this ->_request ->getParam ('final_price ' ) === 'true ' ) {
432
450
$ store_final_price = $ this ->getProductPrice (
433
451
$ product_id ,
434
- $ item ,
452
+ $ storeId ,
435
453
\Magento \Catalog \Pricing \Price \FinalPrice::PRICE_CODE ,
436
- $ store_currency ,
437
- $ base_currency );
454
+ $ base_currency ,
455
+ $ store_currency
456
+ );
438
457
439
- $ item ['final_price ' ] = $ store_final_price ;
458
+ if ($ store_final_price ) {
459
+ $ item ['final_price ' ] = $ store_final_price ;
460
+ }
440
461
}
441
462
442
- if ($ store_special_price ) {
443
- $ item ['special_price ' ] = $ store_special_price ;
444
- //$item['special_price_dt_from'] = null;
445
- //$item['special_price_dt_to'] = null;
463
+ return $ item ;
464
+ }
465
+
466
+ /**
467
+ * @param $productId
468
+ * @param $item
469
+ * @return mixed
470
+ * @throws LocalizedException
471
+ */
472
+ private function appendStock ($ productId , $ item )
473
+ {
474
+ $ websiteId = $ this ->storeManager ->getWebsite ()->getId ();
475
+ $ stockItem = $ this ->stockRegistry ->getStockItem ($ productId , $ websiteId );
476
+
477
+ if (isset ($ stockItem ['is_in_stock ' ])) {
478
+ $ item ['is_in_stock ' ] = $ stockItem ['is_in_stock ' ];
479
+ }
480
+
481
+ if (isset ($ stockItem ['qty ' ])) {
482
+ $ item ['qty ' ] = (float ) $ stockItem ['qty ' ];
446
483
}
447
484
448
485
return $ item ;
449
486
}
450
487
451
-
452
488
protected function getProductPrice (
453
489
$ product_id ,
454
- $ item ,
490
+ $ storeId ,
455
491
$ price_code ,
456
- $ store_currency =null ,
457
- $ base_currency =null
458
- ){
459
- $ product = $ this ->productRepository ->getById ($ product_id );
460
- $ price = $ product ->getPriceInfo ()->getPrice ($ price_code )->getValue ();
492
+ $ base_currency = null ,
493
+ $ store_currency = null
494
+ ) {
495
+ $ product = $ this ->productRepository ->getById ($ product_id , false , $ storeId );
461
496
462
- if ($ store_currency && $ base_currency ){
463
- $ price = $ this ->directoryHelper ->currencyConvert (
464
- $ price ,
465
- $ base_currency ,
466
- $ store_currency
497
+ $ price = $ product ->getPriceInfo ()->getPrice ($ price_code )->getValue ();
498
+
499
+ if ($ store_currency && $ base_currency ) {
500
+ try {
501
+ $ price = $ this ->directoryHelper ->currencyConvert (
502
+ $ price ,
503
+ $ base_currency ,
504
+ $ store_currency
467
505
);
506
+ } catch (\Exception $ e ) {
507
+ // Allow the "undefined rate" exception and return the price as is if no rate has been setup.
508
+ }
468
509
}
469
510
470
511
return $ price ;
0 commit comments