-1

I have create a custom Api and I am getting the response as follws:

[
    [
        {
            "entity_id": "154",
            "state": "new",
            "status": "pending",
            "coupon_code": null,
            "protect_code": "98aa60a748d6b23f93c4c26eed7b53b9",
            "shipping_description": "Shipping Options - Self Collection",
            "is_virtual": "0",
            "store_id": "2",
            "customer_id": "16341",
            "base_discount_amount": "0.0000",

But I want my response as follws:

{
    "items": [
        {
            "base_currency_code": "SGD",
            "base_discount_amount": 0,
            "base_grand_total": 107,
            "base_discount_tax_compensation_amount": 0,
            "base_shipping_amount": 0,
            "base_shipping_discount_amount": 0,
            "base_shipping_incl_tax": 0,
            "base_shipping_tax_amount": 0,
            "base_subtotal": 100,
            "base_subtotal_incl_tax": 107,
            "base_tax_amount": 7,

Please help.Thanks in advance.

<?php

namespace Vendor\CustomApi\Model;

use Vendor\CustomApi\Api\OrderHistoryInterface;
use Magento\Customer\Model\CustomerFactory;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;

class OrderHistory implements OrderHistoryInterface {

    protected $customerFactory;
    protected $customerRepository;

    public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository,\Magento\Framework\Json\Helper\Data $jsonHelper) {
        $this->customerFactory = $customerFactory;
        $this->customerRepository = $customerRepository;
          $this->jsonHelper = $jsonHelper;
    }

    public function getOrderHistory($customerId, $direction, $pageSize, $currentPage) {
        try {
            $customer = $this->customerFactory->create()->getCollection()
                    ->addAttributeToSelect("*")
                    ->addAttributeToFilter("entity_id", array("eq" => $customerId));
            $CustomerResult = $customer->getData();
            $customerInfo = $this->customerRepository->getById($customerId);
            $customerAttributeData = $customerInfo->__toArray();
            $VesselId = $customerAttributeData['custom_attributes']['customer_vessels']['value'];
            $customerData = $this->customerFactory->create()->getCollection()
                            ->addAttributeToSelect("*")
                            ->addAttributeToFilter("customer_vessels", array("eq" => $VesselId))
                            ->addAttributeToFilter("group_id", array("eq" => self::GROUP_ID))->load();
            $CustomerDataResult = $customerData->getData();
            if(empty($CustomerDataResult)) {
               throw new NoSuchEntityException(
                   __('Invalid customer.')
                   );
           }
            $captinId = $CustomerDataResult[0]['entity_id'];
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $order = $objectManager->create('Magento\Sales\Model\Order')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
            $orderInfo = $order->getData();
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $orderItemCollection = $objectManager->create('Magento\Sales\Model\ResourceModel\Order\Item\Collection')
                    ->setOrder('entity_id', $direction);

            $orderItemCollection->getSelect()->joinLeft(
                    'sales_order', 'sales_order.entity_id=main_table.order_id', ['increment_id']
            )->where('sales_order.customer_id=?', $captinId);
            $orderResult = $orderItemCollection->getData();
            foreach ($orderInfo as $k => $v) {
                foreach ($orderResult as $k1 => $v1) {
                    if ($v1['order_id'] == $v['entity_id']) {
                        $v1['product_options'] = $this->jsonHelper->jsonDecode($v1['product_options']);
                        $orderInfo[$k]['product_details'][] = $v1;

                    }
                }
            }
        } catch (\Exception $e) {
            throw new NoSuchEntityException(
            __('Invalid customer.')
            );
        }
//        $managerOrder = $this->jsonHelper->jsonDecode($orderInfo);
//        $managerOrder[] = array('Alka'=>$managerOrder);
//        $managerOrder = $this->jsonHelper->jsonEncode($managerOrder);
//        $object = array();
//        $object[0]['Alka'] =  $orderInfo[0];
//        $object->items = $orderInfo;
//        $orderInfo[]['total_count'] = count($orderInfo);
        return ($orderInfo);
    }

}
  • add your code.. – Rk Rathod Apr 23 at 13:25
  • Add your code from where you send response. – Rohan Hapani Apr 23 at 13:27
  • Rk Rathod and Rohan Hapanai, I have added the code. – Flying Finner Apr 23 at 13:34
  • @MeetaliGupta you can check my answer and let me know – fmsthird Apr 23 at 14:15
0

I think you can actually use a simple json_encode with print_r.
Like below:

print_r(json_encode(['items' => $orderInfo]));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.