1

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?

  • 1
    Turns out it was a lot simpler than I thought. forgot to add credentials: 'same-origin', to the fetch object. – Nick Hu Apr 10 '18 at 6:50
1

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

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.