Skip to content

Commit

Permalink
2.2.6 - fix products api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bnayalivne committed Jul 3, 2017
1 parent d4dec05 commit e13d797
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Api/Data/ProductCollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ interface ProductCollectionInterface
/**
* Get rules.
*
* @return \Remarkety\Mgconnector\Api\Data\ProductInterface
* @return \Remarkety\Mgconnector\Api\Data\ProductInterface[]
*/
public function getProducts();

/**
* Set rules .
*
* @param \Remarkety\Mgconnector\Api\Data\ProductInterface $items
* @param \Remarkety\Mgconnector\Api\Data\ProductInterface[] $items
* @return $this
*/
public function setProducts($items = null);
}
}
50 changes: 48 additions & 2 deletions Model/Api/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
use Remarkety\Mgconnector\Model\ResourceModel\Queue\Collection;
use Remarkety\Mgconnector\Observer\EventMethods;
use Remarkety\Mgconnector\Helper\Data as DataHelper;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Catalog\Model\ProductRepository;
use Magento\CatalogInventory\Api\StockRegistryInterface;

class Data implements DataInterface
{
Expand Down Expand Up @@ -60,6 +63,8 @@ class Data implements DataInterface
protected $resourceConfig;
protected $cacheTypeList;
protected $dataHelper;
protected $productRepository;
protected $stockRegistry;

protected $response_mask = [
'products' => [
Expand Down Expand Up @@ -309,7 +314,9 @@ public function __construct(ProductFactory $productFactory,
Collection $queueCollection,
QueueRepository $queueRepository,
EventMethods $eventMethods,
DataHelper $dataHelper
DataHelper $dataHelper,
ProductRepository $productRepository,
StockRegistryInterface $stockRegistry
)
{
$this->dataHelper = $dataHelper;
Expand Down Expand Up @@ -344,6 +351,8 @@ public function __construct(ProductFactory $productFactory,
$this->_salesOrderResourceCollectionFactory = $salesOrderResourceCollectionFactory;
$this->ruleFactory = $ruleFactory;
$this->couponFactory = $couponFactory;
$this->stockRegistry = $stockRegistry;
$this->productRepository = $productRepository;
}

/**
Expand Down Expand Up @@ -470,13 +479,50 @@ public function getProducts(
$prod['title'] = $row->getName();
}

$variants = [];
if($row->getTypeId() == Configurable::TYPE_CODE){
//configurable products sends variants
$childrenIdsGroups = $this->_catalogProductTypeConfigurable->getChildrenIds($row->getId());
if(isset($childrenIdsGroups[0])) {
$childrenIds = $childrenIdsGroups[0];
foreach ($childrenIds as $childId) {
$childProd = $this->loadProduct($childId);
$stock = $this->stockRegistry->getStockItem($childId);

$created_at_child = new \DateTime($childProd->getCreatedAt());
$updated_at_child = new \DateTime($childProd->getUpdatedAt());

$variants[] = [
'id' => $childProd->getId(),
'sku' => $childProd->getSku(),
'title' => $childProd->getName(),
'created_at' => $created_at_child->format(\DateTime::ATOM),
'updated_at' => $updated_at_child->format(\DateTime::ATOM),
'inventory_quantity' => $stock->getQty(),
'price' => (float)$childProd->getPrice()
];
}
}
} else {
$stock = $this->stockRegistry->getStockItem($row->getId());
$variants[] = [
'inventory_quantity' => $stock->getQty(),
'price' => (float)$row->getPrice()
];
}
$prod['variants'] = $variants;

$productsArray[] = $prod;
}
$object = new DataObject();
$object->setProducts($productsArray);
return $object;
}

private function loadProduct($product_id){
return $this->productRepository->getById($product_id);
}

public function getCategory($category_id)
{
if (!isset($this->categoryMapCache[$category_id])) {
Expand Down Expand Up @@ -1160,7 +1206,7 @@ public function setConfig($mage_store_id, $configName, $scope, $newValue)
*/
public function getVersion()
{
return '2.2.5';
return '2.2.6';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lib-libxml": "*"
},
"type": "magento2-module",
"version": "2.2.5",
"version": "2.2.6",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Remarkety_Mgconnector" setup_version="2.2.5">
<module name="Remarkety_Mgconnector" setup_version="2.2.6">
</module>
</config>

0 comments on commit e13d797

Please sign in to comment.