<p>I tried this myself and it threw an error when there was no cart attached to the session.</p>

<p>I then found these two lines of code in the Craft Commerce <a href="https://github.com/craftcms/commerce/blob/develop/src/controllers/CartController.php" rel="nofollow noreferrer">CartController.php</a>:</p>

<pre><code>        // Get the cart from the current users session, or return a new cart attached to the session
        $cart = Plugin::getInstance()-&gt;getCarts()-&gt;getCart(true);
</code></pre>

<p>getCart() can be found in the <a href="https://github.com/craftcms/commerce/blob/develop/src/services/Carts.php" rel="nofollow noreferrer">Carts Service</a></p>

<p>Which lead me to try:</p>

<pre><code>{% set cart = craft.commerce.carts.cart(true) %}
</code></pre>

<p>So if you pass in true, you force it to save the cart and thus you seem to be able to create a new (empty) cart.</p>

CC BY-SA 4.0
2018-12-21T21:22:31.453Z
2018-12-21T21:22:31.453Z
MisterMike
<p>I'm trying to add a product to a cart (and emptying the cart first) if a user just visits a template. My goal is to make a one-page checkout: so I want the address info on the product page itself. This is what I have so far. I thought the first line would make a cart if there is no cart yet, but it seems nothing happens. (<code>cart.id</code> = <code>null</code>)</p>

<pre><code>{% set cart = craft.commerce.carts.cart %}
Cart ID is {{ cart.id }}
{% do cart.setLineItems([]) %}
{% set lineItem = craft.commerce.lineitems.resolveLineItem(cart.id, product.id, [], 1, '') %}
{% do cart.addLineItem(lineItem) %}
</code></pre>

<plugin-craftcommerce>
Trying to automatically add a product in Commerce
# Trying to automatically add a product in Commerce

(Asked by: username_0 on 2018-12-21)

I'm trying to add a product to a cart (and emptying the cart first) if a user just visits a template. My goal is to make a one-page checkout: so I want the address info on the product page itself. This is what I have so far. I thought the first line would make a cart if there is no cart yet, but it seems nothing happens. (cart.id = null)
{% set cart = craft.commerce.carts.cart %}
Cart ID is {{ cart.id }}
{% do cart.setLineItems([]) %}
{% set lineItem = craft.commerce.lineitems.resolveLineItem(cart.id, product.id, [], 1, '') %}
{% do cart.addLineItem(lineItem) %}

**Tags:** plugin-craftcommerce


---

## 1 answers

### Answer by username_1 on 2018-12-21. Score: 1 | Accepted answer

I tried this myself and it threw an error when there was no cart attached to the session.
I then found these two lines of code in the Craft Commerce CartController.php:
        // Get the cart from the current users session, or return a new cart attached to the session
        $cart = Plugin::getInstance()->getCarts()->getCart(true);

getCart() can be found in the Carts Service
Which lead me to try:
{% set cart = craft.commerce.carts.cart(true) %}

So if you pass in true, you force it to save the cart and thus you seem to be able to create a new (empty) cart.


---

