3
3
4
4
use Magento \Catalog \Api \Data \ProductInterface ;
5
5
use Magento \Catalog \Model \Product \Attribute \Source \Status as ProductStatus ;
6
+ use Magento \Catalog \Pricing \Price \FinalPrice ;
7
+ use Magento \Catalog \Pricing \Price \RegularPrice ;
8
+ use Magento \Catalog \Pricing \Price \SpecialPrice ;
6
9
use Magento \CatalogInventory \Api \StockRegistryInterface ;
10
+ use Magento \Framework \App \Area as AppArea ;
11
+ use Magento \Framework \App \Http \Context as HttpContext ;
7
12
use Magento \Framework \Exception \LocalizedException ;
13
+ use Magento \Store \Model \App \Emulation as AppEmulation ;
8
14
use Ometria \Api \Helper \Filter \V1 \Service as FilterService ;
9
15
use Ometria \Api \Helper \Format \V1 \Products as Helper ;
10
16
@@ -36,6 +42,12 @@ class Products extends Base
36
42
/** @var StockRegistryInterface */
37
43
private $ stockRegistry ;
38
44
45
+ /** @var HttpContext */
46
+ private $ httpContext ;
47
+
48
+ /** @var AppEmulation */
49
+ private $ appEmulation ;
50
+
39
51
protected $ storeIdCache =false ;
40
52
protected $ productTypeFactory ;
41
53
@@ -72,7 +84,9 @@ public function __construct(
72
84
\Magento \Directory \Helper \Data $ directoryHelper ,
73
85
\Ometria \Api \Helper \StoreUrl $ storeUrlHelper ,
74
86
\Magento \Catalog \Model \Product \TypeFactory $ productTypeFactory ,
75
- StockRegistryInterface $ stockRegistry
87
+ StockRegistryInterface $ stockRegistry ,
88
+ HttpContext $ httpContext ,
89
+ AppEmulation $ appEmulation
76
90
) {
77
91
parent ::__construct ($ context );
78
92
$ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
@@ -94,6 +108,8 @@ public function __construct(
94
108
$ this ->storeUrlHelper = $ storeUrlHelper ;
95
109
$ this ->productTypeFactory = $ productTypeFactory ;
96
110
$ this ->stockRegistry = $ stockRegistry ;
111
+ $ this ->httpContext = $ httpContext ;
112
+ $ this ->appEmulation = $ appEmulation ;
97
113
}
98
114
99
115
public function execute ()
@@ -374,7 +390,7 @@ protected function addStoreListingToItems($items)
374
390
}
375
391
376
392
377
- protected function getProductListingsForStore ($ store , $ productIds , $ store_listings )
393
+ protected function getProductListingsForStore ($ store , $ productIds , $ storeListings )
378
394
{
379
395
$ storeId = $ store ->getId ();
380
396
@@ -392,8 +408,8 @@ protected function getProductListingsForStore($store, $productIds, $store_listin
392
408
$ this ->getRequest ()->getParam ('product_image ' , 'image ' )
393
409
);
394
410
395
- $ base_currency = $ store ->getBaseCurrency ()->getCode ();
396
- $ store_currency = $ store ->getDefaultCurrency ()->getCode ();
411
+ $ baseCurrency = $ store ->getBaseCurrency ()->getCode ();
412
+ $ storeCurrency = $ store ->getDefaultCurrency ()->getCode ();
397
413
398
414
foreach ($ items as $ item ) {
399
415
$ id = $ item ['id ' ];
@@ -402,57 +418,57 @@ protected function getProductListingsForStore($store, $productIds, $store_listin
402
418
'store_id ' => $ storeId ,
403
419
'title ' => $ item ['name ' ],
404
420
'url ' => $ url ,
405
- 'store_currency ' => $ store_currency ,
421
+ 'store_currency ' => $ storeCurrency ,
406
422
'visibility ' => $ item ['visibility ' ],
407
423
'status ' => $ item ['status ' ],
408
424
'image_url ' => $ item ['image_url ' ]
409
- );
425
+ );
410
426
411
- $ tmp = $ this ->appendPricing ($ id , $ tmp , $ storeId , $ base_currency , $ store_currency );
427
+ $ tmp = $ this ->appendPricing ($ id , $ tmp , $ storeId , $ baseCurrency , $ storeCurrency );
412
428
413
- $ store_listings [$ id ][$ storeId ] = $ tmp ;
429
+ $ storeListings [$ id ][$ storeId ] = $ tmp ;
414
430
}
415
431
416
- return $ store_listings ;
432
+ return $ storeListings ;
417
433
}
418
434
419
- protected function appendPricing ($ product_id , $ item , $ storeId = null , $ base_currency = null , $ store_currency = null )
435
+ protected function appendPricing ($ productId , $ item , $ storeId = null , $ baseCurrency = null , $ storeCurrency = null )
420
436
{
421
- $ store_price = $ this ->getProductPrice (
422
- $ product_id ,
437
+ $ storePrice = $ this ->getProductPrice (
438
+ $ productId ,
423
439
$ storeId ,
424
- \ Magento \ Catalog \ Pricing \ Price \ RegularPrice::PRICE_CODE ,
425
- $ base_currency ,
426
- $ store_currency
440
+ RegularPrice::PRICE_CODE ,
441
+ $ baseCurrency ,
442
+ $ storeCurrency
427
443
);
428
444
429
- if ($ store_price ) {
430
- $ item ['price ' ] = $ store_price ;
445
+ if ($ storePrice ) {
446
+ $ item ['price ' ] = $ storePrice ;
431
447
}
432
448
433
- $ store_special_price = $ this ->getProductPrice (
434
- $ product_id ,
449
+ $ storeSpecialPrice = $ this ->getProductPrice (
450
+ $ productId ,
435
451
$ storeId ,
436
- \ Magento \ Catalog \ Pricing \ Price \ SpecialPrice::PRICE_CODE ,
437
- $ base_currency ,
438
- $ store_currency
452
+ SpecialPrice::PRICE_CODE ,
453
+ $ baseCurrency ,
454
+ $ storeCurrency
439
455
);
440
456
441
- if ($ store_special_price ) {
442
- $ item ['special_price ' ] = $ store_special_price ;
457
+ if ($ storeSpecialPrice ) {
458
+ $ item ['special_price ' ] = $ storeSpecialPrice ;
443
459
}
444
460
445
461
if ($ this ->_request ->getParam ('final_price ' ) === 'true ' ) {
446
- $ store_final_price = $ this ->getProductPrice (
447
- $ product_id ,
462
+ $ storeFinalPrice = $ this ->getProductPrice (
463
+ $ productId ,
448
464
$ storeId ,
449
- \ Magento \ Catalog \ Pricing \ Price \ FinalPrice::PRICE_CODE ,
450
- $ base_currency ,
451
- $ store_currency
465
+ FinalPrice::PRICE_CODE ,
466
+ $ baseCurrency ,
467
+ $ storeCurrency
452
468
);
453
469
454
- if ($ store_final_price ) {
455
- $ item ['final_price ' ] = $ store_final_price ;
470
+ if ($ storeFinalPrice ) {
471
+ $ item ['final_price ' ] = $ storeFinalPrice ;
456
472
}
457
473
}
458
474
@@ -482,28 +498,46 @@ private function appendStock($productId, $item)
482
498
}
483
499
484
500
protected function getProductPrice (
485
- $ product_id ,
501
+ $ productId ,
486
502
$ storeId ,
487
- $ price_code ,
488
- $ base_currency = null ,
489
- $ store_currency = null
503
+ $ priceCode ,
504
+ $ baseCurrency = null ,
505
+ $ storeCurrency = null
490
506
) {
491
- $ product = $ this ->productRepository ->getById ($ product_id , false , $ storeId );
507
+ // Override HTTP currency value to ensure Magento internals use correct store currency
508
+ $ beforeCurrency = $ this ->httpContext ->getValue (HttpContext::CONTEXT_CURRENCY );
509
+ $ this ->httpContext ->setValue (HttpContext::CONTEXT_CURRENCY , $ storeCurrency , null );
492
510
493
- $ price = $ product -> getPriceInfo ()-> getPrice ( $ price_code )-> getValue ( );
511
+ $ product = $ this -> productRepository -> getById ( $ productId , false , $ storeId );
494
512
495
- if ($ store_currency && $ base_currency ) {
513
+ // Emulate store as required to ensure Magento internals use correct store currency
514
+ $ this ->appEmulation ->startEnvironmentEmulation (
515
+ $ product ->getStoreId (),
516
+ AppArea::AREA_FRONTEND ,
517
+ true
518
+ );
519
+
520
+ $ price = $ product ->getPriceInfo ()->getPrice ($ priceCode )->getValue ();
521
+
522
+ // Final price is already converted so skip it here
523
+ if ($ priceCode != FinalPrice::PRICE_CODE && $ storeCurrency && $ baseCurrency ) {
496
524
try {
497
525
$ price = $ this ->directoryHelper ->currencyConvert (
498
526
$ price ,
499
- $ base_currency ,
500
- $ store_currency
527
+ $ baseCurrency ,
528
+ $ storeCurrency
501
529
);
502
530
} catch (\Exception $ e ) {
503
531
// Allow the "undefined rate" exception and return the price as is if no rate has been setup.
504
532
}
505
533
}
506
534
535
+ // Stop emulating store
536
+ $ this ->appEmulation ->stopEnvironmentEmulation ();
537
+
538
+ // Reset HTTP currency value to before value
539
+ $ this ->httpContext ->setValue (HttpContext::CONTEXT_CURRENCY , $ beforeCurrency , $ beforeCurrency );
540
+
507
541
return $ price ;
508
542
}
509
543
0 commit comments