如何防止“立即购买”按钮重定向到结账页面?

问题描述 投票:0回答:1

在我的 Shopify 应用程序中,我想在“立即购买”按钮重定向到 Shopify 默认主题 Dawn 中的结账页面之前执行一些任务。在我的主题应用程序扩展中,我选择按钮作为 dom 元素并尝试了 PreventDefault 函数,但它仍在重定向。我怎样才能停止重定向?

var paymentButton = document.querySelector('.shopify-payment-button');

  if (paymentButton) {
   
    paymentButton.addEventListener('click', function(event) {
      event.preventDefault();
      event.stopPropagation();


      // things I want to do
    });
  }
javascript shopify shopify-app
1个回答
0
投票
create a clone of the button component to remove the functions


const clone = paymentButton.clo`enter code here`neNode(true); a`enter code here`
  paymentButton.parentNode.replaceChild(clone, paymentButton);

and pass the addEventListener to the clone

 clone.addEventListener('click', function(event) {
  event.preventDefault();
  event.stopPropagation();


  
});
© www.soinside.com 2019 - 2024. All rights reserved.