2

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

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?

1 Answer 1

3

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>
1
  • 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. Aug 31, 2020 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.