I need to display a Shopify buy button twice (links to the same product) on a web page because I have a smartphone table and pc table.
With the generated code I got from Shopify, the button only appears on one table because of this JavaScript:
ShopifyBuy.UI.onReady(client).then(function (ui) {
ui.createComponent('product', {
id: '7369316827224',
node: document.getElementById('buybutton'),
The "document.getElementById" only allows for an ID to display once. So in order to be able return all elements with id="buybutton", I changed it to the following:
ShopifyBuy.UI.onReady(client).then(function (ui) {
ui.createComponent('product', {
id: '7369316827224',
node: document.querySelectorAll('#buybutton'),
Additionally, I changed the HTML from
<div id='buybutton'></div>
to
<div id="#buybutton"></div>
However, these changes cause the buy button to disappear from both tables.
I have also tried changing it to getElementByClass
node: document.getElementByClass('buybutton'),
with HTML <div class="buybutton"></div> but this also did not work (nothing appeared).
What am I doing wrong? Is there another way to do what I want to do?
Here is the entire script from Shopify for reference (some info is replaced with "000" for privacy reasons):
/*<![CDATA[*/
(function () {
var scriptURL = 'https://sdks.shopifycdn.com/buy-button/latest/buy-button-storefront.min.js';
if (window.ShopifyBuy) {
if (window.ShopifyBuy.UI) {
ShopifyBuyInit();
} else {
loadScript();
}
} else {
loadScript();
}
function loadScript() {
var script = document.createElement('script');
script.async = true;
script.src = scriptURL;
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(script);
script.onload = ShopifyBuyInit;
}
function ShopifyBuyInit() {
var client = ShopifyBuy.buildClient({
domain: 'sugatsune-uk.myshopify.com',
storefrontAccessToken: '000',
});
ShopifyBuy.UI.onReady(client).then(function (ui) {
ui.createComponent('product', {
id: '7369316827224',
node: document.getElementById('buybutton'),
moneyFormat: '%C2%A3%7B%7Bamount%7D%7D',
options: {
"product": {
"styles": {
"product": {
"@media (min-width: 601px)": {
"max-width": "calc(25% - 20px)",
"margin-left": "20px",
"margin-bottom": "50px"
}
},
"button": {
"font-weight": "bold",
"font-size": "12px",
"padding-top": "6px",
"padding-bottom": "6px",
":hover": {
"background-color": "#2d2d2d"
},
"background-color": "#000000",
":focus": {
"background-color": "#2d2d2d"
},
"padding-left": "6px",
"padding-right": "6px"
},
"quantityInput": {
"font-size": "12px",
"max-width": "20px",
"padding-top": "6px",
"padding-bottom": "6px"
}
},
"contents": {
"img": false,
"button": false,
"buttonWithQuantity": true,
"title": false,
"price": false
},
"text": {
"button": "Add to cart"
}
},
"productSet": {
"styles": {
"products": {
"@media (min-width: 601px)": {
"margin-left": "-20px"
}
}
}
},
"modalProduct": {
"contents": {
"img": false,
"imgWithCarousel": true,
"button": false,
"buttonWithQuantity": true
},
"styles": {
"product": {
"@media (min-width: 601px)": {
"max-width": "100%",
"margin-left": "0px",
"margin-bottom": "0px"
}
},
"button": {
"font-weight": "bold",
"font-size": "12px",
"padding-top": "6px",
"padding-bottom": "6px",
":hover": {
"background-color": "#2d2d2d"
},
"background-color": "#000000",
":focus": {
"background-color": "#2d2d2d"
},
"padding-left": "6px",
"padding-right": "6px"
},
"quantityInput": {
"font-size": "12px",
"max-width": "20px",
"padding-top": "5px",
"padding-bottom": "5px"
}
},
"text": {
"button": "Add to cart"
}
},
"option": {},
"cart": {
"styles": {
"button": {
"font-weight": "bold",
"font-size": "14px",
"padding-top": "15px",
"padding-bottom": "15px",
":hover": {
"background-color": "#2d2d2d"
},
"background-color": "#000000",
":focus": {
"background-color": "#2d2d2d"
}
}
},
"text": {
"total": "Subtotal",
"button": "Checkout"
}
},
"toggle": {
"styles": {
"toggle": {
"font-weight": "bold",
"background-color": "#000000",
":hover": {
"background-color": "#2d2d2d"
},
":focus": {
"background-color": "#2d2d2d"
}
},
"count": {
"font-size": "14px"
}
}
}
},
});
});
}
})();
/*]]>*/