Displaying Shopify buy button twice on a page with JavaScript Customization (document.getElementById to document.querySelectorAll)

Viewed 67

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"
      }
    }
  }
},
      });
    });
  }
})();
/*]]>*/
1 Answers

It's too much digging and risky to try to mess with the existing code. The easiest and safest solution is to have the second <button> click the real <button> whenever it is clicked. It's the same functionality with very little code. Note, the second <button> has class="buybutton" -- never duplicate #ids.

document.querySelector('.buybutton').onclick = clickBuyButton;

function clickBuyButton(e) {
  document.getElementById('buybutton').click();
}

BTW, the <button>s in this example will work with <div>s as well.

Click the first <button>, then try clicking the second <button>

/*
This part is just functional for demonstration purposes. 
Whatever happens when #buybutton is clicked is of no consequence.
*/
document.getElementById('buybutton').onclick = buy;

function buy(e) {
  const clicked = e.target;
  if (clicked.matches('#buybutton')) {
    document.getElementById('shop').classList.toggle('open');
  }
}

/*
This is the second <button>. It's only purpose is to click
#buybutton whenever it is clicked
*/
document.querySelector('.buybutton').onclick = clickBuyButton;

function clickBuyButton(e) {
  document.getElementById('buybutton').click();
}
fieldset {
  min-height: 30vh;
}

#shop {
  transform: scaleY(0);
  transition: 0.5s;
}

#shop.open {
  transform:scaleY(1);
  transition: 0.3s;
}
<form>
  <fieldset>
    <button id='buybutton' type='button'>BUY</button>
  </fieldset>
  <fieldset id='shop'>
    <legend>BUY STUFF NOW!</legend>
  </fieldset>
  <fieldset>
    <button class='buybutton' type='button'>BUY</button>
  </fieldset>
</form>

Related