Here's my situation: When calling the endpoint, I receive the following data:
{
"ean": "0744963634260",
"price": "400.000000",
"update_at": "2023-10-30 18:39:02"
}
The data is output as a JSON object.
My problems:
- I need to ensure the data is output under the "products" element. For example:
{
"products": [
{
"ean": "0744963634260",
"price": "400.000000",
"update_at": "2023-10-30 18:39:02"
}
]
}
- I'm unable to output multiple elements.
My code:
app\code\Dev\RestApi\Model\Data\Product.php:
namespace Dev\RestApi\Model\Data;
use Dev\RestApi\Api\Data\ProductInterface;
class Product implements ProductInterface
{
private $products = [];
private $ean;
private $price;
private $updateAt;
public function getProducts(): array
{
return $this->products;
}
public function setProducts(array $products)
{
$this->products = $products;
}
public function getEan(): ?string
{
return $this->ean;
}
public function setEan(?string $ean)
{
$this->ean = $ean;
}
public function getPrice(): ?string
{
return $this->price;
}
public function setPrice(?string $price)
{
$this->price = $price;
}
public function getUpdateAt(): ?string
{
return $this->updateAt;
}
public function setUpdateAt(?string $updateAt)
{
$this->updateAt = $updateAt;
}
}
app\code\Dev\RestApi\Api\Data\ProductInterface.php:
namespace Dev\RestApi\Api\Data;
interface ProductInterface
{
/**
* Get the array of products.
*
* @return array
*/
public function getProducts(): array;
/**
* Get ean of the product.
*
* @return string|null
*/
public function getEan(): ?string;
/**
* Get price of the product.
*
* @return string|null
*/
public function getPrice(): ?string;
/**
* Get update time of the product.
*
* @return string|null
*/
public function getUpdateAt(): ?string;
}
app\code\Dev\RestApi\Model\Api\ProductRepository.php:
namespace Dev\RestApi\Model\Api;
use Dev\RestApi\Api\ProductRepositoryInterface;
use Dev\RestApi\Api\Data\ProductInterface;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\JsonFactory;
/**
* Class ProductRepository
*/
class ProductRepository implements ProductRepositoryInterface
{
// A lot of variables
/**
* @var \Magento\Framework\Controller\Result\JsonFactory
*/
public function __construct(
CollectionFactory $productCollectionFactory,
StoreManagerInterface $storeManager,
ScopeConfigInterface $scopeConfig,
\Magento\Directory\Model\Country $country,
\Magento\Directory\Model\CountryFactory $countryFactory
) {
$this->productCollectionFactory = $productCollectionFactory;
$this->storeManager = $storeManager;
$this->scopeConfig = $scopeConfig;
$this->_country = $country;
$this->_countryFactory = $countryFactory;
}
public function execute(): ProductInterface
{
$actualToken = $this->scopeConfig->getValue('priceinfo_module/general/token_text',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
$authorizationHeader = $_SERVER['HTTP_AUTHORIZATION'];
if (preg_match('/Bearer\s+(.*)/', $authorizationHeader, $matches)) {
$token = $matches[1];
}
$requestBody = file_get_contents('php://input');
$requestData = json_decode($requestBody, true);
$details = isset($requestData['details']) ? $requestData['details'] : null;
$method = isset($requestData['method']) ? $requestData['method'] : null;
$offset = isset($requestData['offset']) ? $requestData['offset'] : null;
$count = isset($requestData['count']) ? $requestData['count'] : null;
$productsData = [];
if ($method == 'getProducts' && $actualToken == $token) {
$productCollection = $this->productCollectionFactory->create();
$productCollection->addAttributeToSelect([
array('*')
]);
$productCollection->setPageSize($count);
$productCollection->setCurPage($offset);
if ($details == 0) {
foreach ($productCollection as $product) {
$productData = new \Dev\RestApi\Model\Data\Product();
$productData->setEan($product->getEan());
$productData->setPrice($product->getPrice());
$productData->setUpdateAt($product->getUpdatedAt());
// $productsData[] = $productData;
}
}
return $productData;
} else {
return new \Dev\RestApi\Model\Data\Product();
}
}
}
I tried to solve 2 problem by adding the following:
foreach ($productCollection as $product) {
$productData = new \Dev\RestApi\Model\Data\Product();
$productData->setEan($product->getEan());
$productData->setPrice($product->getPrice());
$productData->setUpdateAt($product->getUpdatedAt());
$productsData[] = $productData;
}
But I can't get the array to output exactly that way, since the output is expected to be an object.
Are you able to suggest how my problems can be solved?
Looking forward to hearing from you.