Add a shipping rates calculator to your cart page

Caution

This is an advanced tutorial and is not supported by Shopify. Knowledge of web design languages such as HTML, CSS, Javascript and Liquid is required. We suggest hiring a Shopify Expert if you are not comfortable proceeding with the following tutorial.

You can add a shipping rates calculator to your cart page that lets customers estimate their shipping costs before they proceed to the checkout page.

Shipping rates calculator

Shipping calculator information and features

The shipping rates calculator displays your shipping rates on the cart page of your store. If a customer is logged in, then the calculator uses the customer's default shipping address to estimate shipping rates. The shipping rates calculator works with carrier-calculated rates, manual rates, or a combination of the two.

Keep the following in mind when setting up your shipping calculator:

  • In some situations, the rates are approximations because only the country, province/state, and postal/zip code are provided for the calculation, rather than the full address. The exact rates are given at checkout.
  • The displayed text can be translated.
  • Rates are formatted in your shop's currency and include the currency descriptor, such as CAD or USD.
  • The HTML for the calculator is easy to edit if you know the basics.
  • The calculator uses an Underscore.js template and relies on a JSON API. It uses Ajax to fetch rates from Shopify.
  • There is no equivalent API to calculate applicable taxes on the cart page. Applicable taxes are added at checkout.

Editing your theme code to add the shipping calculator

  1. Find the theme you want to edit, and then click Actions > Edit code.

  2. In the Assets directory, click vendor.js. If your theme doesn't have a vendor.js file, then click theme.js instead.

  3. To the very bottom of vendor.js, paste this code hosted on GitHub. If you are editing theme.js instead, then paste the same code snippet at the very top of the file.

  4. Click Save.

  5. In the Assets directory, click theme.js. To the very bottom of the file, paste the following code:

    Shopify.Cart.ShippingCalculator.show( {
      submitButton: theme.strings.shippingCalcSubmitButton,
      submitButtonDisabled: theme.strings.shippingCalcSubmitButtonDisabled,
      customerIsLoggedIn: theme.strings.shippingCalcCustomerIsLoggedIn,
      moneyFormat: theme.strings.shippingCalcMoneyFormat                                     
    } );
    
  6. Click Save.

  7. In the Snippets directory, click Add a new snippet.

  8. Name your new snippet shipping-calculator, and click Create snippet:

    Create shipping-calculator snippet

    Your new snippet will open in the code editor.

  9. Into your new shipping-calculator.liquid snippet, paste this code hosted on GitHub.

  10. Click Save.

  11. In the Sections directory, click cart-template.liquid. If your theme doesn't have a cart-template.liquid file, then, in the Templates directory, click cart.liquid.

  12. Find the closing </form> tag. On a new line right above the closing </form> tag, paste the following code:

    {% include 'shipping-calculator' %}
    
  13. At the very bottom of the file, paste the following code:

    <script>
      theme.strings = {
          shippingCalcSubmitButton: {{ settings.shipping_calculator_submit_button_label | default: 'Calculate shipping' | json }},
          shippingCalcSubmitButtonDisabled: {{ settings.shipping_calculator_submit_button_label_disabled | default: 'Calculating...' | json }},
          {% if customer %}shippingCalcCustomerIsLoggedIn: true,{% endif %}
          shippingCalcMoneyFormat: {{ shop.money_with_currency_format | json }}
      }
    </script>
    
    <script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.10/handlebars.min.js"></script>
    <script src="/services/javascripts/countries.js"></script>
    <script src="{{ 'shopify_common.js' | shopify_asset_url }}" defer="defer"></script>
    
  14. Click Save.

  15. In the Config directory, click settings_schema.json. The file will open in the code editor:

    settings_schema.json file in the code editor
  16. Near the very bottom of the file, paste the following code before the last square bracket ] and after the last curly bracket } - make sure to include that first comma , since you're modifying a JSON data structure:

    ,
      {
        "name": "Shipping Rates Calculator",
        "settings": [
          {
            "type": "select",
            "id": "shipping_calculator",
            "label": "Show the shipping calculator?",
            "options": [
              {
                "value": "Disabled",
                "label": "No"
              },
              {
                "value": "Enabled",
                "label": "Yes"
              }
            ],
            "default": "Enabled"
          },
          {
            "type": "text",
            "id": "shipping_calculator_heading",
            "label": "Heading text",
            "default": "Get shipping estimates"
          },
          {
            "type": "text",
            "id": "shipping_calculator_default_country",
            "label": "Default country selection",
            "default": "United States"
          },
          {
            "type": "paragraph",
            "content": "If your customer is logged-in, the country in his default shipping address will be selected. If you are not sure about the  spelling to use here, refer to the first checkout page."
          },
          {
            "type": "text",
            "id": "shipping_calculator_submit_button_label",
            "label": "Submit button label",
            "default": "Calculate shipping"
          },
          {
            "type": "text",
            "id": "shipping_calculator_submit_button_label_disabled",
            "label": "Submit button label when calculating",
            "default": "Calculating..."
          },
          {
            "type": "paragraph",
            "content": "Do not forget to include the snippet shipping-calculator in your cart.liquid template where you want the shipping  calculator to appear. You can get the snippet here: [shipping-calculator.liquid](https:\/\/github.com\/carolineschnapp\/shipping-calculator\/blob\/master\/shipping-calculator.liquid) ."
          }
        ]
      }
    

