为了账号安全,请及时绑定邮箱和手机立即绑定

通过 Ajax 更新 PayPal Smart Button

通过 Ajax 更新 PayPal Smart Button

牛魔王的故事 2023-04-01 16:09:36
我有一个通过 Ajax 更新的购物车(数量,删除产品)。如何更新智能按钮 iframe 中的值?当我刷新页面时它显然有效,但是如何使用 Ajax 在后台进行呢?我尝试使用绕过同源策略的 hack 重新加载 PayPal iframe,但它没有用,Smart Button 消失了。这就是我正在谈论的技巧:const iframe = document.querySelector("iframe");iframe.src = iframe.src这是我的智能按钮代码:<script>     paypal.Buttons({    style: {      shape: "rect",      color: "gold",      layout: "horizontal",      label: "checkout",      size: "responsive",      tagline: "false"  },    createOrder: function(data, actions) {      return actions.order.create({        purchase_units: [{          amount: {            currency_code: "GBP",            value: <?php echo number_format($total, 2); ?>          },        }]      });    },    onApprove: function(data, actions) {      return actions.order.capture().then(function(details) {        alert("Dear " + details.payer.name.given_name + ", Thank you for your payment!");      });    },    onShippingChange: function(data, actions) {      if (data.shipping_address.country_code !== "GB") {        return actions.reject();      }      return actions.resolve();    }  }).render("#paypal-button-container");}</script>
查看完整描述

1 回答

?
千巷猫影

TA贡献1829条经验 获得超7个赞

更改此行:


value: <?php echo number_format($total, 2); ?>

要调用 JavaScript 函数,您需要编写该函数并将其作为页面的一部分


value: getCartTotal()

在某处,也许在 的顶部<script>,您会看到类似以下内容的内容:


window.myCartTotal = '<?php echo number_format($total, 2); ?>';

function getCartTotal() {

  return window.myCartTotal;

}

然后让你的 AJAX 也更新 window.myCartTotal


查看完整回答
反对 回复 2023-04-01
  • 1 回答
  • 0 关注
  • 92 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信