如何使用 PayPal 智能按钮在商品缺货后显示售罄消息
How to display sold out message once an itemruns out of stock with PayPal smart button
我目前正在为我的网站开发商店。我正在使用 PayPal Smart 按钮进行付款。我想知道如何添加库存数量,以便当库存数量为 0 时,购买能力变得不可用。我在互联网上到处搜索,找不到我的问题的任何答案。这是我当前的代码:
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 函数在没有库存时可以 return false,而不是继续 actions.order.create
您的问题更多是关于“我如何更新我的当前库存”和“我如何获得我的当前库存”
Javascript 在客户端运行,因为您在客户端没有您当前的库存编号,您将需要 Javascript 与您的服务器通信。
一种方法是通过 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([...";
}
在确认付款时更新您的库存:
// 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']
您需要的最后一步是在创建订单之前检查您的库存 (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
]);
我目前正在为我的网站开发商店。我正在使用 PayPal Smart 按钮进行付款。我想知道如何添加库存数量,以便当库存数量为 0 时,购买能力变得不可用。我在互联网上到处搜索,找不到我的问题的任何答案。这是我当前的代码:
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 函数在没有库存时可以 return false,而不是继续 actions.order.create
您的问题更多是关于“我如何更新我的当前库存”和“我如何获得我的当前库存”
Javascript 在客户端运行,因为您在客户端没有您当前的库存编号,您将需要 Javascript 与您的服务器通信。
一种方法是通过 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([...";
}
在确认付款时更新您的库存:
// 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']
您需要的最后一步是在创建订单之前检查您的库存 (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
]);