App integration
For Application Developers
Pipeline includes a JavaScript file you can use to add custom functionality to the theme and also help integrate Shopify applications with the theme. The file to use is custom.js and is located in the Assets folder:

How to use

By default, Pipeline does not load custom.js, it has been commented out of theme.liquid.
To activate custom.js, simply uncomment the script line in layout/theme.liquid:
The script tag is located near the very bottom of the file:
Copy that line out of the commented-out section and paste it below:
Save the changes to theme.liquid.
Next, you can add your custom application code block to the custom.js file.
We have included an IIFE function example for event listeners:
1
(function() {
2
3
// Below are example event listeners. They listen for theme events that Pipeline
4
// fires in order to make it easier for you to add customizations.
5
6
// Keep your scripts inside this IIFE function call to avoid leaking your
7
// variables into the global scope.
8
9
10
document.addEventListener('theme:variant:change', function(event) {
11
// You might use something like this to write a pre-order feature or a
12
// custom swatch feature.
13
var variant = event.detail.variant;
14
var container = event.target;
15
if (variant) {
16
console.log('Container ———————— ↓');
17
console.log(container);
18
console.log('Variant —————————— ↓');
19
console.log(variant);
20
// ... update some element on the page
21
}
22
});
23
24
document.addEventListener('theme:cart:change', function(event) {
25
var cart = event.detail.cart;
26
if (cart) {
27
console.log('Cart ———————————— ↓');
28
console.log(cart);
29
// ... update an app or a custom shipping caluclator
30
}
31
});
32
// Fired when page loads to update header values
33
document.addEventListener('theme:cart:init', (e) => {
34
console.log('theme:cart:init');
35
console.log(e);
36
});
37
38
// Debounced scroll listeners. Up and down only fire on direction changes
39
// These events are useful for creating sticky elements and popups.
40
document.addEventListener('theme:scroll', e => { console.log(e); });
41
document.addEventListener('theme:scroll:up', e => { console.log(e); });
42
document.addEventListener('theme:scroll:down', e => { console.log(e); });
43
44
// Debounced resize listener to bundle changes that trigger document reflow
45
document.addEventListener('theme:resize', e => { console.log(e); });
46
47
// Locks and unlocks page scroll for modals and drawers
48
// These are commented out because firing them will lock the page scroll
49
// the lock event must set `detail` to the modal or drawer body so the
50
// scroll locking code knows what element should maintain scoll.
51
// document.dispatchEvent(new CustomEvent('theme:scroll:lock', {bubbles: true, detail: scrollableInnerElement}));
52
// document.dispatchEvent(new CustomEvent('theme:scroll:unlock', {bubbles: true}));
53
54
55
// ^^ Keep your scripts inside this IIFE function call to avoid leaking your
56
// variables into the global scope.
57
})();
58
59
Copied!

Use case example

An example use case for adding a "Sell incoming inventory" checkbox to the product detail page:
Shopify includes a limited set of fields at the theme level. We have the ability for each variant to get:
  • Available inventory
  • Incoming inventory (from transfers)
  • Incoming inventory date (also from transfers)
A missing Shopify feature is a checkbox or ability to "Sell incoming inventory". We could say "sell unlimited quantities" or "sell what's available". However, we are unable to say "sell what's available plus the incoming and no more"
Because we are missing that feature, we can write a JavaScript function and add it to custom.js. The feature can manually add our incoming inventory to the "available" column. The logic looks like this:
  • If variant not available => "sold out"
  • If available and also incoming => "pre-order + date"
  • If available and NO incoming => add to cart
We are collapsing "available" and "incoming" together, thus we can only use transfers to track items that will be to going to pre-sell. Otherwise, anything with an incoming shipment will say "preorder".

Custom.js

We can add the code logic into custom.js
1
/**
2
* The pre-order logic looks like this:
3
*
4
* If variant sold out and there is an incoming transfer
5
* Show the date of the incoming transfer
6
* code in snippets/add-to-cart and theme.css
7
* tagged pre-order
8
*/
9
(function() {
10
document.addEventListener('variant:changed', function(event) {
11
var variant = event.detail.variant;
12
var container = event.target;
13
14
var incomingJSON = container.querySelector('[data-incoming]');
15
if (variant) {
16
var incomingArray = null;
17
if (typeof incomingJSON !== 'undefined') {
18
incomingArray = JSON.parse(incomingJSON.innerText);
19
}
20
var arr = incomingArray.variants;
21
for (let index = 0; index < arr.length; index++) {
22
var loopVariant = arr[index];
23
if (variant.id.toString() === loopVariant.id) {
24
variant.incoming = loopVariant.incoming;
25
variant.next_incoming_date = loopVariant.next_incoming_date;
26
}
27
}
28
if (!variant.available && variant.incoming) {
29
// For true pre-order you will want variant.avialable set to true
30
var date = new Date(variant.next_incoming_date);
31
var month = date.toLocaleString('default', { month: 'long' });
32
var day = date.getDate();
33
var dateString = '<p class="ProductForm__Inventory Text--subdued">Order now, aviablable on ' + month + ' ' + day + '</p>';
34
// show the incoming date
35
container.querySelector('[data-incoming-date]').innerHTML = dateString;
36
} else {
37
// wipe the incoming date
38
container.querySelector('[data-incoming-date]').innerHTML = '';
39
}
40
}
41
});
42
})();
Copied!

add-to-cart.liquid

Next, we can add Liquid markup to the add-to-cart.liquid file:
1
{%- comment -%}
2
------------------------------------------------------------------------------------------------------------------------------------------------------
3
PRE-ORDER INCOMING DATE
4
------------------------------------------------------------------------------------------------------------------------------------------------------
5
{%- endcomment -%}
6
<div data-incoming-date></div>
7
<script type="application/json" data-incoming="{{ section.id }}">
8
{
9
"variants":[
10
{%- for variant in product.variants -%}
11
{
12
"id": "{{ variant.id | json }}",
13
"incoming": {{ variant.incoming }},
14
"next_incoming_date": "{{ variant.next_incoming_date | null }}"
15
}{%- unless forloop.last -%},{%- endunless -%}
16
{%- endfor -%}
17
]
18
}
19
</script>
20
{%- comment -%}
21
------------------------------------------------------------------------------------------------------------------------------------------------------
22
END PRE-ORDER
23
------------------------------------------------------------------------------------------------------------------------------------------------------
24
{%- endcomment -%}
25
Copied!

theme.css

Finally, add any CSS styles to customize the product detail page display:
1
* Pre-order incoming date */
2
.ProductForm__Incoming {
3
margin-top: 8px;
4
font-style: italic;
5
}
6
/* End Pre-order */
Copied!