Creating Donation Product (with Desired Amount option) in Shopify

Every weekend I am planning to write some open source code snippets for Shopify platform. Today I’ll be demonstrating how to create Donation product in shopify which will have option to add desired donation amount. To accomplish this we’ll create a donation product with different variants (with single option “Amount”) each variant will have different price. And we’ll create a product template product.donation.liquid and assign this template to the newly created product. and we’ll make small changes to cart page template (to hide quantity selector and price option for variant).

Here is the demo.

Here are the implementation steps.

Step 1: Create donation product with different variants. See the picture below:

donation-product-variants

Step 2: Create a new product template (product.donation.liquid) and then assign it to newly created product.

Step 3:I am using timber framework for example. Open Product template and try to locate this code

          <label for="Quantity" class="quantity-selector">{{ 'products.product.quantity' | t }}</label>
          <input type="number" id="Quantity" name="quantity" value="1" min="1" class="quantity-selector">
          <button type="submit" name="add" id="AddToCart" class="btn">
            <span id="AddToCartText">{{ 'products.product.add_to_cart' | t }}</span>
         </button>

and then replace the above code with this one.

		 <!-- End: Donation Product Starts -->
          <div id="other-amount" style="display:none;">
            <label for="OtherAmount">Enter Amount:</label>
            <input type="text" id="OtherAmount"  />
          </div>
          <input type="hidden" id="Quantity" name="quantity" value="1" min="1" class="quantity-selector" />
            <div id="addto-cart-wrap">
              <button type="submit" name="add" id="AddToCart" class="btn">
                <span id="AddToCartText">{{ 'products.product.add_to_cart' | t }}</span>
              </button>
            </div>
			
		 <!-- End: Donation Product Code -->

Step 4: Now try to locate OptionSelectors callback function. Which looks like this:

var selectCallback = function(variant, selector) {
    timber.productPage({
      money_format: "{{ shop.money_format }}",
      variant: variant,
      selector: selector
    });
  }; // selectCallback Ends

and replace it with this one:

  var selectCallback = function(variant, selector) {
    timber.productPage({
      money_format: "{{ shop.money_format }}",
      variant: variant,
      selector: selector
    });
    
	// Donation Product Code Starts
    variantName = (variant.title).toLowerCase();    
    if(variantName == 'other'){
    	$('#other-amount').show(0);
        $('#OtherAmount').prop('required', true).val('');
        $('#ProductPrice').hide(0);
    } else {
    	$('#other-amount').hide(0);
        $('#OtherAmount').prop('required', false).val('');
        $('#Quantity').val(1)
        $('#ProductPrice').show(0);
    }    
	// Donation Product Code Ends    
  }; // selectCallback Ends

Above code will try to match “other” variant every time a variant is changed. And if its found then it will show up a text box to enter the desired amount.

Step 5: Now put this js code under document ready function. This code will update the pricing and quantity box. “Other Variant” has pricing as 1. So when someone will enter desired amount, we’ll update the product quantity and also show the price.

// Donation Product Code Starts
    $('#OtherAmount').keyup(function(){
    	console.log($(this).val());
        otherAmountValue = $(this).val();
          if(otherAmountValue == 0){
            alert('Please enter some value');
            $(this).val('');
            return false;
          }
      
        AmountPriceFormat = '$'+otherAmountValue;
        $('#Quantity').val(otherAmountValue);
        $('#ProductPrice').show(0).text(AmountPriceFormat);
    });
// Donation Product Code Ends

Step 6 (optional): If you want to hide your quantity and donation product price on cart page then follow this step. Try to locate this code in cart.liquid template.

            <td data-label="{{ 'cart.label.price' | t }}">
              <span class="h3">
                {{ item.price | money }}
              </span>
            </td>
            <td data-label="{{ 'cart.label.quantity' | t }}">
              <input type="number" name="updates[]" id="updates_{{ item.id }}" value="{{ item.quantity }}" min="0">
            </td>

and replace it with this one:

<!-- Donation Products Code Stars -->
            {% if item.url contains 'donation-product' %}
              <td data-label="{{ 'cart.label.price' | t }}"></td>
              <td data-label="{{ 'cart.label.quantity' | t }}"></td>            
            {% else %}
            <td data-label="{{ 'cart.label.price' | t }}">
              <span class="h3">
                {{ item.price | money }}
              </span>
            </td>
            <td data-label="{{ 'cart.label.quantity' | t }}">
              <input type="number" name="updates[]" id="updates_{{ item.id }}" value="{{ item.quantity }}" min="0" />
            </td>            
            {% endif %}
	<!-- Donation Products Code Ends -->

And thats it. Now you can test. If you find any issue, please leave a comment.

You can also check the final code files on github: https://github.com/amandeepsinghvirdi/donation-product-shopify

Published by

Amandeep

Blogger & Web Developer having expertise in Shopify, BigCommerce, WordPress and Front-End Technologies (HTML, CSS and JavaScript).

4 thoughts on “Creating Donation Product (with Desired Amount option) in Shopify”

  1. Nice Trick!
    I was thinking to create a product with price $1 and change the Quantity label to Amount (inspired by your method).

  2. Hi, I am hopeless at any sort of programming and this is exactly what I need for my site but It’s 4 years old and the timber theme is no longer supported by Shopify. I have attemped your walk through but i couldn’t work it out.

    Any chance you would like the challenge of reworking this for the SIMPLE theme?

Leave a Reply

Your email address will not be published. Required fields are marked *

seven + ten =