2 回答

TA貢獻(xiàn)1839條經(jīng)驗(yàn) 獲得超15個贊
您的問題更多是關(guān)于“如何更新當(dāng)前庫存”和“如何獲取當(dāng)前庫存”
Javascript 在客戶端運(yùn)行,由于您在客戶端沒有當(dāng)前的庫存編號,因此您需要使 Javascript 與您的服務(wù)器進(jìn)行通信。
一種方法是通過 ajax。您可以使用fetch-api來完成此操作。
這是你必須做的:
在顯示按鈕之前(php 代碼):
<?php
// your code ...
// check stock before you echo. Only echo when $stock is > 0
$stock = /* get current stock-amount */;
if ($stock > 0) {
echo "paypal.Buttons([...";
}
確認(rèn)付款后更新您的庫存:
// your current code
localStorage.clear();
window.location.href = 'http://localhost/website/thankyou.php';
// alert('Transaction completed by ' + details.payer.name.given_name + '!');
// $stock = 0;
// add something like this
const formData = new FormData();
formData.append('amountOrdered', /* number as string */);
// maybe use await to block further processing until stock has been updated
fetch('update_stock.php', {
method: 'POST',
body: formData
});
在你的新update_stock.php:
<?php
// update your stock-source by reducing it by $_POST['amountOrdered']
您需要的最后一步是在創(chuàng)建訂單 (JS) 之前檢查您的庫存:
// async here needed so you can use await
createOrder: async function(data, actions) {
// check stock here
let stock = (await fetch('get_current_stock.php')).json();
let currentStock = stock['current'];
// you may want to check for amoutTryingToOrder instead of 1
if (currentStock < 1) {
alert('Out of stock, sorry :(');
return false;
}
return actions.order.create({
在你的get_current_stock.php:
<?php
header('Content-Type: application/json');
$currentStock = /* get current stock */;
echo json_encode([
'current' => $currentStock
]);

TA貢獻(xiàn)1111條經(jīng)驗(yàn) 獲得超0個贊
當(dāng)沒有庫存時,您的 createOrder 函數(shù)可能會返回 false,而不是繼續(xù)執(zhí)行 actions.order.create
- 2 回答
- 0 關(guān)注
- 150 瀏覽
添加回答
舉報