我有以下 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() });
- 1 回答
- 0 關注
- 95 瀏覽
添加回答
舉報
0/150
提交
取消