Configuring your shipping rates calculator

You can now edit the settings for your shipping rates calculator from the theme editor.

  1. Go to the theme editor.

  2. Under General settings, click Shipping Rates Calculator to view and edit the calculator settings.

    You can configure the following settings:

    • Show the shipping calculator? - set this to Yes to display the shipping rates calculator on your cart page, or No to hide it
    • Heading text - enter the text that will be displayed above your shipping rates calculator
    • Default country selection - choose which country will be selected by default
    • Submit button label - enter the text that will be shown on the submit button.

Editing the HTML or CSS of the calculator

Editing the shipping calculator HTML

You can edit the HTML code for your shipping rates calculator to make more advanced customizations.

  1. Find the theme you want to edit, and then click Actions > Edit code.

  2. In the Snippets directory, click shipping-calculator.liquid.

  3. Edit the code as needed. You can add new classes and move the existing HTML elements around in the file to suit your needs.

  1. Click Save.

Editing the shipping calculator CSS

If you want to change the appearance of your shipping rates calculator, then you can add some CSS to your theme stylesheet.

  1. Find the theme you want to edit, and then click Actions > Edit code.

  2. In the Assets directory, click theme.scss.liquid.

  3. At the very bottom of the file, add either the contents of this CSS file, or your own custom CSS code.

  4. Click Save.

Troubleshooting

Depending on your theme and currency settings, you might see your shipping calculator price estimates displayed on the cart page with un-rendered HTML tags:

Price estimate with un-rendered HTML tags

To remove these unwanted tags, you will need to make a change to your currency settings in the admin.

  1. In the Standards and formats section, your shop currency is listed. Click Change formatting:

    Change formatting link
  2. Remove the HTML tags from your currency formats. By default, the settings look like this:

    Default currency settings

    From both the HTML with currency and the HTML without currency text fields, remove the opening and closing HTML span tags. The result should look like this:

    Edited currency settings
  3. Click Save.

Your shipping calculator price estimates should now display without un-rendered HTML tags:

Price estimate without un-rendered HTML tags

Demo store

This demo shop has a shipping rates calculator on the cart page.

Shipping calculator information and features

The shipping rates calculator displays your shipping rates on the cart page of your store. If a customer is logged in, then the calculator uses the customer's default shipping address to estimate shipping rates. The shipping rates calculator works with carrier-calculated rates, manual rates, or a combination of the two.

Keep the following in mind when setting up your shipping calculator:

  • In some situations, the rates are approximations because only the country, province/state, and postal/zip code are provided for the calculation, rather than the full address. The exact rates are given at checkout.
  • The displayed text can be translated.
  • Rates are formatted in your shop's currency and include the currency descriptor, such as CAD or USD.
  • The HTML for the calculator is easy to edit if you know the basics.
  • The calculator uses an Underscore.js template and relies on a JSON API. It uses Ajax to fetch rates from Shopify.
  • There is no equivalent API to calculate applicable taxes on the cart page. Applicable taxes are added at checkout.

Installing the shipping rates calculator

There are five main steps to install the shipping rates calculator in your theme:

  1. Uploading the file jquery.cart.min.js to your theme assets
  2. Adding settings to the theme editor
  3. Creating a shipping-calculator snippet
  4. Including your snippet in cart.liquid
  5. Configuring your shipping rates calculator

Uploading jquery.cart.min.js to your theme assets

  1. Find the theme you want to edit, and then click Actions > Edit code.

  2. In the Assets directory, click Add a new asset.

  3. Click the Create a blank file tab, and enter jquery.cart.min as the name and select .js as the file extension. Click Add asset.

    Add new asset x1

    Your new blank asset will open in the code editor.

  4. Into your new jquery.cart.min asset, paste this JavaScript code snippet.

  5. Click Save.

