skip to Main Content

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.

enter image description here

I have also tried many other sample codes but they are not working anyone can help me here.
Thanks

2

Answers


  1. I am assuming you are passing customer id into POST method.
    Can you try following ?

    Add following requirements

    ...
    use MagentoQuoteModelQuoteFactory;
    ...
    protected $quoteFactory;
    ...
    
    
     public function __construct(
     ...
     MagentoQuoteModelQuoteFactory $quoteFactory,
     ...
    ){
     ...
     $this->quoteFactory = $quoteFactory;
     ...
     }
    

    try following function

    public function getCart() {
    
        $params = $this->request->getPostValue();
        $customerId = $params["customer_id"];
        
        $objectManager = MagentoFrameworkAppObjectManager::getInstance();
        $customerObj = $objectManager->create('MagentoCustomerModelCustomer')->load($customerId);
        
        $customerFirstName = $customerObj->getFirstname();
        $customerFirstName = $customerObj->getLastname(); 
        
        $quote = $this->quoteFactory->create()->loadByCustomer($customerObj);
        
        $items = $quote->getAllItems();
        
        $cart_data = array();
        foreach($items as $item)
        {
            $cart_data[] = array(
                'name'=> $item_data['name'],
                'product_id'=> $item_data['product_id'],
                'price'=>$item_data['price'],                               
                'qty'=> $item_data['qty'],
            );
        } 
        
        return $cart_data;
        
    }  
    
    Login or Signup to reply.
  2. 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.

    1. Declare your API endpoint in Vendor/Module/etc/webapi.xml
    <?xml version="1.0"?>
    <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
        <route url="/V1/example/customer/quote" method="GET">
            <service class="VendorModuleApiQuoteManagerInterface" method="getQuote"/>
            <resources>
                <resource ref="self"/>
            </resources>
            <data>
                <parameter name="customerId" force="true">%customer_id%</parameter>
            </data>
        </route>
    </routes>
    
    1. Your function would look like that
        public function getQuote(int $customerId)
        {
            // Token will be automatically replaced with the Customer ID, so $customerId is an integer here
        }
    
    1. API Request would look like that
    GET: {{host}}example/customer/quote
    HEADER: Authorization: Bearer {{customerToken}}
    

    curl example

    curl -sS -H 'Content-type: application/json' -H 'X-HTTP-Method-Override: GET' 
    -H 'Authorization: Bearer 99x8vbo999ox5qxjm7uhsso7ny3fzl5o' 
    -X GET http://local.magento2.com/rest/default/V1/example/customer/quote
    

    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.

    <?xml version="1.0"?>
    <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
        <route url="/V1/example/customer/quote/:customerId" method="GET">
            <service class="VendorModuleApiQuoteManagerInterface" method="getQuote"/>
            <resources>
                <resource ref="Vendor_Module::general"/>
            </resources>
        </route>
    </routes>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search