Skip to content

Commit 86867c3

Browse files
committed
Merge branch '2.0/feature/product-stock' into 2.0/feature/multi-currency
2 parents 212dd50 + 6828e1e commit 86867c3

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

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

Lines changed: 33 additions & 2 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'];
@@ -453,6 +463,27 @@ protected function appendPricing($product_id, $item, $storeId = null, $base_curr
453463
return $item;
454464
}
455465

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'];
483+
}
484+
485+
return $item;
486+
}
456487

457488
protected function getProductPrice(
458489
$product_id,

0 commit comments

Comments
 (0)