Skip to content

Commit 354b0ff

Browse files
authored
Release/2.1.1 (#25)
* M2-30 Fix bug in currency code caching, ensure prices are converted to correct currency. * M2-28 Default to trying to use the configurable product image regardless of UseConfigurableImage config. * M2-34 Fix case on return variable name. * M2-37 Fix issue with pricing for configurables caused by Magento internal caching of price info per store. * Remove redundant DI class.
1 parent f15ee81 commit 354b0ff

File tree

7 files changed

+58
-19
lines changed

7 files changed

+58
-19
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?xml version="1.0"?>
2-
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"><module name="Ometria_AbandonedCarts" setup_version="2.1.0"/></config>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"><module name="Ometria_AbandonedCarts" setup_version="2.1.1"/></config>
33

app/code/Ometria/Api/Controller/V2/Products.php

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
1818
use Magento\Framework\App\Action\Action;
1919
use Magento\Framework\App\Action\Context;
20+
use Magento\Framework\App\Area as AppArea;
21+
use Magento\Framework\App\Http\Context as HttpContext;
2022
use Magento\Framework\Controller\ResultFactory;
2123
use Magento\Framework\Controller\ResultInterface;
2224
use Magento\Framework\Exception\NoSuchEntityException;
25+
use Magento\Store\Model\App\Emulation as AppEmulation;
2326
use Magento\Store\Model\StoreManagerInterface;
2427
use Magento\Tax\Api\Data\QuoteDetailsInterfaceFactory;
2528
use Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory;
@@ -83,6 +86,12 @@ class Products extends Action
8386
/** @var TaxCalculationInterface */
8487
private $taxCalculationService;
8588

89+
/** @var HttpContext */
90+
private $httpContext;
91+
92+
/** @var AppEmulation */
93+
private $appEmulation;
94+
8695
/** @var array */
8796
private $productCollections = [];
8897

@@ -124,6 +133,8 @@ class Products extends Action
124133
* @param QuoteDetailsInterfaceFactory $quoteDetailsFactory
125134
* @param QuoteDetailsItemInterfaceFactory $quoteDetailsItemFactory
126135
* @param TaxCalculationInterface $taxCalculationService
136+
* @param HttpContext $httpContext
137+
* @param AppEmulation $appEmulation
127138
*/
128139
public function __construct(
129140
Context $context,
@@ -141,7 +152,9 @@ public function __construct(
141152
TaxConfig $taxConfig,
142153
QuoteDetailsInterfaceFactory $quoteDetailsFactory,
143154
QuoteDetailsItemInterfaceFactory $quoteDetailsItemFactory,
144-
TaxCalculationInterface $taxCalculationService
155+
TaxCalculationInterface $taxCalculationService,
156+
HttpContext $httpContext,
157+
AppEmulation $appEmulation
145158
) {
146159
parent::__construct($context);
147160

@@ -160,6 +173,8 @@ public function __construct(
160173
$this->quoteDetailsFactory = $quoteDetailsFactory;
161174
$this->quoteDetailsItemFactory = $quoteDetailsItemFactory;
162175
$this->taxCalculationService = $taxCalculationService;
176+
$this->httpContext = $httpContext;
177+
$this->appEmulation = $appEmulation;
163178
}
164179

165180
/**
@@ -519,7 +534,7 @@ private function getListings(ProductInterface $product)
519534
OmetriaProductInterface::STORE_ID => $storeId,
520535
OmetriaProductInterface::TITLE => $productInStore->getName(),
521536
OmetriaProductInterface::URL => $this->getProductUrl($productInStore),
522-
OmetriaProductInterface::STORE_CURRENCY => $this->getStoreCurrency($storeId),
537+
OmetriaProductInterface::STORE_CURRENCY => $this->getStoreDefaultCurrency($storeId),
523538
OmetriaProductInterface::VISIBILITY => (int) $productInStore->getVisibility(),
524539
OmetriaProductInterface::STATUS => (int) $productInStore->getStatus(),
525540
OmetriaProductInterface::IMAGE_URL => $this->getImageUrl($productInStore)
@@ -541,34 +556,58 @@ private function getListings(ProductInterface $product)
541556
*/
542557
private function appendProductPriceData(&$productData, ProductInterface $product)
543558
{
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+
);
545572

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()) {
548577
$productData[OmetriaProductInterface::PRICE] = $price;
549578
}
550579

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()) {
552582
$productData[OmetriaProductInterface::SPECIAL_PRICE] = $specialPrice;
553583
}
554584

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()) {
556587
$productData[OmetriaProductInterface::FINAL_PRICE] = $finalPrice;
557588
}
558589

559590
$taxDetailsItem = $this->getTaxDetails(
560-
$product
591+
$product,
592+
$finalPrice
561593
);
562594

563595
$productData[OmetriaProductInterface::TAX_AMOUNT] = $taxDetailsItem->getRowTax();
564596
$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);
565603
}
566604

567605
/**
568606
* @param $product
607+
* @param $finalPrice
569608
* @return TaxDetailsItemInterface
570609
*/
571-
public function getTaxDetails($product)
610+
public function getTaxDetails($product, $finalPrice)
572611
{
573612
$priceIncludesTax = $this->taxConfig->priceIncludesTax($product->getStoreId());
574613

@@ -582,7 +621,7 @@ public function getTaxDetails($product)
582621
->setTaxClassKey($taxClassKey)
583622
->setIsTaxIncluded($priceIncludesTax)
584623
->setType('product')
585-
->setUnitPrice($product->getFinalPrice());
624+
->setUnitPrice($finalPrice);
586625

587626
$quoteDetails = $this->quoteDetailsFactory->create();
588627
$quoteDetails->setItems([$item]);
@@ -598,13 +637,13 @@ public function getTaxDetails($product)
598637
* @param $storeId
599638
* @return string
600639
*/
601-
private function getStoreCurrency($storeId)
640+
private function getStoreDefaultCurrency($storeId)
602641
{
603642
if (!isset($this->storeCurrencies[$storeId])) {
604643
$stores = $this->storeManager->getStores();
605644

606645
foreach ($stores as $store) {
607-
$this->storeCurrencies[$storeId] = $store->getDefaultCurrency()->getCode();
646+
$this->storeCurrencies[$store->getId()] = $store->getDefaultCurrency()->getCode();
608647
}
609648
}
610649

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<?xml version="1.0"?>
2-
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"><module name="Ometria_Api" setup_version="2.1.0"/></config>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"><module name="Ometria_Api" setup_version="2.1.1"/></config>

app/code/Ometria/Core/Helper/Product.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ public function getPreferredProductImageUrlV2(ProductInterface $product, $imageI
228228
return $this->getProductImageUrlV2($preferredProduct, $imageId);
229229
}
230230

231-
// No valid preferred product image could be found
232-
return null;
231+
// Default to trying to use the configurable product's image regardless of UseConfigurableImage config
232+
return $this->getProductImageUrlV2($product, $imageId);
233233
}
234234

235235
/**
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<?xml version="1.0"?>
2-
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"><module name="Ometria_Core" setup_version="2.1.0"/></config>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"><module name="Ometria_Core" setup_version="2.1.1"/></config>

app/code/Ometria/Core/view/frontend/web/js/configurable-mixin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ define([
1919
}
2020
});
2121

22-
return $.mage.Configurable;
22+
return $.mage.configurable;
2323
}
2424
});

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ometria/magento2",
33
"type": "magento2-module",
4-
"version": "2.1.0",
4+
"version": "2.1.1",
55
"description": "Dev composer package for Ometria Extension",
66
"authors": [
77
{

0 commit comments

Comments
 (0)