jQuery not defined – Gravity Forms

Some solutions are given here: https://stackoverflow.com/questions/23786388/js-error-on-gravity-forms

If we are embedding gravity form in template files and form is outside wp loop, then we need to call gravity_form_enqueue_scripts( $form_id, $is_ajax ); function to force stylesheets and scripts to load in the header.
Documentation: https://docs.gravityforms.com/gravity_form_enqueue_scripts/

Marriage

I was going through some articles on medium and I came across this article.

I am quoting some paragraphs from the article because I believe this is so true and I should keep this in my mind always

Marriage will not always make you happy, but it will do something even better. It will give you an opportunity to find happiness in peace, in letting go, in learning what’s worth fighting for, in figuring out how to love an imperfect person, in seeing what commitment is and what compromise feels like.

Marriage won’t do the work for you. That work is yours—always and forevermore. The real work is not about finding the right person. It’s about becoming the right person.

8 Hours Sleep

I have been working on a night shift for last five years. And sleeping during daytime is not as relaxing as we sleep at night. But to my surprise, I slept continuous 8 hours in last two days (it rarely happens)! and it was really kinda achievement for me. Very much relaxing and refreshing! After a very long time, I got this much long sleep (without any interruption).

Night Shifts and My Team

I’ve been working in night shifts for past 4.5 years and looks like now I’ve become habitual. I don’t feel sleepy during night (If I have got good sleep during my daytime). Things are going fine with me so far but my new team members are struggling to work in night shift. I really love my team members because everyone in my team is so much hardworking and dedicated. And I feel bad as they are falling sick. In recent one month, half of my team was struggling with health issues due to night shift. I am trying to adjust their working hours like few hours their local time and remaining hours on PST. But with this schedule, I get myself alone at PST hours and get so much pressure to work on multiple things at the same time (because no help (team members) available at that time). Confused what to do 🙁

How to add custom notification message on Shopify checkout page

Today, I’ll write Quick tutorial on how to add custom notification message on shopify checkout. We’ll add two notifications, first one will remain visible all the time on shipping address page and second one will appear if customer select shipping country other than “United States”.

Here is the demo.

Here are the implementation steps:

Step 1: Go to Shopify Admin Panel. Under Sales Channel, Click on “Online Store” and after go to “Preferences” page.
Step 2: Under Google Analytics Section, It will have an option “Add Custom JavaScript to Google Analytics”. Just click on that.
Step 3: Paste this code in the script box.

function countryCheck(country) {
	if(country != 'United States'){           
	   Checkout.$("#country-message").show(0);
	} else {
	   Checkout.$("#country-message").hide(0);
	}
}
if(typeof Checkout === 'object'){
	if(typeof Checkout.$ === 'function'){
		//Default Message will be visible where as country specific message will be hidden
		notificationMsg = '<p id="default-message" style="background-color:#eee; padding:10px; margin:10px 0;">This is a default message. It will appear on page load.</p><p id="country-message" style="background-color:#eee; padding:10px; margin:10px 0; display:none;">This message will appear when you select country other than US.</p>';
		
		//Country Selectbox Selectbox
		$countrySelectboxId = Checkout.$("#checkout_shipping_address_country");
		
		// Insert Default Message on Page Load above the shipping methode section
		Checkout.$(notificationMsg).insertBefore('.section--shipping-address');
		
		// Check Country on Page Load
		countryName = $countrySelectboxId.val();
		
		// Call Country Check function on Page Load
        countryCheck(countryName)

		// Call Country Check function on selectobox change
		$countrySelectboxId.on("change", function(){
			countryName = $countrySelectboxId.val();
			countryCheck(countryName)
		});
		
		
	} // if ends

} // if ends

Now, Just save and then test.

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

Difficult Decision

Firing/Letting someone go is one of the most difficult decisions for management. Today I sent a goodbye email to one of our developers. I tried to be very much polite and positive and also explained about the reason of this difficult decision.

I give enough chances to team member to improve and even work with them to improve their skills, offer learning sessions over weekends and try my best to keep them in the team. But if team member does not show enthusiasm and willingness to improve, we had to take this decision. 🙁

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