Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
JsonProductInfo.php
Go to the documentation of this file.
1 <?php
7 
12 use Magento\Review\Model\ReviewFactory;
13 use Magento\Review\Model\RatingFactory;
17 
19 {
23  protected $productRepository;
24 
32  public function __construct(
33  Context $context,
35  ReviewFactory $reviewFactory,
36  RatingFactory $ratingFactory,
38  ) {
39  $this->productRepository = $productRepository;
40  parent::__construct($context, $coreRegistry, $reviewFactory, $ratingFactory);
41  }
42 
46  public function execute()
47  {
48  $response = new DataObject();
49  $id = $this->getRequest()->getParam('id');
50  if (intval($id) > 0) {
51  $product = $this->productRepository->getById($id);
52  $response->setId($id);
53  $response->addData($product->getData());
54  $response->setError(0);
55  } else {
56  $response->setError(1);
57  $response->setMessage(__('We can\'t retrieve the product ID.'));
58  }
60  $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
61  $resultJson->setData($response->toArray());
62  return $resultJson;
63  }
64 }
$response
Definition: 404.php:11
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
__construct(Context $context, Registry $coreRegistry, ReviewFactory $reviewFactory, RatingFactory $ratingFactory, ProductRepositoryInterface $productRepository)