I am trying to load the shopify liquid object {{product.price | json}} into JS, but it is showing NaN based on the following code on the FE, any ideas on how to pass liquid into JS?
I have two functions, but neither work with the shopify product price. I have use a static number with sucess but need the price to be dynamic as you would expect.
<div class="afterpay-section" id="afterpay" v-for="item in items" v-bind:class="{'afterpay-label': afterpayAlert}">
${ afterpayPayments }
</div>
<script>
const app = new Vue({
el: '#afterpay',
data: {
textColor: {
color: 'red',
},
textFont: {
'font-weight': 'bold',
},
afterpayMessage: 'Or 4 payments of',
withAfterpay: 'with AfterPay',
failedAlertOne: '',
failedAlertTwo: '',
items: [
{ price: {{ product.price | money_without_trailing_zeros | json }} },
],
},
delimiters: ['${', '}'],
computed: {
afterpayAlert: function() {
if (this.failedAlertOne) {
return this.failedAlertOne + ' ' + this.failedAlertTwo;
} else {
return this.afterpayMessage + ' ' + {{ product.price | money_without_trailing_zeros | json }} + ' ' + this.withAfterpay;
}
},
afterpayCal() {
let productPrice = '{{ product.price | money_without_trailing_zeros | json }}';
let afterpayDiscount = 4;
let totalPrice = productPrice / afterpayDiscount;
if(this.afterpayMessage) {
return this.afterpayMessage + ' ' + {{ product.price | money_without_trailing_zeros | json }} + ' ' + this.withAfterpay;
}
},
afterpayPayments () {
let productPrice = '{{ product.price | money_without_trailing_zeros | json }}';
return this.afterpayMessage + ' ' + ((productPrice / 4) / 100 ).toFixed(2) + ' ' + this.withAfterpay;
}
},
})
</script>
<style>
.afterpay-label {
background-color: transparent;
color: #000;
padding: 10px 0;
margin: 5px 0;
}
</style>
You're using money
filter which most likely will give you a string like "$50" or something like that. Then you use this value in your calculations, ((productPrice / 4) / 100 ).toFixed(2)
, which will give you NaN
for sure.
Don't use money
filter or don't forget to remove $
and any other signs for the value you get.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.