Skip to content Skip to sidebar Skip to footer

How to Do an Html File Upload on Shopify

You want to give your customers the ability to upload files when they add a product to their shopping cart.

This functionality can be required, for example, if you want to offering personalized products on Shopify or if you lot need some additional files to fulfill an order.

Nosotros are going to show you two variants on how yous can add a file upload choice to your Shopify production pages.

Variant one - Implement information technology yourself

Shopify does back up file upload when adding an item to the cart. To make use of the functionality, nosotros have to extend our theme with some additional code to brandish a file upload form item and make sure that the file is uploaded when a customer clicks the "add together to cart" push button. The implementation details depend on the specific theme and how the products are added to the cart.

If your theme has an AJAX cart implementation, it is not guaranteed to work out of the box.

We are going to show you how you lot can implement a file upload on the Shopify Debut theme, including AJAX cart support!

Variant 2 - Using a Shopify App

There are various Shopify apps that allow you to add a file upload grade on your product. We've developed a new app that solves a lot of issues similar file validations and custom CSS for you. By installing the app, y'all exercise not have to make any theme modifications to enable file uploads on your production pages.

You could argue that the whole reason that nosotros wrote this weblog mail is to tell you about the app! But seriously, you could build information technology yourself, and we are going to show you lot how, take a bones file upload that works only with your current theme, or you can use the app, which provides a beautiful widget with a lot of customization options.

The app has a free program that allows an unlimited number of uploads. Y'all can find it on the Shopify App Store.

Upload-Lift Shopify App

You lot can skip ahead to come across the app in activity.

Implementing file upload on Shopify Debut theme

The following instructions are specific to the Debut theme on Shopify.

Tough the full general setup tin also be applied to other themes.

Creating a custom production template

To be able to add a file upload choice to specific products on our shop, we first create a new product template file and phone call it "product.upload.liquid".

If yous exercise not know how to add a new template, you can follow this Shopify help doc Create alternating templates.

Next, nosotros copy the lawmaking of the default "templates/product. liquid" into the new custom template. (The theme editor does this automatically)

Afterward the template is saved, we can configure it on a product past selecting it on the product page in the Shopify admin under "Theme templates" in the bottom correct corner.

Creating a custom section

Our template file currently includes a department called "product-template." This file is located in the "sections" binder, and it contains the central part of the product page. To implement our new file upload feature, we best create a new section and phone call information technology "product-upload-template.liquid".

Next, we once more copy the code of the default "sections/product-template.liquid" into the new custom section.

Now we can update our new "product.upload.liquid" template to bespeak to our new department.

            {% section 'product-upload-shopify-template' %}                      

Shopify cart items backdrop

To store the link of the uploaded file somewhere on the cart, we tin use custom cart line particular properties.

When a customer adds a production to cart , a new cart line_item  object is added to the cart.items array. The line_item object contains all the necessary information about the called production, like product variant and price.

The line_item.properties  aspect is an array that can store additional custom information. These properties are saved when an club is created. That makes them a not bad place to store the link to the uploaded file.

Creating the file input chemical element

To create a custom line_item.backdrop attribute when calculation a product to cart, we create an additional form field on the production form.

In "sections/product-upload-template.liquid" we add the following HTML after to the product course after the variant pick:

                          {% form 'product', product, class:form_classes, novalidate: 'novalidate', information-product-form: '' %} ... {% comment %}     Custom form field to add together a line_item.backdrop entry with the name "upload", containing the link to the uploaded file. {% endcomment %} <div class="production-form__item product-form__item--upload">    <label for="upload">Upload</label>    <input required class="required" id="upload" blazon="file" proper noun="properties[upload]" class="production-form__input"> </div> ... {% endform %}                      

The name of the property is set via the name="properties[...]" attribute of the input tag. You can choose a different property proper noun that makes sense for your utilise case.

After we saved the department file and our theme is updated, we can navigate to the production with the template and encounter a file upload input class detail appear higher up the add to cart button.

If yous don't see the upload particular on your production page, check the previous steps and make sure that you lot've selected the new template on the product configuration page.

Making the cart upload piece of work

When at present can select a file and add the product to cart. We can verify that the product was successfully added to the cart, but the "upload" belongings is missing on the line_item.properties.

The reason it is not working out of the box is that the Debut theme default cart implementation is using AJAX to telephone call the add together.js API to add together line_items to the cart. Unfortunately, this skips the file upload procedure entirely.

We accept ii options to ready this.

Pick 1 - Disable Ajax cart

The Debut theme settings contain a property to enable/disable the ajax cart.

In the Shopify Admin Themes section, click on "Customize" on your current Debut based theme. Navigate to the "Theme settings" tab. Click on the "Add to cart notification" section.

Uncheck the checkbox "Prove notification when item is added to cart".

Make sure that you save the theme settings.

The setting is reflected as the property "enable_ajax" in the config file "config/settings_data.json".

When nosotros now try again and select a file and add it to cart, we should see an upload property displayed on the cart line_item on the cart page.

The downside of this approach is that now all of our other products tin non make use of the cart ajax functionality, particularly the cart popup in the top right is non shown anymore. This causes an overall worse user feel.

