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

如何更改每个订单的 PayPal 商品数组?

如何更改每个订单的 PayPal 商品数组?

喵喵时光机 2023-10-24 15:10:33
我有以下 JavaScript 在我的网站上加载 PayPal 按钮,我需要更改此代码,以便添加到购物车中的商品也出现在 PayPal 发票中:<script src="cart.js"></script>    <script>        paypal.Buttons({            createOrder: function(data, actions) {                // This function sets up the details of the transaction, including the amount and line item details.                return actions.order.create({                    purchase_units: [{                        amount: {                            value: countCartTotal()                        }                    }]                });            },            onApprove: function(data, actions) {                // This function captures the funds from the transaction.                return actions.order.capture().then(function(details) {                    // This function shows a transaction success message to your buyer.                    window.location.href = "orderConfirmed.php"                    clearCart()                });            }        }).render('#paypal-button-container');        //This function displays Smart Payment Buttons on your web page.    </script>它加载在我的网页底部,“cart.js”保存整个购物车的价值,例如,如果总金额为 20.00 美元,则用户将在 PayPal 窗口中被收取 20.00 美元。我已经在 PayPal 的沙盒环境中测试了这段代码,它可以工作。但它没有给我购物车中的物品。我从这里的用户那里得到了以下代码:"purchase_units": [{      "description": "Stuff",      "amount": {        "value": "20.00",        "currency_code": "USD",        "breakdown": {          "item_total": {            "currency_code": "USD",            "value": "20.00"          },        }      },      "items": [        {          "unit_amount": {            "currency_code": "USD",            "value": "10.00"          },          "quantity": "1",          "name": "Item 1",        },        {          "unit_amount": {            "currency_code": "USD",            "value": "10.00"          },          "quantity": "1",          "name": "Item 2",        },      ],    }  ]另外,当我添加到countCartTotal()两个“值:”时它会停止工作吗?给我答案的用户说这是直接的JS,但我的知识有限。
查看完整描述

1 回答

?
MMTTMM

TA贡献1869条经验 获得超4个赞

另外,当我将 countCartTotal() 添加到两个“值:”时,它会停止工作吗?

由于您使用的是逐项细分,因此存在这 2 个值以及与您拥有的项目一样多的值。您将需要计算和更新所有事物的价值,并且它们必须以数学方式相加。如果你改变总数而不使项目加起来等于总数,它确实会停止工作。

我建议的方法是创建一个新函数getPurchaseUnits()或类似函数。

这应该返回一个与您硬编码的数组非常相似的数组purchase_units,但项目和正确的总数都已更新。当然,编写此类代码需要一定程度的编程知识,因此您可以将其视为学习一些 Javascript 的机会——就像家庭作业问题一样。

一旦你有了这样一个返回你需要的数组的函数,你就可以把它放在里面,

return actions.order.create({
    getPurchaseUnits()
});


查看完整回答
反对 回复 2023-10-24
  • 1 回答
  • 0 关注
  • 46 浏览

添加回答

举报

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