Skip to content

Commit 0704dac

Browse files
authored
New release/2.2.1 (#28)
* Added base currency values to Orders API response. * Use correct value for billing address name (was previously using shipping address name). * Cast string to float to ensure discount line is formatted correctly when greater than zero but less than one. * Add base currency values to line items in Orders API response. * 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-23 Move inventory access to independent service to allow MSI with backwards compatibility, used in ometria.raw_data and Product API V2. * Return boolean for Product API V2 is_in_stock value. * Add "reward_points" value to customer API response for Magento Commerce merchants. * M2-37 Fix issue with pricing for configurables caused by Magento internal caching of price info per store. * M2-37 Fix issue with pricing for configurables caused by Magento internal caching of price info per store. * M2-37 Fix issue with pricing for configurables caused by Magento internal caching of price info per store. * Remove redundant DI class. * Bump versions. * M2-38 Added config to enable/disable additional Ometria Page Views on variant selection within configurable/swatch PDP. * Bump composer version. * Remove whitespace. * M2-42 Fix type error in Product Inventory Service.
1 parent 6bdbd9d commit 0704dac

File tree

11 files changed

+346
-180
lines changed

11 files changed

+346
-180
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3-
<module name="Ometria_AbandonedCarts" setup_version="2.2.0"/>
3+
<module name="Ometria_AbandonedCarts" setup_version="2.2.1"/>
44
</config>

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

Lines changed: 74 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33

44
use Magento\Catalog\Api\Data\ProductInterface;
55
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;
69
use Magento\CatalogInventory\Api\StockRegistryInterface;
10+
use Magento\Framework\App\Area as AppArea;
11+
use Magento\Framework\App\Http\Context as HttpContext;
712
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Store\Model\App\Emulation as AppEmulation;
814
use Ometria\Api\Helper\Filter\V1\Service as FilterService;
915
use Ometria\Api\Helper\Format\V1\Products as Helper;
1016

@@ -36,6 +42,12 @@ class Products extends Base
3642
/** @var StockRegistryInterface */
3743
private $stockRegistry;
3844

45+
/** @var HttpContext */
46+
private $httpContext;
47+
48+
/** @var AppEmulation */
49+
private $appEmulation;
50+
3951
protected $storeIdCache=false;
4052
protected $productTypeFactory;
4153

@@ -72,7 +84,9 @@ public function __construct(
7284
\Magento\Directory\Helper\Data $directoryHelper,
7385
\Ometria\Api\Helper\StoreUrl $storeUrlHelper,
7486
\Magento\Catalog\Model\Product\TypeFactory $productTypeFactory,
75-
StockRegistryInterface $stockRegistry
87+
StockRegistryInterface $stockRegistry,
88+
HttpContext $httpContext,
89+
AppEmulation $appEmulation
7690
) {
7791
parent::__construct($context);
7892
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
@@ -94,6 +108,8 @@ public function __construct(
94108
$this->storeUrlHelper = $storeUrlHelper;
95109
$this->productTypeFactory = $productTypeFactory;
96110
$this->stockRegistry = $stockRegistry;
111+
$this->httpContext = $httpContext;
112+
$this->appEmulation = $appEmulation;
97113
}
98114

99115
public function execute()
@@ -374,7 +390,7 @@ protected function addStoreListingToItems($items)
374390
}
375391

376392

377-
protected function getProductListingsForStore($store, $productIds, $store_listings)
393+
protected function getProductListingsForStore($store, $productIds, $storeListings)
378394
{
379395
$storeId = $store->getId();
380396

@@ -392,8 +408,8 @@ protected function getProductListingsForStore($store, $productIds, $store_listin
392408
$this->getRequest()->getParam('product_image', 'image')
393409
);
394410

395-
$base_currency = $store->getBaseCurrency()->getCode();
396-
$store_currency = $store->getDefaultCurrency()->getCode();
411+
$baseCurrency = $store->getBaseCurrency()->getCode();
412+
$storeCurrency = $store->getDefaultCurrency()->getCode();
397413

398414
foreach ($items as $item) {
399415
$id = $item['id'];
@@ -402,57 +418,57 @@ protected function getProductListingsForStore($store, $productIds, $store_listin
402418
'store_id' => $storeId,
403419
'title' => $item['name'],
404420
'url' => $url,
405-
'store_currency' => $store_currency,
421+
'store_currency' => $storeCurrency,
406422
'visibility' => $item['visibility'],
407423
'status' => $item['status'],
408424
'image_url' => $item['image_url']
409-
);
425+
);
410426

411-
$tmp = $this->appendPricing($id, $tmp, $storeId, $base_currency, $store_currency);
427+
$tmp = $this->appendPricing($id, $tmp, $storeId, $baseCurrency, $storeCurrency);
412428

413-
$store_listings[$id][$storeId] = $tmp;
429+
$storeListings[$id][$storeId] = $tmp;
414430
}
415431

416-
return $store_listings;
432+
return $storeListings;
417433
}
418434

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)
420436
{
421-
$store_price = $this->getProductPrice(
422-
$product_id,
437+
$storePrice = $this->getProductPrice(
438+
$productId,
423439
$storeId,
424-
\Magento\Catalog\Pricing\Price\RegularPrice::PRICE_CODE,
425-
$base_currency,
426-
$store_currency
440+
RegularPrice::PRICE_CODE,
441+
$baseCurrency,
442+
$storeCurrency
427443
);
428444

429-
if ($store_price) {
430-
$item['price'] = $store_price;
445+
if ($storePrice) {
446+
$item['price'] = $storePrice;
431447
}
432448

433-
$store_special_price = $this->getProductPrice(
434-
$product_id,
449+
$storeSpecialPrice = $this->getProductPrice(
450+
$productId,
435451
$storeId,
436-
\Magento\Catalog\Pricing\Price\SpecialPrice::PRICE_CODE,
437-
$base_currency,
438-
$store_currency
452+
SpecialPrice::PRICE_CODE,
453+
$baseCurrency,
454+
$storeCurrency
439455
);
440456

441-
if ($store_special_price) {
442-
$item['special_price'] = $store_special_price;
457+
if ($storeSpecialPrice) {
458+
$item['special_price'] = $storeSpecialPrice;
443459
}
444460

445461
if ($this->_request->getParam('final_price') === 'true') {
446-
$store_final_price = $this->getProductPrice(
447-
$product_id,
462+
$storeFinalPrice = $this->getProductPrice(
463+
$productId,
448464
$storeId,
449-
\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
450-
$base_currency,
451-
$store_currency
465+
FinalPrice::PRICE_CODE,
466+
$baseCurrency,
467+
$storeCurrency
452468
);
453469

454-
if ($store_final_price) {
455-
$item['final_price'] = $store_final_price;
470+
if ($storeFinalPrice) {
471+
$item['final_price'] = $storeFinalPrice;
456472
}
457473
}
458474

@@ -482,28 +498,46 @@ private function appendStock($productId, $item)
482498
}
483499

484500
protected function getProductPrice(
485-
$product_id,
501+
$productId,
486502
$storeId,
487-
$price_code,
488-
$base_currency = null,
489-
$store_currency = null
503+
$priceCode,
504+
$baseCurrency = null,
505+
$storeCurrency = null
490506
) {
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);
492510

493-
$price = $product->getPriceInfo()->getPrice($price_code)->getValue();
511+
$product = $this->productRepository->getById($productId, false, $storeId);
494512

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) {
496524
try {
497525
$price = $this->directoryHelper->currencyConvert(
498526
$price,
499-
$base_currency,
500-
$store_currency
527+
$baseCurrency,
528+
$storeCurrency
501529
);
502530
} catch (\Exception $e) {
503531
// Allow the "undefined rate" exception and return the price as is if no rate has been setup.
504532
}
505533
}
506534

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+
507541
return $price;
508542
}
509543

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ private function appendProductPriceData(&$productData, ProductInterface $product
569569
$productData[OmetriaProductInterface::FINAL_PRICE] = $finalPrice;
570570
}
571571

572+
// Add tax data to the product data array (this is currency converted internally)
572573
$taxDetailsItem = $this->getTaxDetails(
573574
$product,
574575
$finalPrice
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3-
<module name="Ometria_Api" setup_version="2.2.0"/>
3+
<module name="Ometria_Api" setup_version="2.2.1"/>
44
</config>
5-

0 commit comments

Comments
 (0)