1

I need tho get current customer cart items using rest api, so I've created Cart module and

Here is my interface:

<?php

namespace Bit68\Cart\Api\Cart;

interface MyCartInterface{

/**
* @api
* @return array
*/

public function getItems();

}

?>

here is my class that implements the interface:

namespace Bit68\Cart\Model\Cart;
use Bit68\Cart\Api\Cart\MyCartInterface;
use \Magento\Checkout\Model\Cart\CartInterface;

class MyCart implements MyCartInterface{
/**
* @var Magento\Checkout\Model\Cart\CartInterface
*/
private $cartObj;

public function __construct(\Magento\Checkout\Model\Cart $cartObj){
    $this->cartObj = $cartObj;
}

/**
* @api
* @return array
*/

public function getItems(){
    $items = $this->cartObj->getQuote()->getAllItems();
    return $items;
}
}

?>

Here is 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/cart/items" method="GET">
    <service class="\Bit68\Cart\Api\Cart\MyCartInterface" method="getItems"/>
    <resources>
        <resource ref="anonymous"/>
    </resources>
</route>

and that's where I call my api:

 <?php
 session_start();
 /*
 *  base url of the magento host
 */
 $host = 'http://127.0.0.1/yourParts/';

 //unset($_SESSION['access_token']);
 if (!isset($_SESSION['access_token'])) {
 echo 'Authenticating...<br>';
 /*
 * authentication details of the customer
 */
 $username = 'mail';
 $password = 'password';
 $postData['username'] = $username;
 $postData['password'] = $password;

 /*
 * init curl
 */
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $host.'rest/V1/integration/customer/token');
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
/*
 * set content type and length
 */
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
       'Content-Type: application/json',
       'Content-Length: '.strlen(json_encode($postData)),
   )
);
/*
* setpost data
*/
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
$output = curl_exec($ch);
curl_close($ch);
/*
* access token in json format
*/
echo $output;
$_SESSION['access_token'] = $output;
}
if (isset($_SESSION['access_token'])) {
   /*
   * create headers for authorization
   */
   $headers = array(
       'Authorization: Bearer '.json_decode($_SESSION['access_token']),
   );
   echo '<pre>';
   echo 'api call... with key: '.$_SESSION['access_token'].'<br><br><br>';
   $ch = curl_init();
   /*
   * set api resource url
   */

   curl_setopt($ch, CURLOPT_URL, $host.'rest/V1/cart/items');
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   $output = curl_exec($ch);

   curl_close($ch);
   echo '<br>';
   echo gettype($output);
   echo '<br>';
   echo $output;
   echo '<br>';
   echo "Today is " . date("Y-m-d") . "<br>";
   /*
    * json response need to rtrim with [], some times it is appended to the respose so the json becomes invalid so need to rtrim the response
   */
   $test = json_decode(rtrim($output, '[]'));
   echo '
   =========================RESPONSE================================<br>
   ';

   print_r($test);
  // echo($test->item_id);
 }
 exit(0);

 ?>

and that is my module tree structure

enter image description here

0

Please follow below code to get cart items of a customer.

public function __construct(\Magento\Quote\Api\QuoteManagement $quote){
    $this->quoteManagement = $quote;
}

/**
* @api
* @return array
*/

public function getItems(){
    $items = $this->quoteManagement->getCartForCustomer($customerId);
    return $items;
}

I am sure it'll work for you so try it.

1
  • Did it work for you ? If yes then please accept. It'll help others. Nov 16 '18 at 16:01

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.