1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Review\Controller\Adminhtml\Product;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Review\Controller\Adminhtml\Product as ProductController;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Magento\Review\Model\ReviewFactory;
use Magento\Review\Model\RatingFactory;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\DataObject;
use Magento\Framework\Controller\ResultFactory;
/**
* Represents product info in json
*/
class JsonProductInfo extends ProductController implements HttpGetActionInterface
{
/**
* @var \Magento\Catalog\Api\ProductRepositoryInterface
*/
protected $productRepository;
/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Registry $coreRegistry
* @param \Magento\Review\Model\ReviewFactory $reviewFactory
* @param \Magento\Review\Model\RatingFactory $ratingFactory
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
*/
public function __construct(
Context $context,
Registry $coreRegistry,
ReviewFactory $reviewFactory,
RatingFactory $ratingFactory,
ProductRepositoryInterface $productRepository
) {
$this->productRepository = $productRepository;
parent::__construct($context, $coreRegistry, $reviewFactory, $ratingFactory);
}
/**
* Execute controller
*
* @return \Magento\Framework\Controller\Result\Json
*/
public function execute()
{
$response = new DataObject();
$id = $this->getRequest()->getParam('id');
if ((int)$id > 0) {
$product = $this->productRepository->getById($id);
$response->setId($id);
$response->addData($product->getData());
$response->setError(0);
} else {
$response->setError(1);
$response->setMessage(__('We can\'t retrieve the product ID.'));
}
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$resultJson->setData($response->toArray());
return $resultJson;
}
}