Explore Shopify global objects in Liquid, essential for dynamic theme development and enhancing e-commerce experiences.
Shopify’s Liquid templating language is a cornerstone of theme development, enabling developers to create dynamic and customizable e-commerce storefronts. Among its many features, global objects stand out as essential tools for accessing and manipulating store data. In this blog, we’ll explore Shopify global objects in liquids, covering their use cases, properties, and practical applications.
Liquid uses global objects, which are predefined variables that give access to key information about a Shopify store. These objects demonstrate data structures like products, collections, customers, details in the cart, and more. They are automatically available in all Liquid files, thus being critical for building data-driven dynamic themes.
These objects contain details related to the store itself, such as its name, URL, and localizing settings.
shop: Refers to the store and carries metadata such as the shop name, domain, and currency.
1 2 |
<p>Welcome to {{ shop.name }}!</p> <p>Your currency is set to {{ shop.currency }}</p> |
settings: Retrieves theme settings that can be set in the Shopify admin located under OnlineStore > Customize Theme.
1 |
<p>Support Email: {{ settings.contact_email }}</p> |
These objects refers to logged-in customers and their accounts.
customer: Stores information regarding the currently logged in customer, including name, email, and orders
1 2 3 4 5 |
{% if customer %} <p>Welcome back, {{ customer.first_name }}!</p> {% else %} <p>Please log in to view your account details.</p> {% endif %} |
customer.orders: Retrieves a history of customer orders; however, this can only be accessed with templates such as the customer accounts.
These objects allow for and facilitate the rendering and manipulation of dynamic product and collection data.
product: Refers to one product, usually in the product template, which contains information such as its title, price, variants, and metafields.
1 2 |
<h1>{{ product.title }}</h1> <p>Price: {{ product.price | money }}</p> |
collection: A collection of items, usually in the collection template
1 2 3 4 5 6 |
<h1>{{ collection.title }}</h1> <ul> {% for product in collection.products %} <li>{{ product.title }} - {{ product.price | money }}</li> {% endfor %} </ul> |
These objects assist a developer in managing the cart as well as checkout flow of a customer.
cart: It holds the details of items inside the customer’s cart, including total price and individual line items.
1 2 3 4 5 |
<h2>Your Cart</h2> {% for item in cart.items %} <p>{{ item.product.title }} - Quantity: {{ item.quantity }}</p> {% endfor %} <p>Total: {{ cart.total_price | money }}</p> |
checkout: checkout template is used to access order summary details while using a checkout object.
These objects are helpful in handling content within the blogging feature of Shopify.
blog: Any blog within the Shopify Store.
1 |
<h1>{{ blog.title }}</h1> |
article: Represents any single blog post.
1 2 |
<h2>{{ article.title }}</h2> <p>{{ article.content }}</p> |
These objects help manage URLs and route navigation.
1 2 |
<a href="{{ routes.cart_url }}">Go to Cart</a> <a href="{{ routes.account_login_url }}">Log In</a> |
Metafields extend the functionality of global objects by adding custom fields to products, collections, and other resources.
1 2 |
<p>Material: {{ product.metafields.custom.material }}</p> <p>Care Instructions: {{ product.metafields.custom.care_instructions }}</p> |
You can create personalized shopping experiences by combining global objects like customer and cart.
For example:
1 2 3 |
{% if customer %} <p>Hi {{ customer.first_name }}, you have {{ cart.item_count }} items in your cart.</p> {% endif %} |
Apply Liquid loops and filters to implement more advanced sorting and filtering functionality. For instance, sort the products by price:
1 2 3 4 |
{% assign sorted_products = collection.products | sort: 'price' %} {% for product in sorted_products %} <p>{{ product.title }} - {{ product.price | money }}</p> {% endfor %} |
Shopify global objects are very flexible and very important for dynamic, engaging storefronts. You’ll often be showing product information or personalizing customer experiences by customizing navigation, using a set of object for your theme development.
Mastering and understanding the subtleties of global objects will unlock the full potential of Liquid. Deliver exceptional e-commerce experiences for all of your Shopify clients.
To learn more about Shopify and its capabilities, check out their official website here.
For additional insightful articles and information, please reach out to us.
Web Development Services in the United States