使用JavaScript自定义显示Shopify购买按钮两次(document.getElementById to document.queryselectorall)

发布于 2025-02-09 10:26:41 字数 4950 浏览 1 评论 0原文

我需要在网页上两次显示Shopify购买按钮(链接到同一产品),因为我有一个智能手机表和PC表。

借助我从Shopify获得的生成的代码,该按钮仅出现在一个表上,因为此JavaScript:

ShopifyBuy.UI.onReady(client).then(function (ui) {
      ui.createComponent('product', {
        id: '7369316827224',
        node: document.getElementById('buybutton'),

“ document.getElementById”仅允许显示一次ID。 因此,为了能够返回具有ID =“ buybutton”的所有元素,我将其更改为以下内容:

ShopifyBuy.UI.onReady(client).then(function (ui) {
  ui.createComponent('product', {
    id: '7369316827224',
    node: document.querySelectorAll('#buybutton'),

此外,我将HTML更改为

<div id='buybutton'></div>

<div id="#buybutton"></div>

但这些更改”,这些更改会导致购买按钮从两张表中消失。

我还尝试将其更改为

node: document.getElementByClass('buybutton'),

使用HTML &lt; div class =“ buybutton”&gt;&lt;/div&gt; getElementByClass,但这也不起作用(什么都没出现)。

我在做什么错?还有另一种方法可以做我想做的事吗?

这是Shopify供参考的整个脚本(由于隐私原因,某些信息被“ 000”替换):

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

脱离于你 2025-02-16 10:26:41

尝试弄乱现有代码是太多的挖掘和风险。最简单,最安全的解决方案是让第二个&lt; butt&gt;单击实际&lt;,每当单击时。这是相同的功能,几乎没有代码。请注意,第二个&lt; button&gt;具有 class =“ buybutton” - 永不复制#ids。

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

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

顺便说一句,&lt; button&gt;在此示例中也将与&lt; div; gt; s一起使用。

单击第一个&lt; button&gt;,然后尝试单击第二个&lt; button&gt;

/*
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>

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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文