Php 如何使用PayPal智能按钮在商品售罄时显示售罄信息
我目前正在为我的网站的商店工作。我正在使用PayPal智能按钮进行付款。我想知道如何添加库存数量,以便当库存编号为0时,购买功能变得不可用。我在互联网上到处搜索,找不到我问题的答案。这是我目前的代码:Php 如何使用PayPal智能按钮在商品售罄时显示售罄信息,php,paypal,payment-gateway,Php,Paypal,Payment Gateway,我目前正在为我的网站的商店工作。我正在使用PayPal智能按钮进行付款。我想知道如何添加库存数量,以便当库存编号为0时,购买功能变得不可用。我在互联网上到处搜索,找不到我问题的答案。这是我目前的代码: paypal.Buttons({ style: { shape: 'rect', color: 'blue', layout: 'vertical', label: 'paypal', locale:
paypal.Buttons({
style: {
shape: 'rect',
color: 'blue',
layout: 'vertical',
label: 'paypal',
locale: 'en_CY'
},
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: ". $row['paypal_price'] .",
currency: 'EUR'
}
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
localStorage.clear();
window.location.href = 'http://localhost/website/thankyou.php';
//alert('Transaction completed by ' + details.payer.name.given_name + '!');
$stock = 0;
});
}
}).render('#paypal-button-container');
提前谢谢你 您的createOrder函数在没有库存时可能返回false,而不是继续执行操作。order.create您的问题更多的是关于“如何更新当前库存”和“如何获取当前库存” Javascript在客户端运行,因为客户端没有您当前的股票编号,所以您需要使Javascript与服务器通信 实现这一点的一种方法是通过ajax。您可以使用来实现这一点 这就是你必须做的: 在显示按钮(php代码)之前: 在新的
更新_stock.php
中:
<?php
// update your stock-source by reducing it by $_POST['amountOrdered']
<?php
header('Content-Type: application/json');
$currentStock = /* get current stock */;
echo json_encode([
'current' => $currentStock
]);
非常感谢。这很有帮助!您好,我知道您刚才回答了我的问题,但我有一个关于您的解决方案的问题。我使用了你的解决方案,我遇到了一个问题。我在UPDATE_stock.php页面上添加了一个UPDATE语句,但是,当一个peyment被批准时,所有项目的库存编号都会发生变化,而不是购买的项目。我尝试过许多解决方案,但没有一个奏效。我能做些什么来减少所订购物品的库存。再次感谢!在你的
update_stock.php
中,你也必须发送你的商品id,并使用这个id来减少数量。我不知道您的代码,但如果您使用sql更新股票,您必须在update语句中使用id。例如:updateitems set stock=where item\u id=
。如果您省略了将更新所有项目的位置(这就是您所描述的)。如果这没有帮助,创建一个新问题并提供所有必要的细节。
<?php
// update your stock-source by reducing it by $_POST['amountOrdered']
// 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({
<?php
header('Content-Type: application/json');
$currentStock = /* get current stock */;
echo json_encode([
'current' => $currentStock
]);