简体   繁体   English

Shopify Buy Button JS 的 openCart() 函数不起作用

[英]openCart() function for Shopify Buy Button JS does not work

I like to set up a Shopify shop via a simple HTML/CSS/JS/PHP website structure, where I include the highly customizable Shopify Buy Button JS library .我喜欢通过一个简单的 HTML/CSS/JS/PHP 网站结构来建立一个 Shopify 商店,我在其中包含了高度可定制的Shopify Buy Button JS 库

I followed the tutorial, and everything is set up correctly, as I can fetch and display all my products, which I have set up via the Shopify dashboard.我遵循了教程,一切都设置正确,因为我可以获取并显示我通过 Shopify 仪表板设置的所有产品。

The default styles and iFrames of the Shopify UI elements ( cart , drawer , buy-button , etc.) do not match the style and UX of my website, therefore I want to customize them via css. Shopify UI 元素( cartdrawerbuy-button等)的默认样式和 iFrame 与我网站的样式和用户体验不匹配,因此我想通过 css 自定义它们。 I deactivated the iFrames of most elements via the directive "iframe: false" , and this works properly.我通过指令"iframe: false"停用了大多数元素的iFrames ,这可以正常工作。

I also want to create a customized toggle button, that opens the cart (instead of the classic toggle button form Shopify, that is fixed to the middle of the right side of the screen).我还想创建一个自定义的toggle按钮,用于打开cart (而不是固定在屏幕右侧中间的经典切换按钮形式 Shopify)。

The weird thing is, I'm not able to open the cart via ui.openCart() , as mentioned in the docs.奇怪的是,如文档中所述,我无法通过ui.openCart()打开cart I'm able to open the cart via ui.openCart() when I do it with a setTimout (3s) , but I'm not able to do so via a jQuery click event .当我使用setTimout (3s)操作时,我可以通过ui.openCart()打开购物车,但是我无法通过jQuery click event What am I doing wrong?我究竟做错了什么?

My code so far:到目前为止我的代码:

<script src="//sdks.shopifycdn.com/buy-button/1.0.0/buybutton.js"></script>
<script>
var client = ShopifyBuy.buildClient({
  domain: 'domain.myshopify.com',
  storefrontAccessToken: '2b3xxxxxxxxjh5', // previously apiKey, now deprecated
});

ui = ShopifyBuy.UI.init(client);

ui.createComponent('product', {
  id: 23xxxxxx56,
  node: document.getElementById('my-product'),
  options: {
    "product": {
      "iframe": false
    },
    toggle: {
      "iframe": false
    }
  }
});

// -- this does not work --
$('#shoppingCartDropdownInvoker').click(function(){
  ui.openCart();
});

// -- this does work --
setTimeout(function(){
  ui.openCart();
}, 3000);
</script>

The code for #shoppingCartDropdownInvoker is: #shoppingCartDropdownInvoker的代码是:

<a id="shoppingCartDropdownInvoker" class="btn btn-xs btn-icon btn-text-secondary" href="javascript:;" role="button">
  <span class="fas fa-shopping-cart btn-icon__inner"></span>
</a>

You need to stop the event from bubbling up the chain.您需要阻止事件在链上冒泡。 Add event.stopPropagation as shown添加 event.stopPropagation 如图

$('#shoppingCartDropdownInvoker').click(function(event){
  event.stopPropagation();
  ui.openCart();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM