I am very new to .liquid (Ruby) coding and have just started a store that requires customer enter their own amount on a donation product (not in the cart). I found this tutorial online (https://byaman.com/creating-donation-product-desired-amount-option-shopify) but it was written 4 years ago for the timber theme [which is no longer supported or available by Shopify].
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.
I am not sure how to apply this to the SIMPLE theme that I am using so any suggestions on translating/re-writing the code would be helpful. Thanks in advance.
Here are the implementation steps.
[You can also check the final code files on github: https://github.com/amandeepsinghvirdi/donation-product-shopify]
Step 1: Create donation product with different variants. See the picture below:
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
|
1
2
3
4
5
|
{{ ‘products.product.quantity’ | t }}
{{ ‘products.product.add_to_cart’ | t }}
|
| - | - |
and then replace the above code with this one.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Enter Amount:
{{ ‘products.product.add_to_cart’ | t }}
|
| - | - |
Step 4: Now try to locate OptionSelectors callback function. Which looks like this:
1 2 3 4 5 6 7 |
var selectCallback = function(variant, selector) { timber.productPage({ money_format: “{{ shop.money_format }}”, variant: variant, selector: selector }); }; // selectCallback Ends |
|---|
and replace it with this one:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// 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.
|
1
2
3
4
5
6
7
8
|
{{ item.price | money }}
|
| - | - |
and replace it with this one:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
{% if item.url contains ‘donation-product’ %}
{% else %}
{{ item.price | money }}
{% endif %}
|
| - | - |
And thats it.
