buy-button-js icon indicating copy to clipboard operation
buy-button-js copied to clipboard

TypeError when using buybutton js

Open brianrivet-tilt opened this issue 2 years ago • 10 comments

I'm getting the following error when trying to use the buy button:

TypeError: Cannot read properties of undefined (reading 'indexOf') at buybutton.js:12613:25

Here's and example of my code (I have a number of products and they are all doing this):

` var shopui = ShopifyBuy.UI.init(client);

    shopui.createComponent('cart', {
        options: {
            cart: {
                startOpen: false,                       
                templates: {
                    button: '<button class="shopify-buy__btn shopify-buy__btn--cart-checkout btn-checkout" type="button">CHECKOUT</button>'
                },
                styles: {
                    button: {
                        color: '#ffffff',
                        'background-color': '#AF1121',                              
                        'font-weight': 'bold',
                        'text-transform': 'uppercase',
                        ':hover': {
                            'background-color': '#841717'
                        }
                    }
                }
            },
            toggle: {
                styles: {
                    toggle: {
                        'background-color': '#AF1121',
                        ':hover': {
                            'background-color': '#841717'
                        }
                    }
                     
                }
            }
        }
    });


    shopui.createComponent('product', {
        id: {{ entry.productShopifyId }},
        node: document.getElementById('shopControls'), 
       
        options: {
            product: {
                buttonDestination: 'cart',
                iframe: false,
                contents: {
                    img: false,
                    price: true,
                    description: false,
                    title: false,
                    options: true,
                    quantity: false,                        
                    button: true 
                },                                                   
                templates: {
                    button: '<p class="text-left mt-30px mb-30px"><button class="shopify-buy__btn btn-shopify shopify-btn-redHollow">ADD TO CART</button></p>',

                    price: '<div class="pb-30px text-left"><p class="productPrice">{{data.selectedVariant.formattedPrice}}</p></div>'

                }
                
            }
        }
       
    });        

`

brianrivet-tilt avatar Jan 26 '23 23:01 brianrivet-tilt

Hey, I'm seeing this same behavior. Anybody find a solution? I'm running the 2.2.1 version, found it initially with 2.1.8.

MojoMark avatar Feb 02 '23 20:02 MojoMark

Updating to 2.2.1 fixed the problem for me.

brianrivet-tilt avatar Feb 02 '23 20:02 brianrivet-tilt

Having this issue as well with 2.2.1 (originally had the error with 2.1.8)

mjmaurer avatar Feb 05 '23 17:02 mjmaurer

i would echo Brian's comment. I didn't have the reference to 2.2.1 corrected initially. Now it seems to work ok for me.

MojoMark avatar Feb 14 '23 15:02 MojoMark

I'm having this issue again. Updating to 2.4.0 seems to fix that error, but the {{data.selectedVariant.price}} no longer works and is just outputting [object object] can anyone help me to resolve. This is time sensitive.

brianrivet-tilt avatar Aug 16 '23 18:08 brianrivet-tilt

@brianrivet-tilt data.selectedVariant.price is an object consisting of amount and currencyCode. So you would have to print the amount: data.selectedVariant.price.amount. The same goes for compareAtPrice.

https://shopify.dev/docs/api/storefront/2023-07/objects/MoneyV2

murphy1484 avatar Aug 17 '23 08:08 murphy1484

@murphy1484 The data.selectedVariant.price.amount value only seems to have one trailing zero. Do you know of a way to add an additional zero (17.0 --> 17.00)?

philosophy-flow avatar Sep 06 '23 01:09 philosophy-flow

@philosophy-flow I've added following code to format the price to the product options in the createComponent function.

import formatMoney from "@shopify/buy-button-js/lib/utils/money";

const moneyFormat = '€ {{amount_with_comma_separator}}';

product: {
    viewData: {
        formatVariantPrice() {
            return formatMoney(this.data.selectedVariant.price.amount, moneyFormat);
        }
    },
    ...

then in the templates attribute you can use it.

price: `<div>{{ data.formatVariantPrice }}</div>`

murphy1484 avatar Sep 06 '23 06:09 murphy1484

Hi @brianrivet-tilt I'm just using the script so I'm not sure how the solution to upgrade to 2.40 would apply to me. I'm still getting this same error though - any ideas?

dannytrann avatar Sep 09 '23 06:09 dannytrann

Nevermind, I fixed it by adding the script in my HTML file https://sdks.shopifycdn.com/buy-button/2.4.0/buybutton.js instead of the https://sdks.shopifycdn.com/buy-button/1.0.0/buybutton.js

dannytrann avatar Sep 09 '23 06:09 dannytrann