Chris Hager
Chris Hager

Reputation: 459

Shopify Buy Button Error: "cannot read property '1' of null"

When I'm creating a Shopify "Buy Button" of any type, the generated HTML code does not work. The Chrome Developer Tools Console tells me: buy-button-storefront.js:2999: cannot read property '1' of null.

In the Chrome debugger, I see that the function formatMoney(amount, format) is called with a format parameter which does not work with the placeholderRegex:

Chrome debugger

Any help would be much appreciated.

Upvotes: 1

Views: 2403

Answers (3)

Scott
Scott

Reputation: 893

When money_format isn't set correctly, Shopify's formatMoney() regex fails.

Check your money format: console.log(Shopify.api.getMoneyFormat())

For me, this was returning $ instead of ${{amount}} - It looks like yours is returning instead of €{{amount}}

After checking my script, I was setting the format correctly to begin with - but my JS file had a .liquid extension - so Shopify was seeing {{amount}} as a variable, which rendered blank on the client side. You can avoid this behaviour by using the %raw% tags in your liquid file.

{% raw %} var money_format = '${{amount}}'; {% endraw %}

Upvotes: 4

MyShopify Consultants
MyShopify Consultants

Reputation: 136

I seen this problem a handful of times, its odd it just seems to pop up and I've never actually got to the bottom of it.

Sometimes resetting the money format does the trick

<script>Shopify.money_format = "{{ shop.money_format }}";</script>

I can't take credit for that,I found it on github a while back

Upvotes: 0

Ashiquzzaman Ome
Ashiquzzaman Ome

Reputation: 46

You have to use JavaScript SDk to avoid this problem . Generate a new snippet from Buy Button > JavaScript SDk > Generate code .

I think then it will be solve.

Upvotes: 0

Related Questions