Ecommerce Shopify WordPress Discussion

How do I assign a Shopify liquid object to a JavaScript variable?

When I try to assign a liquid object, it returns a drop. Here's my code, <script> const customer = '{{ customer }}' console.log(customer) </script> And it returns CustomerDrop as an output. How do I get it as an object?
Try using something like this. const customer = {{ customer | json}} console.log(customer)

December 30, 2023

In shopify, liquid objects are converted into a special data type called drops which can only be used within liquid templates. To access and manipulate liquid objects in js, you need to first convert them into a valid JSON format. To do this, you can use the json filter in liquid, which converts the object into a JSON string. <script> const customerJson = '{{ customer | json }}'; const customer = JSON.parse(customerJson); console.log(customer); </script> This will output the customer object as a string in JSON format, which can then be parsed into a JS object using the JSON.parse()

December 30, 2023

Separate the JSON from the main code. use error check. {% if customer %} <script type="application/json" id="customer"> {{customer | json}} </script> {% endif %} <script> const customerJson = document.querySelector('#customer')?.innerHTML; if(!customerJson) return; const customer = JSON.parse(customerJson); console.log(customer); </script>

December 30, 2023

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.