Answers.Body: <p>The cart is not baked into \Craft, but craft\commerce\Plugin.</p>
  
  <p>Try the following:</p>
  
  <pre><code>$cart = craft\commerce\Plugin::getInstance()-&gt;getCarts()-&gt;getCart();
  </code></pre>
  
  <p>Uses the <a href="https://docs.craftcms.com/commerce/api/v2/craft-commerce-services-carts.html#public-methods" rel="noreferrer">following function</a>:</p>
  
  <pre><code>/**
   * Get the current cart for this session.
   *
   * @param bool $forceSave Force the cart to save when requesting it.
   * @return Order
   * @throws Exception
   * @throws \Throwable
   * @throws \craft\errors\ElementNotFoundException
   */
  public function getCart($forceSave = false): Order
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-20T13:19:40.510Z
Answers.LastActivityDate: 2018-12-20T13:19:40.510Z
Answers.UserDisplayName: Jørgen
Body: <p>I want to be able to retrieve the current users Cart via a JSON API endpoint, so I can loop through the products in the cart in JS on the front end. </p>
  
  <p>I've got the Element API plugin installed, but I am not understanding how to grab the current cart from inside the endpoint via PHP. Any help much appreciated.</p>
  
  <pre><code>&lt;?php
  use craft\helpers\UrlHelper;
  use craft\commerce\elements\Product;
  use craft\commerce\elements\Order;
  
  function requireLogin() {
      $currentUser = Craft::$app-&gt;user-&gt;getIdentity();
  
      if(!$currentUser) {
          throw new \yii\web\ForbiddenHttpException("You are not logged in");
      }
  }
  
  function requireCart() {
      // TWIG would be: {% set cart = craft.commerce.getCarts().getCart() %}
      $currentCart = Craft::WHAT?;
      $cartId      = $currentCart-&gt;id;
  
      if(!$currentCart) {
          throw new \yii\web\ForbiddenHttpException("You don't have a cart yet");
      }
  }
  
  return [
      'endpoints' =&gt; [
          'cart.json' =&gt; function() {
              requireLogin();
              requireCart();
  
              \Craft::$app-&gt;response-&gt;headers-&gt;set('Access-Control-Allow-Origin', '*');
  
              return [
                  'elementType' =&gt; Order::class,
                  'criteria'    =&gt; ['id' =&gt; $cartId],
                  'transformer' =&gt; function(Order $order) {
                      $items = [];
  
                      foreach($order-&gt;lineItems as $lineItem) {
                          $items[] = [array(
                              'itemTitle' =&gt; $lineItem-&gt;title
                          )];
                      }
  
                      return [
                          'items' =&gt; $items
                      ];
                  }
              ];
          }
      ]
  ]
  ?&gt;
  </code></pre>
  
Tags: <plugin-elementapi><plugin-craftcommerce2>
Title: Exposing current Commerce2 Cart via the Element API
ThreadText: # Exposing current Commerce2 Cart via the Element API
  
  (Asked by: username_0 on 2018-12-18)
  
  I want to be able to retrieve the current users Cart via a JSON API endpoint, so I can loop through the products in the cart in JS on the front end.
  
  I've got the Element API plugin installed, but I am not understanding how to grab the current cart from inside the endpoint via PHP. Any help much appreciated.
  
  ```
  <?php
  use craft\helpers\UrlHelper;
  use craft\commerce\elements\Product;
  use craft\commerce\elements\Order;
  
  function requireLogin() {
      $currentUser = Craft::$app->user->getIdentity();
  
      if(!$currentUser) {
          throw new \yii\web\ForbiddenHttpException("You are not logged in");
      }
  }
  
  function requireCart() {
      // TWIG would be: {% set cart = craft.commerce.getCarts().getCart() %}
      $currentCart = Craft::WHAT?;
      $cartId      = $currentCart->id;
  
      if(!$currentCart) {
          throw new \yii\web\ForbiddenHttpException("You don't have a cart yet");
      }
  }
  
  return [
      'endpoints' => [
          'cart.json' => function() {
              requireLogin();
              requireCart();
  
              \Craft::$app->response->headers->set('Access-Control-Allow-Origin', '*');
  
              return [
                  'elementType' => Order::class,
                  'criteria'    => ['id' => $cartId],
                  'transformer' => function(Order $order) {
                      $items = [];
  
                      foreach($order->lineItems as $lineItem) {
                          $items[] = [array(
                              'itemTitle' => $lineItem->title
                          )];
                      }
  
                      return [
                          'items' => $items
                      ];
                  }
              ];
          }
      ]
  ]
  ?>
  
  ```
  
  **Tags:** plugin-elementapi, plugin-craftcommerce2
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-12-20. Score: 5 | Accepted answer
  
  The cart is not baked into \Craft, but craft\commerce\Plugin.
  
  Try the following:
  
  ```
  $cart = craft\commerce\Plugin::getInstance()->getCarts()->getCart();
  
  ```
  
  Uses the [following function](https://docs.craftcms.com/commerce/api/v2/craft-commerce-services-carts.html#public-methods):
  
  ```
  /**
   * Get the current cart for this session.
   *
   * @param bool $forceSave Force the cart to save when requesting it.
   * @return Order
   * @throws Exception
   * @throws \Throwable
   * @throws \craft\errors\ElementNotFoundException
   */
  public function getCart($forceSave = false): Order
  
  ```
  
  
  ---
  
  