I am building custom magento 2 API. I want to get customer CART DATA using CUSTOMER_ID. I am doing this to serve magento data into android and ios customer devices.
I have tried following code but its not working.
$params = $this->request->getPostValue();
$customerId = $params["customer_id"];
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerData = $objectManager->create('MagentoCustomerModelCustomer')->load($customerId);
//Code to get customer cart data
$quote = $this->quoteFactory->create();
$customerQuote=$this->quoteModel->loadByCustomerId($quote,$customerId); // where `$customerId` is your `customer id`
return $items = $customerQuote->getAllItems();
For security concern, I am passing custom permanent token and customer id.
I have also tried many other sample codes but they are not working anyone can help me here.
Thanks
2
Answers
I am assuming you are passing customer id into POST method.
Can you try following ?
Add following requirements
try following function
I think it’s better to use customer token instead of Customer ID, that’s how Magento handles customer-related requests. You may retrieve the token when the customer is being signed in.
Vendor/Module/etc/webapi.xml
curl example
It’s a more secure way to retrieve customer data. I would not recommend you to pass customer ID via API.
OR,
If you want to use admin token, specify customer ID in the declaration of an endpoint. It will be also available as the 1st argument of your function.