Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-31T10:39:34.860Z
Answers.Text: 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.
Answers.UserDisplayName: Simon Kuran
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-31T09:48:10.953Z
Answers.LastActivityDate: 2020-08-31T09:48:10.953Z
Answers.UserDisplayName: Luke Holder
Body: <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>
  
Tags: <plugin-craftcommerce><donations>
Title: Force donation purchasable to replace donation of the same amount in cart
ThreadText: # 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 `<input type="hidden" name="options[random]" value="{{ random()|hash }}">` 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 %}
  
  <form method="post" action="">
      <input type="hidden" name="action" value="commerce/cart/update-cart">
      {{ csrfInput() }}
      {% if cart.lineItems %}
          {% set donation = cart.lineItems|first %} <!-- since we know only the donation is in the cart -->
          <input type="hidden" name="cartUpdatedNotice" value="Donation updated.">
          <input type="hidden" name="lineItems[{{ donation.id }}]" value="{{ donation.id }}">
          <input id="donation-amount" type="text" name="lineItems[{{ donation.id }}][options][donationAmount]"
                 value="{{ donation.options.donationAmount }}" placeholder="10">
          <input type="submit" value="Update Donation"/>
      {% else %}
          <input type="hidden" name="cartUpdatedNotice" value="Donation added.">
          <input type="hidden" name="purchasableId" value="{{ craft.commerce.donation.id }}">
          <input id="donation-amount" type="text" name="options[donationAmount]" value="" placeholder="10">
          <input type="submit" value="Add Donation"/>
      {% endif %}
  </form>
  
  ```
  
  > **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.  
  
  ---
  
  