<h3>Craft 3</h3>
<p>This is how Craft Commerce fetches the Card</p>
<pre><code>if ($orderNumber = $request-&gt;getBodyParam('orderNumber')) {
    // Get the cart from the order number
    $cart = Order::find()-&gt;number($orderNumber)-&gt;isCompleted(false)-&gt;one();
}
</code></pre>
<p>You have to include an <code>orderNumber</code> parameter</p>
<h3>Craft 2</h3>
<pre><code>$number = $this-&gt;_getSessionCartNumber();

if ($cart = $this-&gt;_getCartRecordByNumber($number))
{
    $this-&gt;_cart = Commerce_OrderModel::populateModel($cart);
}
else
{
    $this-&gt;_cart = new Commerce_OrderModel;
    $this-&gt;_cart-&gt;number = $number;
}

/**
 * @return mixed|string
 */
private function _getSessionCartNumber()
{
    $cookieId = $this-&gt;cookieCartId;
    $cartNumber = craft()-&gt;userSession-&gt;getStateCookieValue($cookieId);

    if (!$cartNumber)
    {
        $cartNumber = md5(uniqid(mt_rand(), true));
        $configInterval = craft()-&gt;config-&gt;get('cartCookieDuration', 'commerce');
        $interval = new DateInterval($configInterval);
        $cartExpiry = date_create('@0')-&gt;add($interval)-&gt;getTimestamp();
        craft()-&gt;userSession-&gt;saveCookie($cookieId, $cartNumber, $cartExpiry);
    }

    return $cartNumber;
}
</code></pre>
<p>I think you'll have to change the cookie</p>

CC BY-SA 3.0
2018-04-05T08:17:10.110Z
2018-04-05T08:24:04.737Z
2020-06-17T08:56:27.903Z
Robin Schambach
<p>Running craft cms 2 as a backend with react as the front end I am able to create a cart via an ajax post request to <code>actions/commerce/cart/updateCart</code>.</p>

<p>However, I'm not sure how to then update that cart. Every request to <code>updateCart</code> creates a new cart.</p>

<p>What do I need to pass to <code>updateCart</code> for it to update instead of create a new cart?</p>

CC BY-SA 3.0
2018-04-10T06:50:52.960Z
Turns out it was a lot simpler than I thought.

forgot to add `credentials: 'same-origin',` to the fetch object.
Nick Hu
<plugin-craftcommerce>
update existing cart via ajax creates new cart
# update existing cart via ajax creates new cart

(Asked by: username_0 on 2018-04-05)

Running craft cms 2 as a backend with react as the front end I am able to create a cart via an ajax post request to `actions/commerce/cart/updateCart`.

However, I'm not sure how to then update that cart. Every request to `updateCart` creates a new cart.

What do I need to pass to `updateCart` for it to update instead of create a new cart?

**Tags:** plugin-craftcommerce
> **Comments:**
> **username_0**: Turns out it was a lot simpler than I thought.  forgot to add `credentials: 'same-origin',` to the fetch object.  

---

## 1 answers

### Answer by username_1 on 2018-04-05. Score: 1 | Accepted answer

Craft 3
This is how Craft Commerce fetches the Card
if ($orderNumber = $request->getBodyParam('orderNumber')) {
    // Get the cart from the order number
    $cart = Order::find()->number($orderNumber)->isCompleted(false)->one();
}

You have to include an orderNumber parameter
Craft 2
$number = $this->_getSessionCartNumber();

if ($cart = $this->_getCartRecordByNumber($number))
{
    $this->_cart = Commerce_OrderModel::populateModel($cart);
}
else
{
    $this->_cart = new Commerce_OrderModel;
    $this->_cart->number = $number;
}

/**
 * @return mixed|string
 */
private function _getSessionCartNumber()
{
    $cookieId = $this->cookieCartId;
    $cartNumber = craft()->userSession->getStateCookieValue($cookieId);

    if (!$cartNumber)
    {
        $cartNumber = md5(uniqid(mt_rand(), true));
        $configInterval = craft()->config->get('cartCookieDuration', 'commerce');
        $interval = new DateInterval($configInterval);
        $cartExpiry = date_create('@0')->add($interval)->getTimestamp();
        craft()->userSession->saveCookie($cookieId, $cartNumber, $cartExpiry);
    }

    return $cartNumber;
}

I think you'll have to change the cookie


---

