17
17
use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
18
18
use Magento \Framework \App \Action \Action ;
19
19
use Magento \Framework \App \Action \Context ;
20
+ use Magento \Framework \App \Area as AppArea ;
21
+ use Magento \Framework \App \Http \Context as HttpContext ;
20
22
use Magento \Framework \Controller \ResultFactory ;
21
23
use Magento \Framework \Controller \ResultInterface ;
22
24
use Magento \Framework \Exception \NoSuchEntityException ;
25
+ use Magento \Store \Model \App \Emulation as AppEmulation ;
23
26
use Magento \Store \Model \StoreManagerInterface ;
24
27
use Magento \Tax \Api \Data \QuoteDetailsInterfaceFactory ;
25
28
use Magento \Tax \Api \Data \QuoteDetailsItemInterfaceFactory ;
@@ -83,6 +86,12 @@ class Products extends Action
83
86
/** @var TaxCalculationInterface */
84
87
private $ taxCalculationService ;
85
88
89
+ /** @var HttpContext */
90
+ private $ httpContext ;
91
+
92
+ /** @var AppEmulation */
93
+ private $ appEmulation ;
94
+
86
95
/** @var array */
87
96
private $ productCollections = [];
88
97
@@ -124,6 +133,8 @@ class Products extends Action
124
133
* @param QuoteDetailsInterfaceFactory $quoteDetailsFactory
125
134
* @param QuoteDetailsItemInterfaceFactory $quoteDetailsItemFactory
126
135
* @param TaxCalculationInterface $taxCalculationService
136
+ * @param HttpContext $httpContext
137
+ * @param AppEmulation $appEmulation
127
138
*/
128
139
public function __construct (
129
140
Context $ context ,
@@ -141,7 +152,9 @@ public function __construct(
141
152
TaxConfig $ taxConfig ,
142
153
QuoteDetailsInterfaceFactory $ quoteDetailsFactory ,
143
154
QuoteDetailsItemInterfaceFactory $ quoteDetailsItemFactory ,
144
- TaxCalculationInterface $ taxCalculationService
155
+ TaxCalculationInterface $ taxCalculationService ,
156
+ HttpContext $ httpContext ,
157
+ AppEmulation $ appEmulation
145
158
) {
146
159
parent ::__construct ($ context );
147
160
@@ -160,6 +173,8 @@ public function __construct(
160
173
$ this ->quoteDetailsFactory = $ quoteDetailsFactory ;
161
174
$ this ->quoteDetailsItemFactory = $ quoteDetailsItemFactory ;
162
175
$ this ->taxCalculationService = $ taxCalculationService ;
176
+ $ this ->httpContext = $ httpContext ;
177
+ $ this ->appEmulation = $ appEmulation ;
163
178
}
164
179
165
180
/**
@@ -519,7 +534,7 @@ private function getListings(ProductInterface $product)
519
534
OmetriaProductInterface::STORE_ID => $ storeId ,
520
535
OmetriaProductInterface::TITLE => $ productInStore ->getName (),
521
536
OmetriaProductInterface::URL => $ this ->getProductUrl ($ productInStore ),
522
- OmetriaProductInterface::STORE_CURRENCY => $ this ->getStoreCurrency ($ storeId ),
537
+ OmetriaProductInterface::STORE_CURRENCY => $ this ->getStoreDefaultCurrency ($ storeId ),
523
538
OmetriaProductInterface::VISIBILITY => (int ) $ productInStore ->getVisibility (),
524
539
OmetriaProductInterface::STATUS => (int ) $ productInStore ->getStatus (),
525
540
OmetriaProductInterface::IMAGE_URL => $ this ->getImageUrl ($ productInStore )
@@ -541,34 +556,58 @@ private function getListings(ProductInterface $product)
541
556
*/
542
557
private function appendProductPriceData (&$ productData , ProductInterface $ product )
543
558
{
544
- $ prices = $ product ->getPriceInfo ()->getPrices ();
559
+ $ storeId = $ product ->getStoreId ();
560
+ $ storeCurrency = $ this ->getStoreDefaultCurrency ($ storeId );
561
+
562
+ // Override HTTP currency value to ensure Magento internals use correct store currency
563
+ $ beforeCurrency = $ this ->httpContext ->getValue (HttpContext::CONTEXT_CURRENCY );
564
+ $ this ->httpContext ->setValue (HttpContext::CONTEXT_CURRENCY , $ storeCurrency , null );
565
+
566
+ // Emulate store as required to ensure Magento internals use correct store currency
567
+ $ this ->appEmulation ->startEnvironmentEmulation (
568
+ $ storeId ,
569
+ AppArea::AREA_FRONTEND ,
570
+ true
571
+ );
545
572
546
- // Add pricing data to the product data array
547
- if ($ price = $ prices ->get (RegularPrice::PRICE_CODE )->getValue ()) {
573
+ $ priceInfo = $ product ->getPriceInfo ();
574
+
575
+ // Add regular price data to the product data array
576
+ if ($ price = $ priceInfo ->getPrice (RegularPrice::PRICE_CODE )->getAmount ()->getValue ()) {
548
577
$ productData [OmetriaProductInterface::PRICE ] = $ price ;
549
578
}
550
579
551
- if ($ specialPrice = $ prices ->get (SpecialPrice::PRICE_CODE )->getValue ()) {
580
+ // Add special price data to the product data array
581
+ if ($ specialPrice = $ priceInfo ->getPrice (SpecialPrice::PRICE_CODE )->getAmount ()->getValue ()) {
552
582
$ productData [OmetriaProductInterface::SPECIAL_PRICE ] = $ specialPrice ;
553
583
}
554
584
555
- if ($ finalPrice = $ prices ->get (FinalPrice::PRICE_CODE )->getValue ()) {
585
+ // Add final price data to the product data array (this is currency converted internally)
586
+ if ($ finalPrice = $ priceInfo ->getPrice (FinalPrice::PRICE_CODE )->getAmount ()->getValue ()) {
556
587
$ productData [OmetriaProductInterface::FINAL_PRICE ] = $ finalPrice ;
557
588
}
558
589
559
590
$ taxDetailsItem = $ this ->getTaxDetails (
560
- $ product
591
+ $ product ,
592
+ $ finalPrice
561
593
);
562
594
563
595
$ productData [OmetriaProductInterface::TAX_AMOUNT ] = $ taxDetailsItem ->getRowTax ();
564
596
$ productData [OmetriaProductInterface::FINAL_PRICE_INCL_TAX ] = $ taxDetailsItem ->getPriceInclTax ();
597
+
598
+ // Stop emulating store
599
+ $ this ->appEmulation ->stopEnvironmentEmulation ();
600
+
601
+ // Reset HTTP currency value to before value
602
+ $ this ->httpContext ->setValue (HttpContext::CONTEXT_CURRENCY , $ beforeCurrency , $ beforeCurrency );
565
603
}
566
604
567
605
/**
568
606
* @param $product
607
+ * @param $finalPrice
569
608
* @return TaxDetailsItemInterface
570
609
*/
571
- public function getTaxDetails ($ product )
610
+ public function getTaxDetails ($ product, $ finalPrice )
572
611
{
573
612
$ priceIncludesTax = $ this ->taxConfig ->priceIncludesTax ($ product ->getStoreId ());
574
613
@@ -582,7 +621,7 @@ public function getTaxDetails($product)
582
621
->setTaxClassKey ($ taxClassKey )
583
622
->setIsTaxIncluded ($ priceIncludesTax )
584
623
->setType ('product ' )
585
- ->setUnitPrice ($ product -> getFinalPrice () );
624
+ ->setUnitPrice ($ finalPrice );
586
625
587
626
$ quoteDetails = $ this ->quoteDetailsFactory ->create ();
588
627
$ quoteDetails ->setItems ([$ item ]);
@@ -598,13 +637,13 @@ public function getTaxDetails($product)
598
637
* @param $storeId
599
638
* @return string
600
639
*/
601
- private function getStoreCurrency ($ storeId )
640
+ private function getStoreDefaultCurrency ($ storeId )
602
641
{
603
642
if (!isset ($ this ->storeCurrencies [$ storeId ])) {
604
643
$ stores = $ this ->storeManager ->getStores ();
605
644
606
645
foreach ($ stores as $ store ) {
607
- $ this ->storeCurrencies [$ storeId ] = $ store ->getDefaultCurrency ()->getCode ();
646
+ $ this ->storeCurrencies [$ store -> getId () ] = $ store ->getDefaultCurrency ()->getCode ();
608
647
}
609
648
}
610
649
0 commit comments