Skip to content

Commit 818cba3

Browse files
authored
Merge pull request #15 from Ometria/2.0/feature/multi-currency
Add stock to Product API and fix multi currency bug
2 parents d7d02d7 + f3ea4fd commit 818cba3

File tree

5 files changed

+77
-36
lines changed

5 files changed

+77
-36
lines changed

app/code/Ometria/AbandonedCarts/etc/module.xml

100644100755
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_AbandonedCarts" setup_version="2.0.1"/></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.0.2"/></config>

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

Lines changed: 73 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<?php
22
namespace Ometria\Api\Controller\V1;
3+
4+
use Magento\CatalogInventory\Api\StockRegistryInterface;
5+
use Magento\Framework\Exception\LocalizedException;
36
use Ometria\Api\Helper\Format\V1\Products as Helper;
4-
use \Ometria\Api\Controller\V1\Base;
7+
use Ometria\Api\Controller\V1\Base;
8+
59
class Products extends Base
610
{
711
const PRODUCT_TYPE_IDX = 'magento_product_type';
@@ -28,6 +32,9 @@ class Products extends Base
2832
protected $directoryHelper;
2933
protected $storeUrlHelper;
3034

35+
/** @var StockRegistryInterface */
36+
private $stockRegistry;
37+
3138
protected $storeIdCache=false;
3239
protected $productTypeFactory;
3340

@@ -64,7 +71,8 @@ public function __construct(
6471
\Magento\Framework\App\ResourceConnection $resourceConnection,
6572
\Magento\Directory\Helper\Data $directoryHelper,
6673
\Ometria\Api\Helper\StoreUrl $storeUrlHelper,
67-
\Magento\Catalog\Model\Product\TypeFactory $productTypeFactory
74+
\Magento\Catalog\Model\Product\TypeFactory $productTypeFactory,
75+
StockRegistryInterface $stockRegistry
6876
) {
6977
parent::__construct($context);
7078
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
@@ -86,6 +94,7 @@ public function __construct(
8694
$this->directoryHelper = $directoryHelper;
8795
$this->storeUrlHelper = $storeUrlHelper;
8896
$this->productTypeFactory = $productTypeFactory;
97+
$this->stockRegistry = $stockRegistry;
8998
}
9099

91100
protected function getArrayKey($array, $key)
@@ -159,6 +168,7 @@ protected function serializeItem($item)
159168
}
160169

161170
$tmp = $this->appendPricing($tmp['id'], $tmp);
171+
$tmp = $this->appendStock($tmp['id'], $tmp);
162172

163173
if (isset($item['store_listings'])) {
164174
$tmp['store_listings'] = $item['store_listings'];
@@ -402,69 +412,100 @@ protected function getProductListingsForStore(
402412
'image_url' => $this->getBaseImageUrl() . $this->getCustomAttribute($item,$this->getImageUrlKey())
403413
);
404414

405-
$tmp = $this->appendPricing($id, $tmp, $store_currency, $base_currency);
415+
$tmp = $this->appendPricing($id, $tmp, $storeId, $base_currency, $store_currency);
406416

407417
$store_listings[$id][$storeId] = $tmp;
408418
}
409419

410420
return $store_listings;
411421
}
412422

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+
{
415425
$store_price = $this->getProductPrice(
416426
$product_id,
417-
$item,
427+
$storeId,
418428
\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+
}
421436

422437
$store_special_price = $this->getProductPrice(
423438
$product_id,
424-
$item,
439+
$storeId,
425440
\Magento\Catalog\Pricing\Price\SpecialPrice::PRICE_CODE,
426-
$store_currency,
427-
$base_currency);
441+
$base_currency,
442+
$store_currency
443+
);
428444

429-
$item['price'] = $store_price;
445+
if ($store_special_price) {
446+
$item['special_price'] = $store_special_price;
447+
}
430448

431449
if($this->_request->getParam('final_price') === 'true') {
432450
$store_final_price = $this->getProductPrice(
433451
$product_id,
434-
$item,
452+
$storeId,
435453
\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
436-
$store_currency,
437-
$base_currency);
454+
$base_currency,
455+
$store_currency
456+
);
438457

439-
$item['final_price'] = $store_final_price;
458+
if ($store_final_price) {
459+
$item['final_price'] = $store_final_price;
460+
}
440461
}
441462

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'];
446483
}
447484

448485
return $item;
449486
}
450487

451-
452488
protected function getProductPrice(
453489
$product_id,
454-
$item,
490+
$storeId,
455491
$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);
461496

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
467505
);
506+
} catch (\Exception $e) {
507+
// Allow the "undefined rate" exception and return the price as is if no rate has been setup.
508+
}
468509
}
469510

470511
return $price;

app/code/Ometria/Api/etc/module.xml

100644100755
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.0.1"/></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.0.2"/></config>

app/code/Ometria/Core/etc/module.xml

100644100755
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.0.1"/></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.0.2"/></config>

composer.json

100644100755
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.0.1",
4+
"version": "2.0.2",
55
"description": "Dev composer package for Ometria Extension",
66
"authors": [
77
{

0 commit comments

Comments
 (0)