Skip to content

Commit 430aa27

Browse files
author
Dylan Nykolyszyn
committed
Magento 2 API
1 parent 0b4a3eb commit 430aa27

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

Controller/Index/API.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
namespace Reviewscouk\Reviews\Controller\Index;
4+
5+
use Magento\Framework as Framework;
6+
use Magento\Catalog as Catalog;
7+
use Magento\CatalogInventory as CatalogInventory;
8+
use Magento\Store as Store;
9+
use Reviewscouk\Reviews as Reviews;
10+
11+
class API extends Framework\App\Action\Action
12+
{
13+
14+
private $configHelper;
15+
private $cache;
16+
private $productModel;
17+
private $stockModel;
18+
private $imageHelper;
19+
private $storeModel;
20+
21+
public function __construct(
22+
Framework\App\Action\Context $context,
23+
Framework\Cache\Core $core,
24+
Catalog\Model\Product $product,
25+
CatalogInventory\Api\StockRegistryInterface $stockRegistryInterface,
26+
Catalog\Helper\Image $image,
27+
Store\Model\StoreManagerInterface $storeManagerInterface,
28+
Reviews\Helper\Config $config,
29+
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
30+
)
31+
{
32+
parent::__construct($context);
33+
34+
$this->configHelper = $config;
35+
$this->cache = $core;
36+
$this->productModel = $product;
37+
$this->stockModel = $stockRegistryInterface;
38+
$this->imageHelper = $image;
39+
$this->storeModel = $storeManagerInterface;
40+
$this->productCollectionFactory = $productCollectionFactory;
41+
}
42+
43+
private function getProductCollection($page)
44+
{
45+
$collection = $this->productCollectionFactory->create();
46+
/* Addtional */
47+
$collection
48+
->addMinimalPrice()
49+
->addFinalPrice()
50+
->addTaxPercents()
51+
->addAttributeToSelect('*')
52+
->addUrlRewrite()
53+
->setPageSize(100)
54+
->setCurPage($page);
55+
return $collection;
56+
}
57+
58+
public function execute()
59+
{
60+
ob_start();
61+
set_time_limit(0);
62+
63+
$store = $this->storeModel->getStore();
64+
$productFeedEnabled = $this->configHelper->isProductFeedEnabled($store->getId());
65+
$auth['actual_key'] = $this->configHelper->getApiKey($store->getId());
66+
67+
if ($productFeedEnabled) {
68+
69+
$auth['page'] = !empty($_GET['page']) ? $_GET['page'] : '1';
70+
$auth['submitted_key'] = !empty($_GET['key']) ? $_GET['key'] : '';
71+
72+
//Authenticate
73+
if(!isset($auth['submitted_key'], $auth['actual_key']) || $auth['actual_key'] != $auth['submitted_key']) {
74+
echo json_encode(array('success' => false, 'message' => 'Unauthenticated.'));
75+
die();
76+
}
77+
78+
$products = $this->getProductCollection($auth['page']);
79+
80+
$collection = [];
81+
82+
foreach ($products as $product) {
83+
// Load image url via helper.
84+
$imageUrl = $this->imageHelper->init($product, 'product_page_image_large')->getUrl();
85+
$brand = $product->hasData('manufacturer') ? $product->getAttributeText('manufacturer') : ($product->hasData('brand') ? $product->getAttributeText('brand') : 'Not Available');
86+
$price = $product->getPrice();
87+
88+
$finalPrice = $product->getFinalPrice();
89+
90+
$item = [
91+
'id' => $product->getSku(),
92+
'title' => $product->getName(),
93+
'link' => $product->getProductUrl(),
94+
'price' => number_format($price, 2) . " " . $store->getCurrentCurrency()->getCode(),
95+
'sale_price' => number_format($finalPrice, 2) . " " . $store->getCurrentCurrency()->getCode(),
96+
'image_link' => $imageUrl,
97+
'brand' => $brand,
98+
'mpn' => ($product->hasData('mpn') ? $product->getData('mpn') : $product->getSku()),
99+
'gtin' => ($product->hasData('gtin') ? $product->getData('gtin') : ''),
100+
'product_type' => $product->getTypeID(),
101+
];
102+
103+
$item['category'] = [];
104+
105+
$categoryCollection = $product->getCategoryCollection();
106+
if (count($categoryCollection) > 0) {
107+
foreach ($categoryCollection as $category) {
108+
$item['category'][] = $category->getName();
109+
}
110+
}
111+
112+
$stock = $this->stockModel->getStockItem(
113+
$product->getId(),
114+
$product->getStore()->getWebsiteId()
115+
);
116+
117+
$item['in_stock'] = $stock->getIsInStock() ? true : false;
118+
119+
$collection[] = $item;
120+
}
121+
122+
echo json_encode(array('success' => true, 'products' => $collection, 'total' => count($collection)));
123+
die();
124+
125+
} else {
126+
echo json_encode(array('success' => false, 'message' => 'API disabled.'));
127+
die();
128+
}
129+
}
130+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ You'll need to sign up at [Reviews.co.uk](https://www.reviews.co.uk "Reviews.co.
99
2. As this plugin is hosted on [packagist.org](http://packagist.org), you simply use the following to instruct composer to fetch and install the module:
1010

1111
```bash
12-
composer require reviewscouk/reviews:0.0.36
12+
composer require reviewscouk/reviews:0.0.37
1313
```
1414

1515
3. When this is complete, `cd` to `/bin` and run the following:

0 commit comments

Comments
 (0)