Adding settings to the theme editor

  1. In the Config directory, click settings_schema.json. The file will open in the code editor:

    Settings html
  2. Near the very bottom of the file, paste the following code before the last square bracket ] and after the last curly bracket } - make sure to include that first comma , since you're modifying a JSON data structure:

    ,
      {
        "name": "Shipping Rates Calculator",
        "settings": [
          {
            "type": "select",
            "id": "shipping_calculator",
            "label": "Show the shipping calculator?",
            "options": [
              {
                "value": "Disabled",
                "label": "No"
              },
              {
                "value": "Enabled",
                "label": "Yes"
              }
            ],
            "default": "Enabled"
          },
          {
            "type": "text",
            "id": "shipping_calculator_heading",
            "label": "Heading text",
            "default": "Get shipping estimates"
          },
          {
            "type": "text",
            "id": "shipping_calculator_default_country",
            "label": "Default country selection",
            "default": "United States"
          },
          {
            "type": "paragraph",
            "content": "If your customer is logged-in, the country in his default shipping address will be selected. If you are not sure about the  spelling to use here, refer to the first checkout page."
          },
          {
            "type": "text",
            "id": "shipping_calculator_submit_button_label",
            "label": "Submit button label",
            "default": "Calculate shipping"
          },
          {
            "type": "text",
            "id": "shipping_calculator_submit_button_label_disabled",
            "label": "Submit button label when calculating",
            "default": "Calculating..."
          },
          {
            "type": "paragraph",
            "content": "Do not forget to include the snippet shipping-calculator in your cart.liquid template where you want the shipping  calculator to appear. You can get the snippet here: [shipping-calculator.liquid](https:\/\/github.com\/carolineschnapp\/shipping-calculator\/blob\/master\/shipping-calculator.liquid) ."
          }
        ]
      }
    
  3. Click Save.

Creating a shipping-calculator snippet

  1. Find the theme you want to edit, and then click Actions > Edit code.

  2. In the Snippets directory, click Add a new snippet.

  3. Enter shipping-calculator as the name for your new snippet, then click Create snippet:

    Add shipping calculator snippet

    Your new blank snippet will open in the code editor.

  4. Into your new shipping-calculator snippet, paste this Liquid code snippet.

  5. Click Save.

Including the snippet in cart.liquid

Include your snippet in your cart.liquid file in the place where you want to show your shipping rates calculator. In this example, the calculator is right below the cart form on the cart page.

  1. Find the theme you want to edit, and then click Actions > Edit code.

  2. In the Templates directory, click cart.liquid.

  3. Find the closing </form> tag. On a new line below the tag, paste the following code:

    {% include 'shipping-calculator' %}
    
  4. Click Save.

Configuring your shipping rates calculator

You can now edit the settings for your shipping rates calculator from the theme editor.

  1. Go to the theme editor.

  2. Under General settings, click Shipping Rates Calculator to view and edit the calculator settings.

    You can configure the following settings:

    • Show the shipping calculator? - set this to Yes to display the shipping rates calculator on your cart page, or No to hide it
    • Heading text - enter the text that will be displayed above your shipping rates calculator
    • Default country selection - choose which country will be selected by default
    • Submit button label - enter the text that will be shown on the submit button.

Editing the HTML or CSS of the calculator

Editing the shipping calculator HTML

You can edit the HTML code for your shipping rates calculator to make more advanced customizations.

  1. Find the theme you want to edit, and then click Actions > Edit code.

  2. In the Snippets directory, click shipping-calculator.liquid.

  3. Edit the code as needed. You can add new classes and move the existing HTML elements around in the file to suit your needs.

  1. Click Save.

Editing the shipping calculator CSS

If you want to change the appearance of your shipping rates calculator, then you can add some CSS to your theme stylesheet.

  1. Find the theme you want to edit, and then click Actions > Edit code.

  2. In the Assets directory, click theme.scss.liquid.

  3. At the very bottom of the file, add either the contents of this CSS file, or your own custom CSS code.

  4. Click Save.

Troubleshooting

Updating older themes to use a supported version of jQuery

If you are having trouble getting the shipping calculator to work, then check to confirm that your theme is using a jQuery version of 1.7 or newer. If your theme is running a version of jQuery that is older than 1.7, then you can edit your theme code to use a supported version instead.

  1. In the Layout directory, click theme.liquid.

  2. Within the <head> element, find a script tag that references your theme's jQuery source. The src attribute for the script tag contains a URL that includes /jquery/, followed by the version number. The tag looks something like this:

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    

    In the script tag above, the jQuery version being used is 1.4.2. This is an older version of jQuery that will need to be updated for the customization to work. If your theme is using a version that is older than 1.7, replace the version number in the URL with 1.7. The result should look like this:

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
    
  3. Click Save.

Demo store

This demo shop has a shipping rates calculator on the cart page.

Want to discuss this page?

Visit the Shopify Community