Selection 2 - Alter theme.js to support AJAX file uploads

If you disabled the "enable_ajax" setting from the first option. Enable information technology over again. Nosotros know y'all want to have that nice cart popup functionality back!

Nosotros have to modify some of the theme's javascript code to make the ajax cart support file uploads.

Open the "assets/theme.js" file and search for the "_addItemToCart" role. Rename the function, add a suffix "_original" to be able to restore to the original part if needed.

                          _addItemToCart_original: function(information) { ...  }                      

Adjacent, add we add our new implementation beneath the original i:

                          // NEW: implementation to permit ajax file uploads & required checks     _addItemToCart: function($class) {        // You need to utilise standard javascript object here       // create grade information with all the fields from the form       // FormData is >=IE10       var formData = new FormData($form[0]);        var params = {         type: 'Post',         contentType: false, // NEEDED, DON'T OMIT THIS (requires jQuery i.6+)         processData: fake, // NEEDED, DON'T OMIT THIS         url: '/cart/add.js',         data: formData,         dataType: 'json',         success: office(item) {           this._hideErrorMessage();           this._setupCartPopup(item);         }.demark(this),         error:  office(response) {           this.$previouslyFocusedElement.focus();           var errorMessage = response.responseJSON               ? response.responseJSON.description               : theme.strings.cartError;           this._showErrorMessage(errorMessage);           this._handleButtonLoadingState(simulated);         }.bind(this)       };        // execute ajax add together to cart       $.ajax(params);     },                      

Our new office makes apply of the FormData Javascript object to parse and send all form fields, including our new upload field. Equally commented in the code, the FormData only supports IE >= 10.

We then pass the formData as the data parameter to the jQuery.ajax() method. The success and error handlers are the aforementioned equally in the original office.

If we now try again and select a file and add it to cart, nosotros should come across the cart popup with our upload property displayed on the cart line_item in the top right corner.

Making the file upload required

As you may have noticed, nosotros set the "required" attribute on the upload input field. But when we click on "Add to cart" without selecting a file, the production is still added to cart! The reason for this is again the Ajax cart skips the browser native course validation. We have to implement it ourselves in the "_addItemToCart" function.

We add the following code to check if all the inputs that are marked as required have a value. If not, nosotros bear witness an error on the folio and skip calculation the production to the cart.

                          _addItemToCart: part($form) {        var valid = true;        // required fields validation       $.each($form.discover('input.required'), function (index, chemical element) {         if(!$(element).val()){           valid = fake;         }       });        if(!valid){         this._showErrorMessage('missing required fields');         this._handleButtonLoadingState(simulated);         return;       }              ...    }                      

If nosotros at present endeavor to add a product to the cart without selecting a file get-go, nosotros see an error message displayed on the page.

Using the Upload-Elevator App

If you lot don't want to implement the upload functionality yourself, or if y'all want a more customizable and more pleasing upload widget, then using an app like Upload-Elevator is the right way to go. You can install it direct from the Shopify App Store.

Creating a file upload field

Subsequently you have installed the app, you will meet a UI to create a new upload field. On the UI, we can configure the form input settings like required and the name of the properties. Nosotros tin can also prepare the look and feel of the widget to match with our theme colors.

In the display settings section, we tin can configure the field to exist shown on a specific product. The app also supports complex conditions to display a unmarried upload field on multiple product pages. If we would want to place the widget on a specific place on the page, we could also provide a CSS selector. The default location is at the end of the product class.

Testing the file upload

When nosotros visit our product folio, nosotros should see the upload widget shown right above the "Add to cart" button.

You can check a live example on this product folio: personalized-canvas

Seeing the uploaded files

1 overnice additional feature of the app is the ability to see the uploaded files direct in the app.

You can download the files at any time. If the client makes an social club, the files become linked with the order.

Customizing the production page based on the uploaded file

The widget triggers custom events on the form element that you tin can subscribe to with javascript. By subscribing to the "upload:added" issue, nosotros can, for case, alter the product epitome and replace it with the uploaded epitome.

You tin can come across a live instance of the events on this product page: upload-events

If you lot need help with the implementation details, send us a message on the support function of the app!

Customizing the cart page based on the uploaded file

As described before, the uploaded file link is stored in the cart line_item.properties.

This means you tin can access them in your liquid templates to display the uploaded files on your cart page or any page where you lot have access to the cart object.

Again if you need assist with the implementation details, send united states a message on the support function of the app!

Determination

Nosotros have shown you ii unlike means to add a file upload feature to your product pages. Depending on how much coding you want to practise yourself, it is entirely possible to implement the feature with the Shopify core platform. Still, to support ajax cart functionality, we had to modify the theme-specific javascript code.

By using a tertiary-party Shopify app like Upload-Lift, the setup becomes straightforward and allows us to provide a way better user feel.

chowwhishatim.blogspot.com

Source: https://www.cloudlift.app/blogs/shopify/how-to-add-a-file-upload-on-shopify-product-pages

Post a Comment for "How to Do an Html File Upload on Shopify"