How to convert string into array? Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Array in magento 2How to move minicart into topbar.phtml?Convert event in Magento2?Warning: array_merge(): Argument #2 is not an arrayget order by string/value in product_options arrayMagento 2, How to respond array in API without sortMagento 225 :Notice: Array to string conversionget translated string Magento 2Help with Magento error on an old install (Array to string conversion)Convert Magento 2 frontend with angular 7?
Special flights
Most effective melee weapons for arboreal combat? (pre-gunpowder technology)
Understanding p-Values using an example
I can't produce songs
A term for a woman complaining about things/begging in a cute/childish way
Asymptotics question
Does the Mueller report show a conspiracy between Russia and the Trump Campaign?
Nose gear failure in single prop aircraft: belly landing or nose-gear up landing?
What adaptations would allow standard fantasy dwarves to survive in the desert?
Resize vertical bars (absolute-value symbols)
Why is it faster to reheat something than it is to cook it?
Why not use the yoke to control yaw, as well as pitch and roll?
What order were files/directories output in dir?
Mounting TV on a weird wall that has some material between the drywall and stud
Co-worker has annoying ringtone
AppleTVs create a chatty alternate WiFi network
Why are vacuum tubes still used in amateur radios?
What are the main differences between the original Stargate SG-1 and the Final Cut edition?
Does silver oxide react with hydrogen sulfide?
I got rid of Mac OSX and replaced it with linux but now I can't change it back to OSX or windows
Tannaka duality for semisimple groups
What initially awakened the Balrog?
Why is a lens darker than other ones when applying the same settings?
malloc in main() or malloc in another function: allocating memory for a struct and its members
How to convert string into array?
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Array in magento 2How to move minicart into topbar.phtml?Convert event in Magento2?Warning: array_merge(): Argument #2 is not an arrayget order by string/value in product_options arrayMagento 2, How to respond array in API without sortMagento 225 :Notice: Array to string conversionget translated string Magento 2Help with Magento error on an old install (Array to string conversion)Convert Magento 2 frontend with angular 7?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am getting the below mentioned string in my API response. How can I convert this into array?
"product_options": ""info_buyRequest":"qty":1,"giftcard_lifetime":null,"giftcard_is_redeemable":0,"giftcard_email_template":null,"giftcard_type":null"
Vendor/CustomApi/Model/OrderHistory.php
<?php
namespace VendorCustomApiModel;
use AmosCustomApiApiOrderHistoryInterface;
use MagentoCustomerModelCustomerFactory;
use MagentoCustomerApiCustomerRepositoryInterface;
use MagentoFrameworkExceptionNoSuchEntityException;
class OrderHistory implements OrderHistoryInterface
protected $customerFactory;
protected $customerRepository;
public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository)
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;
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();
$captinId = $CustomerDataResult[0]['entity_id'];
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$order = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
$orderInfo = $order->getData();
// print_r($orderInfo);die();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$orderItemCollection = $objectManager->create('MagentoSalesModelResourceModelOrderItemCollection')
->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'])
$orderInfo[$k]['product_details'][] = $v1;
catch (Exception $e)
throw new NoSuchEntityException(
__('Invalid customer.')
);
return json_decode($orderInfo);
Vendor/CustomApi/Api/OrderHistoryInterface.php
<?php
namespace VendorCustomApiApi;
interface OrderHistoryInterface
CONST GROUP_ID = 5;
/**
* User can login and once logged in success, user will receive their information with order history.
* @param int $customerId
* @param string $direction
* @param int $pageSize
* @param int $currentPage
* @return mixed Order History
*/
public function getOrderHistory($customerId,$direction,$pageSize,$currentPage);
magento2.2
|
show 6 more comments
I am getting the below mentioned string in my API response. How can I convert this into array?
"product_options": ""info_buyRequest":"qty":1,"giftcard_lifetime":null,"giftcard_is_redeemable":0,"giftcard_email_template":null,"giftcard_type":null"
Vendor/CustomApi/Model/OrderHistory.php
<?php
namespace VendorCustomApiModel;
use AmosCustomApiApiOrderHistoryInterface;
use MagentoCustomerModelCustomerFactory;
use MagentoCustomerApiCustomerRepositoryInterface;
use MagentoFrameworkExceptionNoSuchEntityException;
class OrderHistory implements OrderHistoryInterface
protected $customerFactory;
protected $customerRepository;
public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository)
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;
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();
$captinId = $CustomerDataResult[0]['entity_id'];
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$order = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
$orderInfo = $order->getData();
// print_r($orderInfo);die();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$orderItemCollection = $objectManager->create('MagentoSalesModelResourceModelOrderItemCollection')
->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'])
$orderInfo[$k]['product_details'][] = $v1;
catch (Exception $e)
throw new NoSuchEntityException(
__('Invalid customer.')
);
return json_decode($orderInfo);
Vendor/CustomApi/Api/OrderHistoryInterface.php
<?php
namespace VendorCustomApiApi;
interface OrderHistoryInterface
CONST GROUP_ID = 5;
/**
* User can login and once logged in success, user will receive their information with order history.
* @param int $customerId
* @param string $direction
* @param int $pageSize
* @param int $currentPage
* @return mixed Order History
*/
public function getOrderHistory($customerId,$direction,$pageSize,$currentPage);
magento2.2
1
have you triedjson_decode
?
– Shoaib Munir
2 days ago
I have tried this but getting error as following: "message": "Class mixed does not exist", "code": -1,
– Meetali Gupta
2 days ago
please share your code here, it should work withjson_decode
– Shoaib Munir
2 days ago
2
I have added my code. Please check.
– Meetali Gupta
2 days ago
1
I am unable to findjson_decode
in your code
– Shoaib Munir
2 days ago
|
show 6 more comments
I am getting the below mentioned string in my API response. How can I convert this into array?
"product_options": ""info_buyRequest":"qty":1,"giftcard_lifetime":null,"giftcard_is_redeemable":0,"giftcard_email_template":null,"giftcard_type":null"
Vendor/CustomApi/Model/OrderHistory.php
<?php
namespace VendorCustomApiModel;
use AmosCustomApiApiOrderHistoryInterface;
use MagentoCustomerModelCustomerFactory;
use MagentoCustomerApiCustomerRepositoryInterface;
use MagentoFrameworkExceptionNoSuchEntityException;
class OrderHistory implements OrderHistoryInterface
protected $customerFactory;
protected $customerRepository;
public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository)
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;
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();
$captinId = $CustomerDataResult[0]['entity_id'];
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$order = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
$orderInfo = $order->getData();
// print_r($orderInfo);die();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$orderItemCollection = $objectManager->create('MagentoSalesModelResourceModelOrderItemCollection')
->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'])
$orderInfo[$k]['product_details'][] = $v1;
catch (Exception $e)
throw new NoSuchEntityException(
__('Invalid customer.')
);
return json_decode($orderInfo);
Vendor/CustomApi/Api/OrderHistoryInterface.php
<?php
namespace VendorCustomApiApi;
interface OrderHistoryInterface
CONST GROUP_ID = 5;
/**
* User can login and once logged in success, user will receive their information with order history.
* @param int $customerId
* @param string $direction
* @param int $pageSize
* @param int $currentPage
* @return mixed Order History
*/
public function getOrderHistory($customerId,$direction,$pageSize,$currentPage);
magento2.2
I am getting the below mentioned string in my API response. How can I convert this into array?
"product_options": ""info_buyRequest":"qty":1,"giftcard_lifetime":null,"giftcard_is_redeemable":0,"giftcard_email_template":null,"giftcard_type":null"
Vendor/CustomApi/Model/OrderHistory.php
<?php
namespace VendorCustomApiModel;
use AmosCustomApiApiOrderHistoryInterface;
use MagentoCustomerModelCustomerFactory;
use MagentoCustomerApiCustomerRepositoryInterface;
use MagentoFrameworkExceptionNoSuchEntityException;
class OrderHistory implements OrderHistoryInterface
protected $customerFactory;
protected $customerRepository;
public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository)
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;
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();
$captinId = $CustomerDataResult[0]['entity_id'];
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$order = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
$orderInfo = $order->getData();
// print_r($orderInfo);die();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$orderItemCollection = $objectManager->create('MagentoSalesModelResourceModelOrderItemCollection')
->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'])
$orderInfo[$k]['product_details'][] = $v1;
catch (Exception $e)
throw new NoSuchEntityException(
__('Invalid customer.')
);
return json_decode($orderInfo);
Vendor/CustomApi/Api/OrderHistoryInterface.php
<?php
namespace VendorCustomApiApi;
interface OrderHistoryInterface
CONST GROUP_ID = 5;
/**
* User can login and once logged in success, user will receive their information with order history.
* @param int $customerId
* @param string $direction
* @param int $pageSize
* @param int $currentPage
* @return mixed Order History
*/
public function getOrderHistory($customerId,$direction,$pageSize,$currentPage);
magento2.2
magento2.2
edited 2 days ago
Meetali Gupta
asked 2 days ago
Meetali GuptaMeetali Gupta
587
587
1
have you triedjson_decode
?
– Shoaib Munir
2 days ago
I have tried this but getting error as following: "message": "Class mixed does not exist", "code": -1,
– Meetali Gupta
2 days ago
please share your code here, it should work withjson_decode
– Shoaib Munir
2 days ago
2
I have added my code. Please check.
– Meetali Gupta
2 days ago
1
I am unable to findjson_decode
in your code
– Shoaib Munir
2 days ago
|
show 6 more comments
1
have you triedjson_decode
?
– Shoaib Munir
2 days ago
I have tried this but getting error as following: "message": "Class mixed does not exist", "code": -1,
– Meetali Gupta
2 days ago
please share your code here, it should work withjson_decode
– Shoaib Munir
2 days ago
2
I have added my code. Please check.
– Meetali Gupta
2 days ago
1
I am unable to findjson_decode
in your code
– Shoaib Munir
2 days ago
1
1
have you tried
json_decode
?– Shoaib Munir
2 days ago
have you tried
json_decode
?– Shoaib Munir
2 days ago
I have tried this but getting error as following: "message": "Class mixed does not exist", "code": -1,
– Meetali Gupta
2 days ago
I have tried this but getting error as following: "message": "Class mixed does not exist", "code": -1,
– Meetali Gupta
2 days ago
please share your code here, it should work with
json_decode
– Shoaib Munir
2 days ago
please share your code here, it should work with
json_decode
– Shoaib Munir
2 days ago
2
2
I have added my code. Please check.
– Meetali Gupta
2 days ago
I have added my code. Please check.
– Meetali Gupta
2 days ago
1
1
I am unable to find
json_decode
in your code– Shoaib Munir
2 days ago
I am unable to find
json_decode
in your code– Shoaib Munir
2 days ago
|
show 6 more comments
2 Answers
2
active
oldest
votes
Just use this below code where you want to convert to array :
/**
* @var MagentoFrameworkJsonHelperData
*/
protected $jsonHelper;
/**
* [__construct description]
* @param MagentoFrameworkJsonHelperData $jsonHelper [description]
*/
public function __construct(
.......
MagentoFrameworkJsonHelperData $jsonHelper,
.......
)
.......
$this->jsonHelper = $jsonHelper;
.......
public function yourFunction()
$strToArr = $this->jsonHelper->jsonDecode('your data');
print_r($strToArr);
add a comment |
Replace this code
Vendor/CustomApi/Model/OrderHistory.php
<?php
namespace VendorCustomApiModel;
use AmosCustomApiApiOrderHistoryInterface;
use MagentoCustomerModelCustomerFactory;
use MagentoCustomerApiCustomerRepositoryInterface;
use MagentoFrameworkExceptionNoSuchEntityException;
class OrderHistory implements OrderHistoryInterface
protected $customerFactory;
protected $customerRepository;
public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository)
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;
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();
$captinId = $CustomerDataResult[0]['entity_id'];
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$order = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
$orderInfo = $order->getData();
// print_r($orderInfo);die();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$orderItemCollection = $objectManager->create('MagentoSalesModelResourceModelOrderItemCollection')
->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'])
$orderInfo[$k]['product_details'][] = $v1;
catch (Exception $e)
throw new NoSuchEntityException(
__('Invalid customer.')
);
header('Content-Type: application/json'); echo json_encode($orderInfo);
It's better to use magento standard way instead of json_encode() core php function ;) & There are little bit different output of json_encode() and MagentoFrameworkJsonHelperData class
– Rohan Hapani
2 days ago
1
Are it is regarding API so I am using this one :) for POSTMAN
– Aaditya
2 days ago
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f270637%2fhow-to-convert-string-into-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just use this below code where you want to convert to array :
/**
* @var MagentoFrameworkJsonHelperData
*/
protected $jsonHelper;
/**
* [__construct description]
* @param MagentoFrameworkJsonHelperData $jsonHelper [description]
*/
public function __construct(
.......
MagentoFrameworkJsonHelperData $jsonHelper,
.......
)
.......
$this->jsonHelper = $jsonHelper;
.......
public function yourFunction()
$strToArr = $this->jsonHelper->jsonDecode('your data');
print_r($strToArr);
add a comment |
Just use this below code where you want to convert to array :
/**
* @var MagentoFrameworkJsonHelperData
*/
protected $jsonHelper;
/**
* [__construct description]
* @param MagentoFrameworkJsonHelperData $jsonHelper [description]
*/
public function __construct(
.......
MagentoFrameworkJsonHelperData $jsonHelper,
.......
)
.......
$this->jsonHelper = $jsonHelper;
.......
public function yourFunction()
$strToArr = $this->jsonHelper->jsonDecode('your data');
print_r($strToArr);
add a comment |
Just use this below code where you want to convert to array :
/**
* @var MagentoFrameworkJsonHelperData
*/
protected $jsonHelper;
/**
* [__construct description]
* @param MagentoFrameworkJsonHelperData $jsonHelper [description]
*/
public function __construct(
.......
MagentoFrameworkJsonHelperData $jsonHelper,
.......
)
.......
$this->jsonHelper = $jsonHelper;
.......
public function yourFunction()
$strToArr = $this->jsonHelper->jsonDecode('your data');
print_r($strToArr);
Just use this below code where you want to convert to array :
/**
* @var MagentoFrameworkJsonHelperData
*/
protected $jsonHelper;
/**
* [__construct description]
* @param MagentoFrameworkJsonHelperData $jsonHelper [description]
*/
public function __construct(
.......
MagentoFrameworkJsonHelperData $jsonHelper,
.......
)
.......
$this->jsonHelper = $jsonHelper;
.......
public function yourFunction()
$strToArr = $this->jsonHelper->jsonDecode('your data');
print_r($strToArr);
answered 2 days ago
Rohan HapaniRohan Hapani
7,01631865
7,01631865
add a comment |
add a comment |
Replace this code
Vendor/CustomApi/Model/OrderHistory.php
<?php
namespace VendorCustomApiModel;
use AmosCustomApiApiOrderHistoryInterface;
use MagentoCustomerModelCustomerFactory;
use MagentoCustomerApiCustomerRepositoryInterface;
use MagentoFrameworkExceptionNoSuchEntityException;
class OrderHistory implements OrderHistoryInterface
protected $customerFactory;
protected $customerRepository;
public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository)
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;
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();
$captinId = $CustomerDataResult[0]['entity_id'];
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$order = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
$orderInfo = $order->getData();
// print_r($orderInfo);die();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$orderItemCollection = $objectManager->create('MagentoSalesModelResourceModelOrderItemCollection')
->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'])
$orderInfo[$k]['product_details'][] = $v1;
catch (Exception $e)
throw new NoSuchEntityException(
__('Invalid customer.')
);
header('Content-Type: application/json'); echo json_encode($orderInfo);
It's better to use magento standard way instead of json_encode() core php function ;) & There are little bit different output of json_encode() and MagentoFrameworkJsonHelperData class
– Rohan Hapani
2 days ago
1
Are it is regarding API so I am using this one :) for POSTMAN
– Aaditya
2 days ago
add a comment |
Replace this code
Vendor/CustomApi/Model/OrderHistory.php
<?php
namespace VendorCustomApiModel;
use AmosCustomApiApiOrderHistoryInterface;
use MagentoCustomerModelCustomerFactory;
use MagentoCustomerApiCustomerRepositoryInterface;
use MagentoFrameworkExceptionNoSuchEntityException;
class OrderHistory implements OrderHistoryInterface
protected $customerFactory;
protected $customerRepository;
public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository)
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;
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();
$captinId = $CustomerDataResult[0]['entity_id'];
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$order = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
$orderInfo = $order->getData();
// print_r($orderInfo);die();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$orderItemCollection = $objectManager->create('MagentoSalesModelResourceModelOrderItemCollection')
->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'])
$orderInfo[$k]['product_details'][] = $v1;
catch (Exception $e)
throw new NoSuchEntityException(
__('Invalid customer.')
);
header('Content-Type: application/json'); echo json_encode($orderInfo);
It's better to use magento standard way instead of json_encode() core php function ;) & There are little bit different output of json_encode() and MagentoFrameworkJsonHelperData class
– Rohan Hapani
2 days ago
1
Are it is regarding API so I am using this one :) for POSTMAN
– Aaditya
2 days ago
add a comment |
Replace this code
Vendor/CustomApi/Model/OrderHistory.php
<?php
namespace VendorCustomApiModel;
use AmosCustomApiApiOrderHistoryInterface;
use MagentoCustomerModelCustomerFactory;
use MagentoCustomerApiCustomerRepositoryInterface;
use MagentoFrameworkExceptionNoSuchEntityException;
class OrderHistory implements OrderHistoryInterface
protected $customerFactory;
protected $customerRepository;
public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository)
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;
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();
$captinId = $CustomerDataResult[0]['entity_id'];
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$order = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
$orderInfo = $order->getData();
// print_r($orderInfo);die();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$orderItemCollection = $objectManager->create('MagentoSalesModelResourceModelOrderItemCollection')
->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'])
$orderInfo[$k]['product_details'][] = $v1;
catch (Exception $e)
throw new NoSuchEntityException(
__('Invalid customer.')
);
header('Content-Type: application/json'); echo json_encode($orderInfo);
Replace this code
Vendor/CustomApi/Model/OrderHistory.php
<?php
namespace VendorCustomApiModel;
use AmosCustomApiApiOrderHistoryInterface;
use MagentoCustomerModelCustomerFactory;
use MagentoCustomerApiCustomerRepositoryInterface;
use MagentoFrameworkExceptionNoSuchEntityException;
class OrderHistory implements OrderHistoryInterface
protected $customerFactory;
protected $customerRepository;
public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository)
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;
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();
$captinId = $CustomerDataResult[0]['entity_id'];
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$order = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
$orderInfo = $order->getData();
// print_r($orderInfo);die();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$orderItemCollection = $objectManager->create('MagentoSalesModelResourceModelOrderItemCollection')
->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'])
$orderInfo[$k]['product_details'][] = $v1;
catch (Exception $e)
throw new NoSuchEntityException(
__('Invalid customer.')
);
header('Content-Type: application/json'); echo json_encode($orderInfo);
answered 2 days ago
AadityaAaditya
4,36621139
4,36621139
It's better to use magento standard way instead of json_encode() core php function ;) & There are little bit different output of json_encode() and MagentoFrameworkJsonHelperData class
– Rohan Hapani
2 days ago
1
Are it is regarding API so I am using this one :) for POSTMAN
– Aaditya
2 days ago
add a comment |
It's better to use magento standard way instead of json_encode() core php function ;) & There are little bit different output of json_encode() and MagentoFrameworkJsonHelperData class
– Rohan Hapani
2 days ago
1
Are it is regarding API so I am using this one :) for POSTMAN
– Aaditya
2 days ago
It's better to use magento standard way instead of json_encode() core php function ;) & There are little bit different output of json_encode() and MagentoFrameworkJsonHelperData class
– Rohan Hapani
2 days ago
It's better to use magento standard way instead of json_encode() core php function ;) & There are little bit different output of json_encode() and MagentoFrameworkJsonHelperData class
– Rohan Hapani
2 days ago
1
1
Are it is regarding API so I am using this one :) for POSTMAN
– Aaditya
2 days ago
Are it is regarding API so I am using this one :) for POSTMAN
– Aaditya
2 days ago
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f270637%2fhow-to-convert-string-into-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
have you tried
json_decode
?– Shoaib Munir
2 days ago
I have tried this but getting error as following: "message": "Class mixed does not exist", "code": -1,
– Meetali Gupta
2 days ago
please share your code here, it should work with
json_decode
– Shoaib Munir
2 days ago
2
I have added my code. Please check.
– Meetali Gupta
2 days ago
1
I am unable to find
json_decode
in your code– Shoaib Munir
2 days ago