I have created custom webapi using https://www.demacmedia.com/extending-magento-2-rest-web-api/ This is the code that I am using:
<?php
namespace Panda\WebService\Model\Resource;
use Panda\WebService\Api\WebServiceRepositoryInterface;
use Magento\Catalog\Model\CategoryFactory;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Magento\Framework\App\ResourceConnectionFactory;
use Magento\Customer\Model\CustomerFactory;
use Magento\Customer\Model\Address;
/**
* Class WebServiceRepository
* @package Panda\WebService\Model
*/
class WebServiceRepository implements WebServiceRepositoryInterface
{
/**
* @var ResourceConnectionFactory
*/
protected $_resourceConnection;
/**
* @var ProductCollectionFactory
*/
protected $_productCollection;
/**
* @var CategoryFactory
*/
protected $_category;
/**
* @var CategoryFactory
*/
protected $_customer;
/**
* @var Address
*/
protected $_address;
/**
* @var \Magento\Framework\Json\Helper\Data
*/
protected $_jsonHelper;
/**
* WebServiceRepository constructor.
*
* @param ResourceConnectionFactory $_resourceConnection
*/
public function __construct(
ResourceConnectionFactory $_resourceConnection,
ProductCollectionFactory $_productCollection,
CategoryFactory $_category,
CustomerFactory $_customer,
Address $address,
\Magento\Framework\Json\Helper\Data $jsonHelper
)
{
$this->_resourceConnection = $_resourceConnection;
$this->_productCollection = $_productCollection;
$this->_category = $_category;
$this->_customer = $_customer;
$this->_address = $address;
$this->_jsonHelper = $jsonHelper;
}
/**
* @return int
*/
public function getCatalogProductCount()
{
return $this->_productCollection->create()->getSize();
}
/**
* @param $categoryId
*
* @return int
*/
public function getCategoryProductCount($categoryId)
{
$size = 0;
$category = $this->_category->create()->load($categoryId);
if (isset($category) && !empty($category)) {
$size = $category->getProductCollection()->getSize();
}
return $size;
}
/**
* @param $customerId
*
* @return int
*/
public function getCustomer($customerId)
{
$customerData = array();
$customerModel = $this->_customer->create();
$customer = $customerModel->load($customerId);
if (isset($customer) && !empty($customer)) {
$customerData['UserName'] = $customer->getEmail();
$customerData['IDClient'] = $customer->getId();
$customerData['ID'] = $customer->getId();
$customerData['SuccExterne'] = $customer->getData('succexterne');
$customerData['LastName'] = $customer->getLastname();
$customerData['FirstName'] = $customer->getFirstname();
$customerData['BirthDate'] = $customer->getDob();
$customerData['Email'] = $customer->getEmail();
$customerData['Language'] = $customer->getData('language');
$customerData['Gender'] = $customer->getGender();
$customerData['ConsentementC28'] = $customer->getData('consentementc28');
$customerData['CardNo'] = $customer->getData('cardno');
$customerData['Externe'] = $customer->getData('externe');
$updateTimeStamp = strtotime($customer->getUpdatedAt());
$customerData['ModificationDate'] = date('Y-m-d', $updateTimeStamp);
$childId = $customer->getData('child_customer_id');
// Get Address
$billingID = $customer->getDefaultBilling();
$shippingID = $customer->getDefaultShipping();
if(!empty($billingID) && isset($billingID)) {
$billingAddress = $this->_address->load($billingID);
$streetAddress = implode(', ', $billingAddress->getStreet());
$customerData['Address'] = $streetAddress;
$customerData['City'] = $billingAddress->getCity();
$customerData['Province'] = $billingAddress->getRegion();
$customerData['Country'] = $billingAddress->getCountryId();
$customerData['PostalCode'] = $billingAddress->getPostcode();
$customerData['Phone'] = $billingAddress->getTelephone();
} elseif (!empty($shippingID) && isset($shippingID)) {
$shippingAddress = $this->_address->load($shippingID);
$streetAddress = implode(', ', $shippingAddress->getStreet());
$customerData['Address'] = $streetAddress;
$customerData['City'] = $shippingAddress->getCity();
$customerData['Province'] = $shippingAddress->getRegion();
$customerData['Country'] = $shippingAddress->getCountryId();
$customerData['PostalCode'] = $shippingAddress->getPostcode();
$customerData['Phone'] = $shippingAddress->getTelephone();
}
// Get Child Customer
if($childId) {
$childCustomer = $customerModel->load($childId);
$customerData['CHILDREN'] = [
'ID' => $childId,
'ParentID' => $customerId,
'LastName' => $childCustomer->getLastname(),
'FirstName' => $childCustomer->getFirstname(),
'Gender' => $childCustomer->getGender(),
'BirthDate' => $childCustomer->getDob(),
'Deleted' => false
];
}
}
return $customerData;
// echo $this->_jsonHelper->jsonEncode($customerData);
exit(0);
}
}
When I return single data then there is no problem, but when I return json or array then it doesn't format that in proper json. Can someone help?