<p>Anywhere you have a add donation to cart form, you could check if the donation is in the cart and update it instead:</p>
<pre><code>{% set cart = craft.commerce.carts.cart %}

&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;action&quot; value=&quot;commerce/cart/update-cart&quot;&gt;
    {{ csrfInput() }}
    {% if cart.lineItems %}
        {% set donation = cart.lineItems|first %} &lt;!-- since we know only the donation is in the cart --&gt;
        &lt;input type=&quot;hidden&quot; name=&quot;cartUpdatedNotice&quot; value=&quot;Donation updated.&quot;&gt;
        &lt;input type=&quot;hidden&quot; name=&quot;lineItems[{{ donation.id }}]&quot; value=&quot;{{ donation.id }}&quot;&gt;
        &lt;input id=&quot;donation-amount&quot; type=&quot;text&quot; name=&quot;lineItems[{{ donation.id }}][options][donationAmount]&quot;
               value=&quot;{{ donation.options.donationAmount }}&quot; placeholder=&quot;10&quot;&gt;
        &lt;input type=&quot;submit&quot; value=&quot;Update Donation&quot;/&gt;
    {% else %}
        &lt;input type=&quot;hidden&quot; name=&quot;cartUpdatedNotice&quot; value=&quot;Donation added.&quot;&gt;
        &lt;input type=&quot;hidden&quot; name=&quot;purchasableId&quot; value=&quot;{{ craft.commerce.donation.id }}&quot;&gt;
        &lt;input id=&quot;donation-amount&quot; type=&quot;text&quot; name=&quot;options[donationAmount]&quot; value=&quot;&quot; placeholder=&quot;10&quot;&gt;
        &lt;input type=&quot;submit&quot; value=&quot;Add Donation&quot;/&gt;
    {% endif %}
&lt;/form&gt;
</code></pre>

CC BY-SA 4.0
2020-08-31T10:39:34.860Z
I mentioned that I tried this technique there was some ugliness when the user used the back button after entering an invalid donation amount before correcting it. That might just be a browser quirk I have to work around. Glad to know this is the recommended method, thanks.
Simon Kuran
CC BY-SA 4.0
2020-08-31T09:48:10.953Z
2020-08-31T09:48:10.953Z
Luke Holder
<p>I'm using Craft Commerce Lite and the donation purchasable. I don't want users to ever be able to add more than one donation to the cart. Using the Lite edition stops users from being able to add donations with different amounts since those would need two line items. In the docs for <a href="https://craftcms.com/docs/commerce/3.x/donations.html#adding-the-donation-to-the-cart" rel="nofollow noreferrer">Adding the Donation to the Cart</a></p>
<blockquote>
<p>Customers can add more than one donation to the cart, but the line item will be replaced if the optionSignature remains the same, just like it does for any purchasables added to the cart. (For example, two donations added for the same amount will be consolidated into one line item with a quantity of 2.)</p>
<p>Once the donation is in the cart, the donation amount can also be updated using the standard line item option update form. You would normally hide the qty field, because while it continues to work it’s most common for customers to donate a single amount.</p>
</blockquote>
<p>How do I disable this consolidation feature?</p>
<p>Adding <code>&lt;input type=&quot;hidden&quot; name=&quot;options[random]&quot; value=&quot;{{ random()|hash }}&quot;&gt;</code> works, but that displays in the CP so it's not clean.</p>
<p>I also tried showing a commerce/cart/update-cart form on the donation page that redirects to the payment page (there's no user viewable cart on this site) when the cart isn't empty, it works, but it seems like this shouldn’t be necessary. It also can show the user incorrect data if they enter an invalid donation amount, switch to a valid amount, go to the payment page, and then hit the back button. The invalid update cart form will be shown.</p>
<p>What's the simplest way to streamline all of this this?</p>

<plugin-craftcommerce><donations>
Force donation purchasable to replace donation of the same amount in cart
# Force donation purchasable to replace donation of the same amount in cart

(Asked by: username_0 on 2020-08-31)

I'm using Craft Commerce Lite and the donation purchasable. I don't want users to ever be able to add more than one donation to the cart. Using the Lite edition stops users from being able to add donations with different amounts since those would need two line items. In the docs for [Adding the Donation to the Cart](https://craftcms.com/docs/commerce/3.x/donations.html#adding-the-donation-to-the-cart)

> Customers can add more than one donation to the cart, but the line item will be replaced if the optionSignature remains the same, just like it does for any purchasables added to the cart. (For example, two donations added for the same amount will be consolidated into one line item with a quantity of 2.)
>
> Once the donation is in the cart, the donation amount can also be updated using the standard line item option update form. You would normally hide the qty field, because while it continues to work it’s most common for customers to donate a single amount.

How do I disable this consolidation feature?

Adding  works, but that displays in the CP so it's not clean.

I also tried showing a commerce/cart/update-cart form on the donation page that redirects to the payment page (there's no user viewable cart on this site) when the cart isn't empty, it works, but it seems like this shouldn’t be necessary. It also can show the user incorrect data if they enter an invalid donation amount, switch to a valid amount, go to the payment page, and then hit the back button. The invalid update cart form will be shown.

What's the simplest way to streamline all of this this?

**Tags:** plugin-craftcommerce, donations


---

## 1 answers

### Answer by username_1 on 2020-08-31. Score: 3 | Accepted answer

Anywhere you have a add donation to cart form, you could check if the donation is in the cart and update it instead:
{% set cart = craft.commerce.carts.cart %}


    
    {{ csrfInput() }}
    {% if cart.lineItems %}
        {% set donation = cart.lineItems|first %} 
        
        
        
        
    {% else %}
        
        
        
        
    {% endif %}

> **Comments:**
> **username_0**: I mentioned that I tried this technique there was some ugliness when the user used the back button after entering an invalid donation amount before correcting it. That might just be a browser quirk I have to work around. Glad to know this is the recommended method, thanks.  

---

